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.shigengyu.hyperion.cache.WorkflowStateCacheLoader.java

private static final StateOwner getStateOwner(Class<? extends WorkflowState> workflowStateClass) {

    // Try get annotation on class
    StateOwner stateOwner = workflowStateClass.getAnnotation(StateOwner.class);

    // Try get annotation on declaring class
    Class<?> declaringClass = workflowStateClass;
    while (stateOwner == null && (declaringClass = declaringClass.getDeclaringClass()) != null) {
        stateOwner = declaringClass.getAnnotation(StateOwner.class);
    }/*from w  ww. jav a 2  s.c om*/

    // Try get annotation on package
    if (stateOwner == null) {
        stateOwner = workflowStateClass.getPackage().getAnnotation(StateOwner.class);
    }

    return stateOwner;
}

From source file:Main.java

public static void printMemberClasses(final Class dataType) {
    final Class[] nestedClasses = dataType.getClasses();
    final Class[] declaredNestedClasses = dataType.getDeclaredClasses();
    final Class[] nestedInterfaces = dataType.getInterfaces();
    final Class declaringClass = dataType.getDeclaringClass();

    System.out.println("Member Class infor for: " + dataType.getName());
    System.out.println("Nested Classes: " + Arrays.asList(nestedClasses));
    System.out.println("Declared Nested Classes: " + Arrays.asList(declaredNestedClasses));
    System.out.println("Interfaces: " + Arrays.asList(nestedInterfaces));
    System.out.println("Declaring Class: " + declaringClass);
}

From source file:Main.java

public static String canonicalName(Class clazz) {
    StringBuilder name = new StringBuilder();

    if (clazz.isArray()) {
        name.append(canonicalName(clazz.getComponentType()));
        name.append("[]");
    } else if (clazz.getDeclaringClass() == null) {
        name.append(clazz.getName());/*from www .  j a v a  2s. com*/
    } else {
        name.append(canonicalName(clazz.getDeclaringClass()));
        name.append(".");
        name.append(clazz.getName().substring(clazz.getDeclaringClass().getName().length() + 1));
    }

    return name.toString();
}

From source file:net.sf.beanlib.hibernate3.Hibernate3SequenceGenerator.java

/** Returns the next sequence id from the specified sequence and session. */
public static long nextval(final String sequenceName, final Session session) {
    Object target = session;/*  w ww.  java  2 s .c om*/

    if (Proxy.isProxyClass(session.getClass())) {
        // Dig out the underlying session.
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(session);

        if (invocationHandler instanceof DtoCentricCloseSuppressingInvocationHandler) {
            // This is faster for we don't need to use reflection.
            DtoCentricCloseSuppressingInvocationHandler dch = (DtoCentricCloseSuppressingInvocationHandler) invocationHandler;
            target = dch.getTarget();
        } else {
            Class<?> invocationHandlerClass = invocationHandler.getClass();
            Class<?> invocationHandlerDeclaringClass = invocationHandlerClass.getDeclaringClass();

            if (invocationHandlerDeclaringClass == HibernateTemplate.class) {
                String className = invocationHandlerClass.getName();

                if (className.endsWith("CloseSuppressingInvocationHandler")) {
                    // Assume this is the private class org.springframework.orm.hibernate3.HibernateTempate$CloseSuppressingInvocationHandler
                    // Dig out the private target.  
                    // I know this is bad, but there doesn't seem to be a better way.  Oh well.
                    try {
                        Field f = invocationHandlerClass.getDeclaredField("target");
                        f.setAccessible(true);
                        target = f.get(invocationHandler);
                    } catch (SecurityException e) {
                        throw new RuntimeException(e);
                    } catch (NoSuchFieldException e) {
                        throw new RuntimeException(e);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }

                }
            }

        }
    }
    SessionImpl sessionImpl;

    if (target instanceof SessionImpl)
        sessionImpl = (SessionImpl) target;
    else
        throw new IllegalStateException("Not yet know how to handle the given session!");
    IdentifierGenerator idGenerator = createIdentifierGenerator(sequenceName, session);
    Serializable id = idGenerator.generate(sessionImpl, null);
    return (Long) id;
}

From source file:org.assertj.assertions.generator.util.ClassUtil.java

private static boolean isNotNestedClass(Class<?> clazz) {
    return clazz.getDeclaringClass() == null;
}

From source file:org.deeplearning4j.nn.conf.ComputationGraphConfiguration.java

/**
 * Create a computation graph configuration from json
 *
 * @param json the neural net configuration from json
 * @return {@link org.deeplearning4j.nn.conf.ComputationGraphConfiguration}
 *///from w  ww  .j a  v a 2 s.co  m
public static ComputationGraphConfiguration fromJson(String json) {
    //As per MultiLayerConfiguration.fromJson()
    ObjectMapper mapper = NeuralNetConfiguration.mapper();
    try {
        return mapper.readValue(json, ComputationGraphConfiguration.class);
    } catch (IOException e) {
        //No op - try again after adding new subtypes
    }

    //Try: programmatically registering JSON subtypes for GraphVertex classes. This allows users to to add custom GraphVertex
    // implementations without needing to manually register subtypes
    //First: get all registered subtypes
    AnnotatedClass ac = AnnotatedClass.construct(GraphVertex.class,
            mapper.getSerializationConfig().getAnnotationIntrospector(), null);
    Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac,
            mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector());
    Set<Class<?>> registeredSubtypes = new HashSet<>();
    for (NamedType nt : types) {
        registeredSubtypes.add(nt.getType());
    }

    //Second: get all subtypes of GraphVertex using reflection
    Reflections reflections = new Reflections();
    Set<Class<? extends GraphVertex>> subTypes = reflections.getSubTypesOf(GraphVertex.class);

    //Third: register all subtypes that are not already registered
    List<NamedType> toRegister = new ArrayList<>();
    for (Class<? extends GraphVertex> c : subTypes) {
        if (!registeredSubtypes.contains(c)) {
            String name;
            if (ClassUtils.isInnerClass(c)) {
                Class<?> c2 = c.getDeclaringClass();
                name = c2.getSimpleName() + "$" + c.getSimpleName();
            } else {
                name = c.getSimpleName();
            }
            toRegister.add(new NamedType(c, name));
        }
    }
    mapper = NeuralNetConfiguration.reinitMapperWithSubtypes(toRegister);

    try {
        return mapper.readValue(json, ComputationGraphConfiguration.class);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.jasper.compiler.JspUtil.java

/**
 * Compute the canonical name from a Class instance.  Note that a
 * simple replacment of '$' with '.' of a binary name would not work,
 * as '$' is a legal Java Identifier character.
 * @param c A instance of java.lang.Class
 * @return  The canonical name of c.//from   w  w w.  ja  va 2  s . c om
 */
public static String getCanonicalName(Class c) {

    String binaryName = c.getName();
    c = c.getDeclaringClass();

    if (c == null) {
        return binaryName;
    }

    StringBuffer buf = new StringBuffer(binaryName);
    do {
        buf.setCharAt(c.getName().length(), '.');
        c = c.getDeclaringClass();
    } while (c != null);

    return buf.toString();
}

From source file:org.thelq.stackexchange.api.model.types.EntryFormatTest.java

@DataProvider
public Object[][] enumsInFieldsAreNotFromAnotherClassDataProvider() throws IOException {
    return TestUtils.toTestParameters(getEntriesFields(new Predicate<Field>() {
        public boolean apply(Field input) {
            Class type = input.getType();
            return type.isEnum() && type.getDeclaringClass() != null;
        }/*from ww  w  .j  av a  2  s.c  om*/
    }));
}

From source file:org.apache.struts2.jasper.compiler.JspUtil.java

/**
 * Compute the canonical name from a Class instance.  Note that a
 * simple replacement of '$' with '.' of a binary name would not work,
 * as '$' is a legal Java Identifier character.
 *
 * @param c A instance of java.lang.Class
 * @return The canonical name of c.//from   w ww .  j a v  a 2s.co m
 */
public static String getCanonicalName(Class c) {

    String binaryName = c.getName();
    c = c.getDeclaringClass();

    if (c == null) {
        return binaryName;
    }

    StringBuilder buf = new StringBuilder(binaryName);
    do {
        buf.setCharAt(c.getName().length(), '.');
        c = c.getDeclaringClass();
    } while (c != null);

    return buf.toString();
}

From source file:nl.luminis.test.util.annotations.HierarchyDiscovery.java

private Type resolveType(Class<?> clazz) {
    if (clazz.getTypeParameters().length > 0) {
        TypeVariable<?>[] actualTypeParameters = clazz.getTypeParameters();

        return TypeUtils.parameterizeWithOwner(clazz.getDeclaringClass(), clazz, actualTypeParameters);
    } else {//w  w  w  .ja  v a  2 s .c o  m
        return clazz;
    }
}