Java Class.getDeclaringClass Examples

Java Class.getDeclaringClass - 28 examples found. These are the top rated real world Java examples of Class.getDeclaringClass extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
 private Object findAncestor(Class<?> clazz) {
   // First check enclosing classes
   Class<?> c = clazz.getDeclaringClass();
   while (c != null) {
     if (c.isAnnotationPresent(Gwtc.class)) {
       return c;
     }
     c = c.getDeclaringClass();
   }
   Package p = clazz.getPackage();
   if (p.getAnnotation(Gwtc.class) != null) {
     return p;
   }
   Object o = findAncestor(p);
   if (o == null) {
     c = clazz.getSuperclass();
     while (c != null) {
       if (c.isAnnotationPresent(Gwtc.class)) {
         return c;
       }
       c = c.getSuperclass();
     }
   }
   return o;
 }
  @Test
  public void jaxbClassesTest() throws Exception {
    Assume.assumeTrue(TestType.JAXB.equals(getType()));

    Set<Class<?>> jaxbClasses = reflections.getTypesAnnotatedWith(XmlRootElement.class);

    assertTrue("Not enough classes found! [" + jaxbClasses.size() + "]", jaxbClasses.size() > 20);

    String className = null;
    try {
      for (Class<?> jaxbClass : jaxbClasses) {
        if (jaxbClass.getDeclaringClass() != null
            && jaxbClass.getDeclaringClass().getSimpleName().endsWith("Test")) {
          continue;
        }
        className = jaxbClass.getName();
        Constructor<?> construct = jaxbClass.getConstructor(new Class[] {});
        Object jaxbInst = construct.newInstance(new Object[] {});
        testRoundTrip(jaxbInst);
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(className + ": " + e.getClass().getSimpleName() + " [" + e.getMessage() + "]");
    }
  }
 /** Is c1 an inner class of c2? */
 protected boolean isInnerClass(Class c1, final Class c2) {
   Class c = c1.getDeclaringClass();
   if (c == null) {
     try {
       final Field f = c1.getField("declaring$Class$Reference$0");
       c = (Class) f.get(null);
     } catch (final Exception e) {
     }
   }
   c1 = c;
   while (c != null) {
     if (c == c2) {
       return true;
     }
     c = c.getDeclaringClass();
     if (c == null) {
       try {
         final Field f = c1.getField("declaring$Class$Reference$0");
         c = (Class) f.get(null);
       } catch (final Exception e) {
       }
     }
     c1 = c;
   }
   return false;
 }
 public static <T> T newInnerInstance(Object instance, Class<T> innerClass) throws Exception {
   if (innerClass.getDeclaringClass() != null
       && (innerClass.getModifiers() & Modifier.STATIC) == 0) {
     Constructor<T> c =
         innerClass.getDeclaredConstructor(new Class[] {innerClass.getDeclaringClass()});
     return c.newInstance(new Object[] {instance});
   } else {
     return innerClass.newInstance();
   }
 }
Example #5
0
 public void addEnclosingClasses(Class<?> c) {
   c = c.getDeclaringClass();
   while (c != null) {
     Gwtc gwtc = c.getAnnotation(Gwtc.class);
     if (gwtc != null && addClass(c)) {
       addGwtcClass(gwtc, c);
     }
     c = c.getDeclaringClass();
   }
 }
 @Test
 public void testDeclaringType() throws Exception {
   for (Class<?> type : standardTypes) {
     assertThat(
         describe(type).getDeclaringType(),
         type.getDeclaringClass() == null
             ? nullValue(TypeDescription.class)
             : is((TypeDescription) new TypeDescription.ForLoadedType(type.getDeclaringClass())));
   }
 }