/** Returns true if the inject annotation is on the constructor. */ private static boolean hasAtInject(Constructor cxtor) { return cxtor.isAnnotationPresent(Inject.class) || cxtor.isAnnotationPresent(javax.inject.Inject.class); }
public boolean callConstructorIsAnnotationPresent(Constructor<?> m, Class<? extends Annotation> annotClass) { return m.isAnnotationPresent(annotClass); } public Annotation callConstructorGetAnnotation(Constructor<?> m, Class<? extends Annotation> annotClass) {
/** * Check if constructor can be used for deserialization. */ private static boolean checkConstructor(Constructor<?> constructor, boolean hasParams) { return Modifier.isPublic(constructor.getModifiers()) && !constructor.isAnnotationPresent(Transient.class) && ((constructor.getParameterCount() > 0) == hasParams); }
private void handleClass(Class<?> cl) { if (stopClasses.contains(cl) || cl.isAnnotationPresent(QueryExclude.class)) { return; } else if (cl.isAnnotationPresent(entityAnnotation)) { entityTypes.put(cl, null); } else if (cl.isAnnotationPresent(embeddableAnnotation)) { embeddableTypes.put(cl, null); } else if (cl.isAnnotationPresent(supertypeAnnotation)) { superTypes.put(cl, null); } else { for (Constructor<?> constructor : cl.getConstructors()) { if (constructor.isAnnotationPresent(QueryProjection.class)) { projectionTypes.put(cl, null); break; } } } }
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; }
private static <T> Constructor<T> findImplicitConstructorFor(Class<T> type) { @SuppressWarnings("unchecked") final Constructor<T>[] constructors = (Constructor<T>[]) type.getDeclaredConstructors(); List<Constructor<T>> annotatedConstructors = Stream.of(constructors) .filter(c -> c.isAnnotationPresent(ConstructorProperties.class)) .collect(Collectors.toList()); if (annotatedConstructors.size() > 1) { throw new IllegalArgumentException(type + " may have at most one constructor annotated @ConstructorProperties"); } else if (annotatedConstructors.size() == 1) { return annotatedConstructors.get(0); } if (constructors.length != 1) { throw new IllegalArgumentException(type + " must have exactly one constructor, or specify it with @JdbiConstructor or @ConstructorProperties"); } return constructors[0]; } }
/** * Find an invokable constructor. Prefer an {@link JdbiConstructor} annotated * one if present. Throws if multiple or zero candidates are found. * * @param <T> the type to inspect * @param type the type to inspect * @return the preferred constructor */ public static <T> Constructor<T> findConstructorFor(Class<T> type) { @SuppressWarnings("unchecked") final Constructor<T>[] constructors = (Constructor<T>[]) type.getDeclaredConstructors(); List<Constructor<T>> annotatedConstructors = Stream.of(constructors) .filter(c -> c.isAnnotationPresent(JdbiConstructor.class)) .collect(Collectors.toList()); if (annotatedConstructors.size() > 1) { throw new IllegalArgumentException(type + " may have at most one constructor annotated @JdbiConstructor"); } else if (annotatedConstructors.size() == 1) { return annotatedConstructors.get(0); } return findImplicitConstructorFor(type); }
/** * Find an invokable instance factory, such as a constructor or a static factory method. * Prefer an {@link JdbiConstructor} annotated constructor or static factory method if * one is present. Throws if multiple or zero candidates are found. * * @param <T> the type to inspect * @param type the type to inspect * @return the preferred constructor or static factory method */ static <T> InstanceFactory<T> findFactoryFor(Class<T> type) { @SuppressWarnings("unchecked") final Constructor<T>[] constructors = (Constructor<T>[]) type.getDeclaredConstructors(); List<Constructor<T>> explicitConstructors = Stream.of(constructors) .filter(constructor -> constructor.isAnnotationPresent(JdbiConstructor.class)) .collect(Collectors.toList()); List<Method> explicitFactoryMethods = Stream.of(type.getDeclaredMethods()) .filter(method -> method.isAnnotationPresent(JdbiConstructor.class)) .collect(Collectors.toList()); if (explicitConstructors.size() + explicitFactoryMethods.size() > 1) { throw new IllegalArgumentException(type + " may have at most one constructor or static factory method annotated @JdbiConstructor"); } if (explicitConstructors.size() == 1) { return new ConstructorInstanceFactory<>(explicitConstructors.get(0)); } if (explicitFactoryMethods.size() == 1) { return new StaticMethodInstanceFactory<>(type, explicitFactoryMethods.get(0)); } return new ConstructorInstanceFactory<>(findImplicitConstructorFor(type)); }
Constructor<?> constructor = null; for (Constructor<?> c : implementation.getConstructors()) { if (c.isAnnotationPresent(Inject.class)) { constructor = c; break;
if (!constructor.isAnnotationPresent(Inject.class)) { continue;
private void addConstructors(Class<?> cl, EntityType type) { for (Constructor<?> constructor : cl.getConstructors()) { if (constructor.isAnnotationPresent(QueryProjection.class)) { List<Parameter> parameters = Lists.newArrayList(); for (int i = 0; i < constructor.getParameterTypes().length; i++) { Type parameterType = typeFactory.get( constructor.getParameterTypes()[i], constructor.getGenericParameterTypes()[i]); for (Annotation annotation : constructor.getParameterAnnotations()[i]) { if (annotation.annotationType().equals(QueryType.class)) { QueryType queryType = (QueryType) annotation; parameterType = parameterType.as(TypeCategory.valueOf(queryType.value().name())); } } parameters.add(new Parameter("param" + i, parameterType)); } type.addConstructor(new com.mysema.codegen.model.Constructor(parameters)); } } }
} else { for (Constructor<?> c : cons) { if (c.isAnnotationPresent(Inject.class)) { if (inject != null) { throw new IllegalStateException(
if (ctor.isAnnotationPresent(Inject.class)) { injectCtors.add((Constructor<T>) ctor); } else {
private Constructor<?> findAnnotatedConstructor(final Constructor<?>[] constructors) { for (final Constructor<?> constructor : constructors) { if (constructor.isAnnotationPresent(AutomapConstructor.class)) { return constructor; } } return null; }
private Constructor<?> findDefaultConstructor(final Constructor<?>[] constructors) { if (constructors.length == 1) return constructors[0]; for (final Constructor<?> constructor : constructors) { if (constructor.isAnnotationPresent(AutomapConstructor.class)) { return constructor; } } return null; }
private void verifyJson(final Class<?> cls) { final List<Constructor<?>> constructors = Arrays .stream(cls.getConstructors()) .filter(c -> c.isAnnotationPresent(ConstructorProperties.class) || c.isAnnotationPresent(JsonCreator.class)) .collect(Collectors.toList()); final List<Method> jsonCreators = Arrays .stream(cls.getMethods()) .filter(m -> ((m.getModifiers() & Modifier.STATIC) != 0) && m.isAnnotationPresent(JsonCreator.class)) .collect(Collectors.toList()); if (constructors.size() + jsonCreators.size() != 1) { throw new AssertionError( "Constructors (" + constructors + ") and @JsonCreator methods (" + jsonCreators + ") conflicting for JSON de-serialization"); } }
private Optional<Object> lookupClass(final Class<?> cls, final boolean secondary) { return Arrays .stream(cls.getConstructors()) .filter(c -> c.isAnnotationPresent(ConstructorProperties.class)) .findAny() .map(c -> { final String[] names = c.getAnnotation(ConstructorProperties.class).value(); final Type[] types = c.getGenericParameterTypes(); if (names.length != types.length) { throw new IllegalArgumentException( "@ConstructorProperties number of arguments do not match number of " + "parameters: " + c); } final Object[] arguments = new Object[types.length]; for (int i = 0; i < types.length; i++) { arguments[i] = lookup(types[i], secondary, names[i]); } try { return c.newInstance(arguments); } catch (final ReflectiveOperationException e) { throw Throwables.propagate(e); } }); }
public void verify() { final ValueSuppliers suppliers = new ValueSuppliers(valueSuppliers); final FakeValueProvider valueProvider = new FakeValueProvider(suppliers); final Constructor<?> constructor = Arrays .stream(cls.getConstructors()) .filter(c -> c.isAnnotationPresent(ConstructorProperties.class)) .findAny() .orElseThrow(() -> new IllegalArgumentException( "No constructor annotated with @ConstructorProperties")); final List<FieldSpec> specs = buildFieldSpecs(constructor, valueProvider); if (checkGetters) { verifyGetters(constructor, specs); } verifyEquals(constructor, specs); verifyToString(constructor, specs); if (checkJson) { verifyJson(cls); } }
/** * Returns <jk>true</jk> if the specified constructor doesn't have the {@link Deprecated @Deprecated} annotation on it. * * @param c The constructor. * @return <jk>true</jk> if the specified constructor doesn't have the {@link Deprecated @Deprecated} annotation on it. */ public static boolean isNotDeprecated(Constructor<?> c) { return ! c.isAnnotationPresent(Deprecated.class); }
public static boolean isAnnotationPresent(Constructor<?> clazz, Class<? extends Annotation> anType) { if (AnnotationDataStore.isConstructorDataRecorded(clazz)) { return AnnotationDataStore.isConstructorAnnotationPresent(clazz, anType); } return clazz.isAnnotationPresent(anType); }