Example usage for java.lang.reflect Constructor isAnnotationPresent

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

Introduction

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

Prototype

@Override
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) 

Source Link

Usage

From source file:gobblin.runtime.cli.ConstructorAndPublicMethodsCliObjectFactory.java

private boolean canUseConstructor(Constructor<?> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())) {
        return false;
    }/* w ww.  j  a  va 2  s.c om*/
    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;
}

From source file:gobblin.runtime.cli.ConstructorAndPublicMethodsGobblinCliFactory.java

private boolean canUseConstructor(Constructor<?> constructor) {
    if (!Modifier.isPublic(constructor.getModifiers())) {
        return false;
    }//  ww  w. j a va  2s.c o m
    if (!constructor.isAnnotationPresent(EmbeddedGobblinCliSupport.class)) {
        return false;
    }
    for (Class<?> param : constructor.getParameterTypes()) {
        if (param != String.class) {
            return false;
        }
    }
    return constructor.getParameterTypes().length == constructor.getAnnotation(EmbeddedGobblinCliSupport.class)
            .argumentNames().length;
}

From source file:org.jdto.impl.AnnotationBeanInspector.java

@Override
Constructor findAppropiateConstructor(Class beanClass) {
    Constructor[] beanConstructors = beanClass.getConstructors();

    for (Constructor constructor : beanConstructors) {
        if (constructor.isAnnotationPresent(DTOConstructor.class)) {
            return constructor;
        }/*from   w  w  w .j  av  a  2  s.  c  om*/
    }

    if (beanConstructors != null && beanConstructors.length > 0) {
        //return the last constructor.
        return beanConstructors[beanConstructors.length - 1];
    } else {
        return null;
    }

}

From source file:com.opengamma.language.external.ExternalFunctionHandler.java

/**
 * Creates a handler wrapper for a given class.
 * //from w  w w. ja v  a  2  s  .  c  om
 * @param clazz the class containing external function methods
 */
public ExternalFunctionHandler(final Class<?> clazz) {
    final Constructor<?>[] constructors = clazz.getConstructors();
    final Method[] methods = clazz.getMethods();
    final ArrayList<PublishedFunction> functions = new ArrayList<PublishedFunction>(
            constructors.length + methods.length);
    // Only need an instance of the class if one or more annotated methods are not static. In this case, the same
    // instance will be re-used for each non-static method. If instantiation fails (e.g. no default constructor), just
    // skip instance methods and log warnings.
    Object sharedInstance = null;
    boolean instantiateFailed = false;
    for (Constructor<?> constructor : constructors) {
        if (!constructor.isAnnotationPresent(ExternalFunction.class)) {
            continue;
        }
        s_logger.debug("Found constructor {}", constructor);
        // If there is a constructor method, can only have static declarations
        instantiateFailed = true;
        functions.add(new ConstructorWrapper(constructor));
    }
    for (Method method : methods) {
        if (!method.isAnnotationPresent(ExternalFunction.class)) {
            continue;
        }
        s_logger.debug("Found method {}", method);
        final Object instance;
        if (Modifier.isStatic(method.getModifiers())) {
            instance = null;
        } else {
            if (instantiateFailed) {
                s_logger.warn("Skipping method {}", method);
                continue;
            } else if (sharedInstance == null) {
                sharedInstance = tryGetInstance(clazz);
                if (sharedInstance == null) {
                    s_logger.warn("Default instantiation failed for {}", clazz);
                    s_logger.warn("Skipping method {}", method);
                    instantiateFailed = true;
                    continue;
                }
            }
            instance = sharedInstance;
        }
        functions.add(new MethodWrapper(method, instance));
    }
    functions.trimToSize();
    _functions = functions;
}

From source file:org.evosuite.setup.TestUsageChecker.java

public static boolean canUse(Constructor<?> c) {

    if (c.isSynthetic()) {
        return false;
    }//from   ww w. j  a  v a 2 s . c o m

    // synthetic constructors are OK
    if (Modifier.isAbstract(c.getDeclaringClass().getModifiers()))
        return false;

    // TODO we could enable some methods from Object, like getClass
    //if (c.getDeclaringClass().equals(java.lang.Object.class))
    //   return false;// handled here to avoid printing reasons

    if (c.getDeclaringClass().equals(java.lang.Thread.class))
        return false;// handled here to avoid printing reasons

    if (c.getDeclaringClass().isAnonymousClass())
        return false;

    if (c.getDeclaringClass().isLocalClass()) {
        logger.debug("Skipping constructor of local class " + c.getName());
        return false;
    }

    if (c.getDeclaringClass().isMemberClass() && !TestUsageChecker.canUse(c.getDeclaringClass()))
        return false;

    if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) {
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (Properties.hasTargetClassBeenLoaded() && !c.getDeclaringClass().equals(targetClass)) {
            logger.debug("Excluding deprecated constructor " + c.getName());
            return false;
        }
    }

    if (isForbiddenNonDeterministicCall(c)) {
        return false;
    }

    if (Modifier.isPublic(c.getModifiers())) {
        TestClusterUtils.makeAccessible(c);
        return true;
    }

    for (java.lang.reflect.Type paramType : c.getGenericParameterTypes()) {
        if (!canUse(paramType))
            return false;
    }

    // If default access rights, then check if this class is in the same package as the target class
    if (!Modifier.isPrivate(c.getModifiers())) {
        //              && !Modifier.isProtected(c.getModifiers())) {
        String packageName = ClassUtils.getPackageName(c.getDeclaringClass());
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            TestClusterUtils.makeAccessible(c);
            return true;
        }
    }

    return false;
}

From source file:com.opensymphony.xwork2.util.finder.DefaultClassFinder.java

public List<Constructor> findAnnotatedConstructors(Class<? extends Annotation> annotation) {
    classesNotLoaded.clear();//from w w  w.ja  va 2  s.co m
    List<ClassInfo> seen = new ArrayList<ClassInfo>();
    List<Constructor> constructors = new ArrayList<Constructor>();
    List<Info> infos = getAnnotationInfos(annotation.getName());
    for (Info info : infos) {
        if (info instanceof MethodInfo && "<init>".equals(info.getName())) {
            MethodInfo methodInfo = (MethodInfo) info;
            ClassInfo classInfo = methodInfo.getDeclaringClass();

            if (seen.contains(classInfo))
                continue;

            seen.add(classInfo);

            try {
                Class clazz = classInfo.get();
                for (Constructor constructor : clazz.getConstructors()) {
                    if (constructor.isAnnotationPresent(annotation)) {
                        constructors.add(constructor);
                    }
                }
            } catch (Throwable e) {
                LOG.error("Error loading class [{}]", classInfo.getName(), e);
                classesNotLoaded.add(classInfo.getName());
            }
        }
    }
    return constructors;
}

From source file:org.apache.struts2.convention.DefaultClassFinder.java

public List<Constructor> findAnnotatedConstructors(Class<? extends Annotation> annotation) {
    classesNotLoaded.clear();/*from   w  w  w.ja  v  a2  s  .c om*/
    List<ClassInfo> seen = new ArrayList<>();
    List<Constructor> constructors = new ArrayList<>();
    List<Info> infos = getAnnotationInfos(annotation.getName());
    for (Info info : infos) {
        if (info instanceof MethodInfo && "<init>".equals(info.getName())) {
            MethodInfo methodInfo = (MethodInfo) info;
            ClassInfo classInfo = methodInfo.getDeclaringClass();

            if (seen.contains(classInfo))
                continue;

            seen.add(classInfo);

            try {
                Class clazz = classInfo.get();
                for (Constructor constructor : clazz.getConstructors()) {
                    if (constructor.isAnnotationPresent(annotation)) {
                        constructors.add(constructor);
                    }
                }
            } catch (Throwable e) {
                LOG.error("Error loading class [{}]", classInfo.getName(), e);
                classesNotLoaded.add(classInfo.getName());
            }
        }
    }
    return constructors;
}

From source file:com.opensymphony.xwork2.util.finder.ClassFinder.java

public List<Constructor> findAnnotatedConstructors(Class<? extends Annotation> annotation) {
    classesNotLoaded.clear();//from   w w w  . ja va  2s  .  c  o m
    List<ClassInfo> seen = new ArrayList<ClassInfo>();
    List<Constructor> constructors = new ArrayList<Constructor>();
    List<Info> infos = getAnnotationInfos(annotation.getName());
    for (Info info : infos) {
        if (info instanceof MethodInfo && "<init>".equals(info.getName())) {
            MethodInfo methodInfo = (MethodInfo) info;
            ClassInfo classInfo = methodInfo.getDeclaringClass();

            if (seen.contains(classInfo))
                continue;

            seen.add(classInfo);

            try {
                Class clazz = classInfo.get();
                for (Constructor constructor : clazz.getConstructors()) {
                    if (constructor.isAnnotationPresent(annotation)) {
                        constructors.add(constructor);
                    }
                }
            } catch (Throwable e) {
                if (LOG.isErrorEnabled())
                    LOG.error("Error loading class [#0]", e, classInfo.getName());
                classesNotLoaded.add(classInfo.getName());
            }
        }
    }
    return constructors;
}