Skip to content

Package: ConstructorPropertiesAnnotationIntrospector

ConstructorPropertiesAnnotationIntrospector

nameinstructionbranchcomplexitylinemethod
ConstructorPropertiesAnnotationIntrospector(JsonbContext, AnnotationFinder)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
createJsonbCreator(Executable, String[])
M: 38 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
forContext(JsonbContext)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getCreator(Constructor[])
M: 79 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 16 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toString()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0 which is available at
6: * http://www.eclipse.org/legal/epl-2.0,
7: * or the Eclipse Distribution License v. 1.0 which is available at
8: * http://www.eclipse.org/org/documents/edl-v10.php.
9: *
10: * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11: */
12:
13: package org.eclipse.yasson.internal;
14:
15: import java.lang.reflect.Constructor;
16: import java.lang.reflect.Executable;
17: import java.lang.reflect.Modifier;
18: import java.lang.reflect.Parameter;
19: import java.util.Arrays;
20: import java.util.logging.Logger;
21:
22: import org.eclipse.yasson.internal.model.CreatorModel;
23: import org.eclipse.yasson.internal.model.JsonbCreator;
24: import org.eclipse.yasson.internal.properties.MessageKeys;
25: import org.eclipse.yasson.internal.properties.Messages;
26:
27: class ConstructorPropertiesAnnotationIntrospector {
28:
29: private static final Logger LOG = Logger.getLogger(ConstructorPropertiesAnnotationIntrospector.class.getName());
30:
31: private final JsonbContext jsonbContext;
32: private final AnnotationFinder constructorProperties;
33:
34: public static ConstructorPropertiesAnnotationIntrospector forContext(JsonbContext jsonbContext) {
35: return new ConstructorPropertiesAnnotationIntrospector(jsonbContext, AnnotationFinder.findConstructorProperties());
36: }
37:
38: /**
39: * Only for testing and internal purposes.
40: * <p>
41: * Please use static factory methods e.g. {@link #forContext(JsonbContext)}.
42: *
43: * @param context {@link JsonbContext}
44: * @param annotationFinder {@link AnnotationFinder}
45: */
46: protected ConstructorPropertiesAnnotationIntrospector(JsonbContext context, AnnotationFinder annotationFinder) {
47: this.jsonbContext = context;
48: this.constructorProperties = annotationFinder;
49: }
50:
51: public JsonbCreator getCreator(Constructor<?>[] constructors) {
52: JsonbCreator jsonbCreator = null;
53:
54:• for (Constructor<?> constructor : constructors) {
55: Object properties = constructorProperties.valueIn(constructor.getDeclaredAnnotations());
56:• if (!(properties instanceof String[])) {
57: continue;
58: }
59:• if (!Modifier.isPublic(constructor.getModifiers())) {
60: String declaringClass = constructor.getDeclaringClass().getName();
61: String message = "The constructor of {0} annotated with @ConstructorProperties {1} is not accessible and will "
62: + "be ignored.";
63: LOG.finest(String.format(message, declaringClass, Arrays.toString((String[]) properties)));
64: continue;
65: }
66:• if (jsonbCreator != null) {
67: // don't fail in this case, because it is perfectly allowed to have more than one
68: // @ConstructorProperties-Annotation in general.
69: // It is just undefined, which constructor to choose for JSON in this case.
70: // The behavior should be the same (null), as if there is no ConstructorProperties-Annotation at all.
71: LOG.warning(Messages.getMessage(MessageKeys.MULTIPLE_CONSTRUCTOR_PROPERTIES_CREATORS,
72: constructor.getDeclaringClass().getName()));
73: return null;
74: }
75: jsonbCreator = createJsonbCreator(constructor, (String[]) properties);
76: }
77: return jsonbCreator;
78: }
79:
80: private JsonbCreator createJsonbCreator(Executable executable, String[] properties) {
81: final Parameter[] parameters = executable.getParameters();
82:
83: CreatorModel[] creatorModels = new CreatorModel[parameters.length];
84:• for (int i = 0; i < parameters.length; i++) {
85: final Parameter parameter = parameters[i];
86: creatorModels[i] = new CreatorModel(properties[i], parameter, executable, jsonbContext);
87: }
88: return new JsonbCreator(executable, creatorModels);
89: }
90:
91: @Override
92: public String toString() {
93: return "ConstructorPropertiesAnnotationIntrospector [jsonbContext=" + jsonbContext + ", constructorProperties=" + constructorProperties + "]";
94: }
95: }