public Annotation callConstructorGetAnnotation(Constructor<?> m, Class<? extends Annotation> annotClass) { return m.getAnnotation(annotClass); } public List<List<Annotation>> callConstructorGetParameterAnnotations(Constructor<?> m) {
@Nullable public static String[] evaluate(Constructor<?> candidate, int paramCount) { ConstructorProperties cp = candidate.getAnnotation(ConstructorProperties.class); if (cp != null) { String[] names = cp.value(); if (names.length != paramCount) { throw new IllegalStateException("Constructor annotated with @ConstructorProperties but not " + "corresponding to actual number of parameters (" + paramCount + "): " + candidate); } return names; } else { return null; } } }
@SuppressWarnings("unchecked") private boolean injectOnConstructor(final Class clazz) { final Constructor[] cs = clazz.getDeclaredConstructors(); for (final Constructor constructor : cs) { if (constructor.getAnnotation(Inject.class) != null) { return true; } } return false; }
private boolean shouldInject(final Class<?> clazz) { for(Constructor<?> constructor : clazz.getDeclaredConstructors()) { if (constructor.getAnnotation(Inject.class) != null) { return true; } } return false; } }
/** * {@inheritDoc} */ public Instantiator replaceBy(Resolved instantiator) { Priority left = constructor.getAnnotation(Priority.class), right = instantiator.constructor.getAnnotation(Priority.class); int leftPriority = left == null ? Priority.DEFAULT : left.value(), rightPriority = right == null ? Priority.DEFAULT : right.value(); if (leftPriority > rightPriority) { return this; } else if (leftPriority < rightPriority) { return instantiator; } else { throw new IllegalStateException("Ambiguous constructors " + constructor + " and " + instantiator.constructor); } }
@CheckForNull public static Constructor getDataBoundConstructor(@Nonnull Class type) { for (Constructor c : type.getConstructors()) { if (c.getAnnotation(DataBoundConstructor.class) != null) return c; } return null; }
@Override public <A extends IAnnotation> A findAnnotation(Constructor<?> cons, Class<A> annotationClass) { final Class<? extends Annotation> a = m_annotationMap.get(annotationClass); if (a == null) { throw new IllegalArgumentException("Java @Annotation class for '" + annotationClass + "' not found."); } Annotation annotation = cons.getAnnotation(a); return findAnnotation(cons.getDeclaringClass(), annotation, annotationClass, null, cons, null, new Pair<>(annotation, cons)); }
@Nullable public static String[] evaluate(Constructor<?> candidate, int paramCount) { ConstructorProperties cp = candidate.getAnnotation(ConstructorProperties.class); if (cp != null) { String[] names = cp.value(); if (names.length != paramCount) { throw new IllegalStateException("Constructor annotated with @ConstructorProperties but not " + "corresponding to actual number of parameters (" + paramCount + "): " + candidate); } return names; } else { return null; } } }
private boolean canUseConstructor(Constructor<?> constructor) { if (!Modifier.isPublic(constructor.getModifiers())) { return false; } if (!constructor.isAnnotationPresent(CliObjectSupport.class)) { return false; } for (Class<?> param : constructor.getParameterTypes()) { if (param != String.class) { return false; } } return constructor.getParameterTypes().length == constructor.getAnnotation(CliObjectSupport.class).argumentNames().length; }
public void checkOverriddenInjectAnnotatedClass(List<Object> modules) { Set<String> errors = new HashSet<>(); for (Map.Entry<ObjectId, Provider> entry : fields.entrySet()) { ObjectId objectId = entry.getKey(); Constructor[] constructors = objectId.objectClass.getConstructors(); for (Constructor constructor : constructors) { if (constructor.getAnnotation(Inject.class) != null && !existProvidesMethodInModule(objectId, modules)) { errors.add(objectId.objectClass.getName()); } } } ErrorsFormatter.throwExceptionOnErrors( "Error while trying to override objects", errors, "You must define overridden objects using a @Provides annotated method instead of using @Inject annotation"); }
JSONCreator annotation = constructor.getAnnotation(JSONCreator.class); if (annotation != null) { if (creatorConstructor != null) {
@SuppressWarnings("MagicConstant") private boolean isCompatible(final Constructor<?> constructor) { if (constructor.getAnnotation(Inject.class) != null) { // JSR-330 applicable return true; } final int paramSize = constructor.getParameterTypes().length; if (paramSize != 0 && resolverAnnotations.get().isEmpty()) { return false; } if (!Modifier.isPublic(constructor.getModifiers())) { // return true for a default constructor, return false otherwise. return paramSize == 0 && (constructor.getDeclaringClass().getModifiers() & (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE)) == constructor.getModifiers(); } for (final Annotation[] paramAnnotations : constructor.getParameterAnnotations()) { boolean found = false; for (final Annotation paramAnnotation : paramAnnotations) { if (resolverAnnotations.get().contains(paramAnnotation.annotationType())) { found = true; break; } } if (!found) { return false; } } return true; }
@Override public <A extends IAnnotation> A findAnnotation(Constructor<?> cons, Class<A> annotationClass) { final Class<? extends Annotation> a = m_annotationMap.get(annotationClass); if (a == null) { throw new IllegalArgumentException( "Java @Annotation class for '" + annotationClass + "' not found."); } Annotation annotation = cons.getAnnotation(a); return findAnnotation( cons.getDeclaringClass(), annotation, annotationClass, null, cons, null, new Pair<>(annotation, cons), null); }
ConstructorProperties cp = ctor.getAnnotation(ConstructorProperties.class); String[] paramNames = (cp != null ? cp.value() : parameterNameDiscoverer.getParameterNames(ctor)); Assert.state(paramNames != null, () -> "Cannot resolve parameter names for constructor " + ctor);
@DataProvider(name = "dp") public static Object[][] createData(Constructor c) { Assert.assertEquals(c.getDeclaringClass(), ConstructorSample.class); Assert.assertNotNull(c.getAnnotation(Factory.class)); Assert.assertEquals(c.getParameterTypes().length, 1); Assert.assertEquals(c.getParameterTypes()[0], String.class); return new Object[][] {{"Cedric"}, {"Alois"}}; }
@Test public void javaLangAnnotationTypeViaFindMergedAnnotation() throws Exception { Constructor<?> deprecatedCtor = Date.class.getConstructor(String.class); assertEquals(deprecatedCtor.getAnnotation(Deprecated.class), findMergedAnnotation(deprecatedCtor, Deprecated.class)); assertEquals(Date.class.getAnnotation(Deprecated.class), findMergedAnnotation(Date.class, Deprecated.class)); }
@DataProvider(name = "dp1") public static Object[][] createData1(ConstructorOrMethod cOrM) { Assert.assertEquals(cOrM.getDeclaringClass(), ConstructorOrMethodSample.class); Assert.assertNull(cOrM.getMethod()); Assert.assertNotNull(cOrM.getConstructor()); Constructor c = cOrM.getConstructor(); Assert.assertNotNull(c.getAnnotation(Factory.class)); Assert.assertEquals(c.getParameterTypes().length, 1); Assert.assertEquals(c.getParameterTypes()[0], String.class); return new Object[][] {{"0"}, {"1"}}; }
@Test public void constructorPass() throws Exception { check(ImmutableValForPass.class.getConstructor(int.class).getAnnotation(B1.class)).notNull(); check(ImmutableValForPass.class.getConstructor(int.class).getAnnotation(C1.class)).notNull(); } }
ConstructorProperties cp = ctor.getAnnotation(ConstructorProperties.class); String[] paramNames = (cp != null ? cp.value() : parameterNameDiscoverer.getParameterNames(ctor)); Assert.state(paramNames != null, () -> "Cannot resolve parameter names for constructor " + ctor);
@DataProvider(name = "cookie-master") public static Object[][] cookies(ConstructorOrMethod method) { TestInfo value; if (method.getConstructor() != null) { value = (TestInfo) method.getConstructor().getAnnotation(TestInfo.class); } else { value = method.getMethod().getAnnotation(TestInfo.class); } String name = value.name(); if ("glutton".equalsIgnoreCase(name)) { return new Object[][] {{"oreo", 200}}; } if ("nibbler".equalsIgnoreCase(name)) { return new Object[][] {{"marie-gold", 10}}; } return new Object[][] { {"oreo", 200}, {"marie-gold", 10} }; } }