Example usage for java.lang.reflect Constructor getAnnotation

List of usage examples for java.lang.reflect Constructor getAnnotation

Introduction

In this page you can find the example usage for java.lang.reflect Constructor getAnnotation.

Prototype

public <T extends Annotation> T getAnnotation(Class<T> annotationClass) 

Source Link

Usage

From source file:Main.java

public static void print_method_or_constructor(Member member) {
    Constructor c = (Constructor) member;
    Annotation type = c.getAnnotation(Deprecated.class);

}

From source file:org.movealong.junitfu.ConstructorMockBinder.java

private static Collection<? extends MockBinder> createMockBinders(Constructor<?> constructor) {
    if (constructor.getAnnotation(Inject.class) != null) {
        HashSet<MockBinder> mockBinders = new HashSet<MockBinder>();
        for (int parameterIndex = 0; parameterIndex < constructor
                .getParameterTypes().length; parameterIndex++) {
            for (Annotation annotation : constructor.getParameterAnnotations()[parameterIndex]) {
                if (annotation.annotationType() == Mock.class) {
                    Class<?> mockClass = constructor.getParameterTypes()[parameterIndex];
                    String mockName = defaultIfEmpty(((Mock) annotation).value(),
                            uncapitalize(mockClass.getSimpleName()));
                    mockBinders.add(new SimpleMockBinder(mockClass, mockName));
                }//from  w  w w  . ja  va2  s  .  c om
            }
        }
        return mockBinders;
    } else {
        return Collections.emptyList();
    }
}

From source file:mangotiger.poker.channel.EventMatcherImpl.java

public static Type[] args(final Constructor<?> constructor) {
    return constructor.isAnnotationPresent(Parameters.class)
            ? constructor.getAnnotation(Parameters.class).value()
            : ZERO_TYPES;//w w w  .  java2 s  .  c  om
}

From source file:org.grouplens.grapht.util.Types.java

/**
 * Return true if the type is not abstract and not an interface, and has
 * a constructor annotated with {@link Inject} or its only constructor
 * is the default constructor.//  w  ww. ja v a2  s .c  o  m
 * 
 * @param type A class type
 * @return True if the class type is instantiable
 */
public static boolean isInstantiable(Class<?> type) {
    if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
        // first check for a constructor annotated with @Inject, 
        //  - this doesn't care how many we'll let the injector complain
        //    if there are more than one
        for (Constructor<?> c : type.getDeclaredConstructors()) {
            if (c.getAnnotation(Inject.class) != null) {
                return true;
            }
        }

        // check if we only have the public default constructor
        if (type.getConstructors().length == 1 && type.getConstructors()[0].getParameterTypes().length == 0) {
            return true;
        }
    }

    // no constructor available
    return false;
}

From source file:org.opendaylight.controller.cluster.datastore.DatastoreContextIntrospector.java

/**
 * Finds the appropriate constructor for the specified type that we will use to construct
 * instances.//from   w w  w .  ja  va 2 s  .c  o  m
 */
private static void processPropertyType(Class<?> propertyType) throws Exception {
    Class<?> wrappedType = Primitives.wrap(propertyType);
    if (constructors.containsKey(wrappedType)) {
        return;
    }

    // If the type is a primitive (or String type), we look for the constructor that takes a
    // single String argument, which, for primitives, validates and converts from a String
    // representation which is the form we get on ingress.
    if (propertyType.isPrimitive() || Primitives.isWrapperType(propertyType)
            || propertyType.equals(String.class)) {
        constructors.put(wrappedType, propertyType.getConstructor(String.class));
    } else {
        // This must be a yang-defined type. We need to find the constructor that takes a
        // primitive as the only argument. This will be used to construct instances to perform
        // validation (eg range checking). The yang-generated types have a couple single-argument
        // constructors but the one we want has the bean ConstructorProperties annotation.
        for (Constructor<?> ctor : propertyType.getConstructors()) {
            ConstructorProperties ctorPropsAnnotation = ctor.getAnnotation(ConstructorProperties.class);
            if (ctor.getParameterTypes().length == 1 && ctorPropsAnnotation != null) {
                findYangTypeGetter(propertyType, ctorPropsAnnotation.value()[0]);
                constructors.put(propertyType, ctor);
                break;
            }
        }
    }
}

From source file:co.jirm.mapper.definition.SqlParameterDefinition.java

private static Map<String, SqlParameterDefinition> getSqlBeanParametersFromConstructorProperties(
        Constructor<?> c, SqlObjectConfig config) {
    Map<String, SqlParameterDefinition> parameters = new LinkedHashMap<String, SqlParameterDefinition>();

    String[] constructorProperties = c.getAnnotation(ConstructorProperties.class).value();
    Class<?>[] pts = c.getParameterTypes();

    for (int i = 0; i < pts.length; i++) {
        parameters.put(constructorProperties[i],
                parameterDef(config, c.getDeclaringClass(), constructorProperties[i], pts[i], i));
    }//www.jav a  2 s  .  co m

    return parameters;
}

From source file:com.zc.util.refelect.Reflector.java

/**
 * ?constructorannotationClass//from   w w  w  . ja  va  2 s.c  om
 *
 * @param constructor
 *            constructor
 * @param annotationClass
 *            annotationClass
 *
 * @return {@link java.lang.annotation.Annotation}
 */
public static <T extends Annotation> T getAnnotation(Constructor constructor, Class annotationClass) {

    constructor.setAccessible(true);

    if (constructor.isAnnotationPresent(annotationClass)) {
        return (T) constructor.getAnnotation(annotationClass);
    }

    return null;
}

From source file:org.jenkinsci.plugins.workflow.structs.DescribableHelper.java

@SuppressWarnings("unchecked")
private static <T> Constructor<T> findConstructor(Class<? extends T> clazz, int length) {
    try { // may work without this, but only if the JVM happens to return the right overload first
        if (clazz == ParametersDefinitionProperty.class && length == 1) { // TODO pending core fix
            return (Constructor<T>) ParametersDefinitionProperty.class.getConstructor(List.class);
        }// www. j a  va  2s  . c o m
    } catch (NoSuchMethodException x) {
        throw new AssertionError(x);
    }
    Constructor<T>[] ctrs = (Constructor<T>[]) clazz.getConstructors();
    for (Constructor<T> c : ctrs) {
        if (c.getAnnotation(DataBoundConstructor.class) != null) {
            if (c.getParameterTypes().length != length) {
                throw new IllegalArgumentException(c
                        + " has @DataBoundConstructor but it doesn't match with your .stapler file. Try clean rebuild");
            }
            return c;
        }
    }
    for (Constructor<T> c : ctrs) {
        if (c.getParameterTypes().length == length) {
            return c;
        }
    }
    throw new IllegalArgumentException(clazz + " does not have a constructor with " + length + " arguments");
}

From source file:com.github.dactiv.common.utils.ReflectionUtils.java

/**
 * ?constructorannotationClass//w ww  .  j av  a 2 s .com
 * 
 * @param constructor
 *            constructor
 * @param annotationClass
 *            annotationClass
 * 
 * @return {@link Annotation}
 */
public static <T extends Annotation> T getAnnotation(Constructor constructor, Class annotationClass) {

    Assert.notNull(constructor, "constructor?");
    Assert.notNull(annotationClass, "annotationClass?");

    constructor.setAccessible(true);

    if (constructor.isAnnotationPresent(annotationClass)) {
        return (T) constructor.getAnnotation(annotationClass);
    }

    return null;
}

From source file:org.lenskit.eval.traintest.metrics.MetricLoaderHelper.java

@Nullable
public <T> T createMetric(Class<T> type, JsonNode node) {
    String typeName = getMetricTypeName(node);
    if (typeName == null) {
        return null;
    }//from  w  ww  .  ja v  a  2s.  co m
    if (!node.isObject()) {
        node = JsonNodeFactory.instance.objectNode().set("type", node);
    }

    Class<?> metric = findClass(typeName);
    if (metric == null) {
        logger.warn("could not find metric {} for ", typeName, type);
        return null;
    }
    for (Constructor<?> ctor : metric.getConstructors()) {
        if (ctor.getAnnotation(JsonCreator.class) != null) {
            return type.cast(SpecUtils.createMapper().convertValue(node, metric));
        }
    }

    // ok, just instantiate
    try {
        return type.cast(metric.newInstance());
    } catch (InstantiationException | IllegalAccessException e) {
        throw new RuntimeException("Cannot instantiate " + metric, e);
    }
}