Example usage for java.lang Class getDeclaringClass

List of usage examples for java.lang Class getDeclaringClass

Introduction

In this page you can find the example usage for java.lang Class getDeclaringClass.

Prototype

@CallerSensitive
public Class<?> getDeclaringClass() throws SecurityException 

Source Link

Document

If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class in which it was declared.

Usage

From source file:com.quirklabs.authorise.ProxyAwareLocalVariableTableParameterNameDiscoverer.java

/**
 * Obtain a (cached) ClassReader for the given class.
 *//*ww w  . j  a  v a  2 s.c om*/
private ClassReader getClassReader(Class clazz) throws IOException {
    synchronized (this.classReaderCache) {
        ClassReader classReader = (ClassReader) this.classReaderCache.get(clazz.getDeclaringClass());
        if (classReader == null) {
            InputStream is = clazz.getResourceAsStream(ClassUtils.getClassFileName(clazz));
            if (is == null) {
                throw new FileNotFoundException("Class file for class [" + clazz.getName() + "] not found");
            }
            try {
                classReader = new ClassReader(is);
                this.classReaderCache.put(clazz.getDeclaringClass(), classReader);
            } finally {
                is.close();
            }
        }
        return classReader;
    }
}

From source file:org.spring.guice.module.GuiceModuleMetadata.java

private boolean visible(Class<?> type) {
    Class<?> cls = type;
    while (cls != null && cls != Object.class) {
        if (!Modifier.isInterface(cls.getModifiers()) && !Modifier.isPublic(cls.getModifiers())
                && !Modifier.isProtected(cls.getModifiers())) {
            return false;
        }//from   w  w w  . jav a  2 s.c o  m
        cls = cls.getDeclaringClass();
    }
    return true;
}

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

public static boolean canUse(Class<?> c) {
    //if (Throwable.class.isAssignableFrom(c))
    //   return false;
    if (Modifier.isPrivate(c.getModifiers()))
        return false;

    if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) {
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();

        if (Properties.hasTargetClassBeenLoaded() && !c.equals(targetClass)) {
            logger.debug("Skipping deprecated class " + c.getName());
            return false;
        }//from   w  w  w .  j  a  v  a2 s  .  c o  m
    }

    if (c.isAnonymousClass()) {
        return false;
    }

    if (c.getName().startsWith("junit"))
        return false;

    if (TestClusterUtils.isEvoSuiteClass(c) && !MockList.isAMockClass(c.getCanonicalName())) {
        return false;
    }

    if (c.getEnclosingClass() != null) {
        if (!canUse(c.getEnclosingClass()))
            return false;
    }

    if (c.getDeclaringClass() != null) {
        if (!canUse(c.getDeclaringClass()))
            return false;
    }

    // If the SUT is not in the default package, then
    // we cannot import classes that are in the default
    // package
    if (!c.isArray() && !c.isPrimitive() && !Properties.CLASS_PREFIX.isEmpty() && !c.getName().contains(".")) {
        return false;
    }

    if (c.getName().contains("EnhancerByMockito")) {
        return false;
    }

    // TODO: This should be unnecessary if Java reflection works...
    // This is inefficient
    if (TestClusterUtils.isAnonymousClass(c.getName())) {
        String message = c + " looks like an anonymous class, ignoring it (although reflection says "
                + c.isAnonymousClass() + ") " + c.getSimpleName();
        LoggingUtils.logWarnAtMostOnce(logger, message);
        return false;
    }

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

    // 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);
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            return true;
        }
    }

    logger.debug("Not public");
    return false;
}

From source file:com.ancientprogramming.fixedformat4j.format.impl.FixedFormatManagerImpl.java

private <T> T createRecordInstance(Class<T> fixedFormatRecordClass) {
    T instance;//  w w  w .java 2s  .  c om
    try {
        Constructor<T> constructor = fixedFormatRecordClass.getDeclaredConstructor();
        instance = constructor.newInstance();
    } catch (NoSuchMethodException e) {
        //If the class is a possible inner class do some more work
        Class declaringClass = fixedFormatRecordClass.getDeclaringClass();
        if (declaringClass != null) {
            try {
                Object declaringClassInstance;
                try {
                    Constructor declaringClassConstructor = declaringClass.getDeclaredConstructor();
                    declaringClassInstance = declaringClassConstructor.newInstance();
                } catch (NoSuchMethodException dex) {
                    throw new FixedFormatException(format(
                            "Trying to create instance of innerclass %s, but the declaring class %s is missing a default constructor which is nessesary to be loaded through %s",
                            fixedFormatRecordClass.getName(), declaringClass.getName(), getClass().getName()));
                } catch (Exception de) {
                    throw new FixedFormatException(format(
                            "unable to create instance of declaring class %s, which is needed to instansiate %s",
                            declaringClass.getName(), fixedFormatRecordClass.getName()), e);
                }
                Constructor<T> constructor = fixedFormatRecordClass.getDeclaredConstructor(declaringClass);
                instance = constructor.newInstance(declaringClassInstance);
            } catch (FixedFormatException ex) {
                throw ex;
            } catch (NoSuchMethodException ex) {
                throw new FixedFormatException(
                        format("%s is missing a default constructor which is nessesary to be loaded through %s",
                                fixedFormatRecordClass.getName(), getClass().getName()));
            } catch (Exception ex) {
                throw new FixedFormatException(
                        format("unable to create instance of %s", fixedFormatRecordClass.getName()), e);
            }
        } else {
            throw new FixedFormatException(
                    format("%s is missing a default constructor which is nessesary to be loaded through %s",
                            fixedFormatRecordClass.getName(), getClass().getName()));
        }
    } catch (Exception e) {
        throw new FixedFormatException(
                format("unable to create instance of %s", fixedFormatRecordClass.getName()), e);
    }
    return instance;
}

From source file:org.evosuite.utils.generic.GenericClass.java

protected static Type addTypeParameters(Class<?> clazz) {
    if (clazz.isArray()) {
        return GenericArrayTypeImpl.createArrayType(addTypeParameters(clazz.getComponentType()));
    } else if (isMissingTypeParameters(clazz)) {
        TypeVariable<?>[] vars = clazz.getTypeParameters();
        // Type[] arguments = new Type[vars.length];
        // Arrays.fill(arguments, UNBOUND_WILDCARD);
        Type owner = clazz.getDeclaringClass() == null ? null : addTypeParameters(clazz.getDeclaringClass());
        return new ParameterizedTypeImpl(clazz, vars, owner);
    } else {//from ww w .j a v  a  2  s  . c o m
        return clazz;
    }
}

From source file:com.odoo.core.orm.OModel.java

private boolean compatibleField(Field field) {
    if (mOdooVersion != null) {
        Annotation[] annotations = field.getDeclaredAnnotations();
        if (annotations.length > 0) {
            int version = 0;
            for (Annotation annotation : annotations) {
                // Check for odoo api annotation
                Class<? extends Annotation> type = annotation.annotationType();
                if (type.getDeclaringClass().isAssignableFrom(Odoo.api.class)) {
                    switch (mOdooVersion.getVersion_number()) {
                    case 9:
                        if (type.isAssignableFrom(Odoo.api.v9alpha.class)) {
                            version++;/*  w  ww  .java 2s  .c o  m*/
                        }
                        break;
                    case 8:
                        if (type.isAssignableFrom(Odoo.api.v8.class)) {
                            version++;
                        }
                        break;
                    case 7:
                        if (type.isAssignableFrom(Odoo.api.v7.class)) {
                            version++;
                        }
                        break;
                    }
                }
                // Check for functional annotation
                if (type.isAssignableFrom(Odoo.Functional.class) || type.isAssignableFrom(Odoo.onChange.class)
                        || type.isAssignableFrom(Odoo.hasDomainFilter.class)) {
                    version++;
                }
            }
            return (version > 0) ? true : false;
        }
        return true;
    }
    return false;
}

From source file:com.rapidminer.tools.Tools.java

public static File findSourceFile(StackTraceElement e) {
    try {/*w w w . jav  a 2s. c o  m*/
        Class<?> clazz = Class.forName(e.getClassName());
        while (clazz.getDeclaringClass() != null) {
            clazz = clazz.getDeclaringClass();
        }
        String filename = clazz.getName().replace('.', File.separatorChar);
        return FileSystemService.getSourceFile(filename + ".java");
    } catch (Throwable t) {
    }
    String filename = e.getClassName().replace('.', File.separatorChar);
    return FileSystemService.getSourceFile(filename + ".java");
}

From source file:com.laxser.blitz.web.impl.module.ModulesBuilderImpl.java

private boolean isCandidate(Class<?> clazz) {
    if (clazz.isAnnotationPresent(Ignored.class)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Ignores bean definition because it's present by @Ignored : " + clazz.getName());
        }//from www . j  a v  a2 s.c  o m
        return false;
    }
    if (!Modifier.isPublic(clazz.getModifiers())) {
        if (logger.isDebugEnabled()) {
            logger.debug("Ignores bean definition because it's not a public class: " + clazz.getName());
        }
        return false;
    }
    if (Modifier.isAbstract(clazz.getModifiers())) {
        if (logger.isDebugEnabled()) {
            logger.debug("Ignores bean definition because it's a abstract class: " + clazz.getName());
        }
        return false;
    }
    if (clazz.getDeclaringClass() != null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Ignores bean definition because it's a inner class: " + clazz.getName());
        }
        return false;
    }
    return true;
}

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

public static boolean canUse(Class<?> c) {
    //if (Throwable.class.isAssignableFrom(c))
    //   return false;
    if (Modifier.isPrivate(c.getModifiers()))
        return false;

    if (!Properties.USE_DEPRECATED && c.isAnnotationPresent(Deprecated.class)) {
        logger.debug("Skipping deprecated class {}", c.getName());
        return false;
    }//www  .java 2  s  . c o  m

    if (c.isAnonymousClass()) {
        return false;
    }

    if (ANONYMOUS_MATCHER1.matcher(c.getName()).matches()) {
        logger.debug("{} looks like an anonymous class, ignoring it", c);
        return false;
    }

    if (ANONYMOUS_MATCHER2.matcher(c.getName()).matches()) {
        logger.debug("{} looks like an anonymous class, ignoring it", c);
        return false;
    }

    if (c.getName().startsWith("junit"))
        return false;

    if (isEvoSuiteClass(c) && !MockList.isAMockClass(c.getCanonicalName())) {
        return false;
    }

    if (c.getEnclosingClass() != null) {
        if (!canUse(c.getEnclosingClass()))
            return false;
    }

    if (c.getDeclaringClass() != null) {
        if (!canUse(c.getDeclaringClass()))
            return false;
    }

    // If the SUT is not in the default package, then
    // we cannot import classes that are in the default
    // package
    if (!c.isArray() && !c.isPrimitive() && !Properties.CLASS_PREFIX.isEmpty() && !c.getName().contains(".")) {
        return false;
    }

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

    // 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);
        if (packageName.equals(Properties.CLASS_PREFIX)) {
            return true;
        }
    }

    logger.debug("Not public");
    return false;
}

From source file:org.evosuite.testcase.ImportsTestCodeVisitor.java

/**
 * <p>//  www .jav  a 2s.c om
 * getClassName
 * </p>
 *
 * @param clazz
 *            a {@link Class} object.
 * @return a {@link String} object.
 */
public String getClassName(Class<?> clazz) {
    if (classNames.containsKey(clazz))
        return classNames.get(clazz);

    if (clazz.isArray()) {
        return getClassName(clazz.getComponentType()) + "[]";
    }

    GenericClass c = new GenericClass(clazz);
    String name = c.getSimpleName();
    if (classNames.values().contains(name)) {
        name = clazz.getCanonicalName();
    } else {
        /*
         * If e.g. there is a foo.bar.IllegalStateException with
         * foo.bar being the SUT package, then we need to use the
         * full package name for java.lang.IllegalStateException
         */
        String fullName = Properties.CLASS_PREFIX + "." + name;
        if (!fullName.equals(clazz.getCanonicalName())) {
            try {
                if (ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT())
                        .hasClass(fullName)) {
                    name = clazz.getCanonicalName();
                }
            } catch (IllegalArgumentException e) {
                // If the classpath is not correct, then we just don't check
                // because that cannot happen in regular EvoSuite use, only
                // from test cases
            }
        }
    }
    // Ensure outer classes are imported as well
    Class<?> outerClass = clazz.getEnclosingClass();
    if (outerClass != null) {
        String enclosingName = getClassName(outerClass);
        String simpleOuterName = outerClass.getSimpleName();
        if (simpleOuterName.equals(enclosingName)) {
            name = enclosingName + name.substring(simpleOuterName.length());
        } else {
            name = enclosingName + name.substring(name.lastIndexOf(simpleOuterName) + simpleOuterName.length());
        }
    }

    Class<?> declaringClass = clazz.getDeclaringClass();
    if (declaringClass != null) {
        getClassName(declaringClass);
    }

    // We can't use "Test" because of JUnit
    if (name.equals("Test")) {
        name = clazz.getCanonicalName();
    }
    classNames.put(clazz, name);

    return name;
}