List of usage examples for java.lang Class getSuperclass
@HotSpotIntrinsicCandidate public native Class<? super T> getSuperclass();
From source file:fi.foyt.foursquare.api.JSONFieldParser.java
/** * Returns field of class// w w w .ja va2s . c o m * * @param entityClass class * @param fieldName field * @return Field */ private static Field getField(Class<?> entityClass, String fieldName) { try { Field field = entityClass.getDeclaredField(fieldName); field.setAccessible(true); return field; } catch (SecurityException e) { return null; } catch (NoSuchFieldException e) { Class<?> superClass = entityClass.getSuperclass(); if (superClass.equals(Object.class)) { return null; } else { return getField(superClass, fieldName); } } }
From source file:com.github.magicsky.sya.checkers.TestSourceReader.java
/** * Returns an array of StringBuilder objects for each comment section found preceding the named * test in the source code./*ww w. j a v a 2 s.c om*/ * * @param srcRoot the directory inside the bundle containing the packages * @param clazz the name of the class containing the test * @param testName the name of the test * @param numSections the number of comment sections preceding the named test to return. * Pass zero to get all available sections. * @return an array of StringBuilder objects for each comment section found preceding the named * test in the source code. * @throws IOException */ public static StringBuilder[] getContentsForTest(String srcRoot, Class clazz, final String testName, int numSections) throws IOException { // Walk up the class inheritance chain until we find the test method. try { while (clazz.getMethod(testName).getDeclaringClass() != clazz) { clazz = clazz.getSuperclass(); } } catch (SecurityException e) { Assert.fail(e.getMessage()); } catch (NoSuchMethodException e) { Assert.fail(e.getMessage()); } while (true) { // Find and open the .java file for the class clazz. String fqn = clazz.getName().replace('.', '/'); fqn = fqn.indexOf("$") == -1 ? fqn : fqn.substring(0, fqn.indexOf("$")); String classFile = fqn + ".java"; InputStream in; Class superclass = clazz.getSuperclass(); try { in = FileUtils.openInputStream(new File(srcRoot + '/' + classFile)); } catch (IOException e) { if (superclass == null || !superclass.getPackage().equals(clazz.getPackage())) { throw e; } clazz = superclass; continue; } BufferedReader br = new BufferedReader(new InputStreamReader(in)); try { // Read the java file collecting comments until we encounter the test method. List<StringBuilder> contents = new ArrayList<StringBuilder>(); StringBuilder content = new StringBuilder(); for (String line = br.readLine(); line != null; line = br.readLine()) { line = line.replaceFirst("^\\s*", ""); // Replace leading whitespace, preserve trailing if (line.startsWith("//")) { content.append(line.substring(2) + "\n"); } else { if (!line.startsWith("@") && content.length() > 0) { contents.add(content); if (numSections > 0 && contents.size() == numSections + 1) contents.remove(0); content = new StringBuilder(); } if (line.length() > 0 && !contents.isEmpty()) { int idx = line.indexOf(testName); if (idx != -1 && !Character.isJavaIdentifierPart(line.charAt(idx + testName.length()))) { return contents.toArray(new StringBuilder[contents.size()]); } if (!line.startsWith("@")) { contents.clear(); } } } } } finally { br.close(); } if (superclass == null || !superclass.getPackage().equals(clazz.getPackage())) { throw new IOException("Test data not found for " + clazz.getName() + "." + testName); } clazz = superclass; } }
From source file:com.zc.util.refelect.Reflector.java
/** * ?Classcglib AOPCGLIBClass?Class/*w w w.j a v a 2 s . com*/ * * @param targetClass * * @return Class */ public static Class<?> getTargetClass(Class<?> targetClass) { Class clazz = targetClass; if (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR)) { Class<?> superClass = clazz.getSuperclass(); if (superClass != null && !Object.class.equals(superClass)) { return superClass; } } return clazz; }
From source file:ReflectUtil.java
/** * Returns an array of Type objects representing the actual type arguments * to targetType used by clazz.//from ww w . j a v a 2 s. c o m * * @param clazz the implementing class (or subclass) * @param targetType the implemented generic class or interface * @return an array of Type objects or null */ public static Type[] getActualTypeArguments(Class<?> clazz, Class<?> targetType) { Set<Class<?>> classes = new HashSet<Class<?>>(); classes.add(clazz); if (targetType.isInterface()) classes.addAll(getImplementedInterfaces(clazz)); Class<?> superClass = clazz.getSuperclass(); while (superClass != null) { classes.add(superClass); superClass = superClass.getSuperclass(); } for (Class<?> search : classes) { for (Type type : (targetType.isInterface() ? search.getGenericInterfaces() : new Type[] { search.getGenericSuperclass() })) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (targetType.equals(parameterizedType.getRawType())) return parameterizedType.getActualTypeArguments(); } } } return null; }
From source file:com.medsphere.fileman.FMRecord.java
private static void accumulateNumericMapping(Map<String, String> numberMap, Class<? extends FMRecord> domainClass) { if (domainClass != null && domainClass.getSuperclass() != null) { Class<? extends FMRecord> superClass = (Class<? extends FMRecord>) domainClass.getSuperclass(); accumulateNumericMapping(numberMap, superClass); }//www.ja va 2s . c o m for (AnnotatedElement e : domainClass.getDeclaredFields()) { FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class); if (annote != null) { numberMap.put(annote.name(), annote.number()); } } for (AnnotatedElement e : domainClass.getDeclaredMethods()) { FMAnnotateFieldInfo annote = e.getAnnotation(FMAnnotateFieldInfo.class); if (annote != null) { numberMap.put(annote.name(), annote.number()); } } }
From source file:eu.leads.processor.planner.ClassUtil.java
private static boolean isMatch(Class targetClass, Class loadedClass) { if (targetClass.equals(loadedClass)) { return true; }/*from w w w. java2s .co m*/ Class[] classInterfaces = loadedClass.getInterfaces(); if (classInterfaces != null) { for (Class eachInterfaceClass : classInterfaces) { if (eachInterfaceClass.equals(targetClass)) { return true; } if (isMatch(targetClass, eachInterfaceClass)) { return true; } } } Class superClass = loadedClass.getSuperclass(); if (superClass != null) { if (isMatch(targetClass, superClass)) { return true; } } return false; }
From source file:Main.java
public static Method getColumnGetMethod(Class<?> entityType, Field field) { String fieldName = field.getName(); Method getMethod = null;// w w w .j ava2 s .c o m if (field.getType() == boolean.class) { getMethod = getBooleanColumnGetMethod(entityType, fieldName); } if (getMethod == null) { String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); try { getMethod = entityType.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { //Logger.d(methodName + " not exist"); } } if (getMethod == null && !Object.class.equals(entityType.getSuperclass())) { return getColumnGetMethod(entityType.getSuperclass(), field); } return getMethod; }
From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java
/** * Find the first {@link Class} in the inheritance hierarchy of the specified {@code clazz} * (including the specified {@code clazz} itself) which declares an annotation for the * specified {@code annotationType}, or {@code null} if not found. If the supplied * {@code clazz} is {@code null}, {@code null} will be returned. * <p>If the supplied {@code clazz} is an interface, only the interface itself will be checked; * the inheritance hierarchy for interfaces will not be traversed. * <p>The standard {@link Class} API does not provide a mechanism for determining which class * in an inheritance hierarchy actually declares an {@link java.lang.annotation.Annotation}, so we need to handle * this explicitly./*from w w w . j av a 2 s . c o m*/ * @param annotationType the Class object corresponding to the annotation type * @param clazz the Class object corresponding to the class on which to check for the annotation, * or {@code null} * @return the first {@link Class} in the inheritance hierarchy of the specified {@code clazz} * which declares an annotation for the specified {@code annotationType}, or {@code null} * if not found * @see Class#isAnnotationPresent(Class) * @see Class#getDeclaredAnnotations() * @see #findAnnotationDeclaringClassForTypes(java.util.List, Class) * @see #isAnnotationDeclaredLocally(Class, Class) */ public static Class<?> findAnnotationDeclaringClass(Class<? extends Annotation> annotationType, Class<?> clazz) { if (clazz == null || clazz.equals(Object.class)) { return null; } return (isAnnotationDeclaredLocally(annotationType, clazz)) ? clazz : findAnnotationDeclaringClass(annotationType, clazz.getSuperclass()); }
From source file:com.github.valdr.thirdparty.spring.AnnotationUtils.java
/** * Get a single {@link java.lang.annotation.Annotation} of {@code annotationType} from the supplied {@link java.lang.reflect.Method}, * traversing its super methods if no annotation can be found on the given method itself. * <p>Annotations on methods are not inherited by default, so we need to handle this explicitly. * @param method the method to look for annotations on * @param annotationType the annotation class to look for * @return the annotation found, or {@code null} if none found *//*from w w w . j ava 2 s . co m*/ public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) { A annotation = getAnnotation(method, annotationType); Class<?> clazz = method.getDeclaringClass(); if (annotation == null) { annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces()); } while (annotation == null) { clazz = clazz.getSuperclass(); if (clazz == null || clazz.equals(Object.class)) { break; } try { Method equivalentMethod = clazz.getDeclaredMethod(method.getName(), method.getParameterTypes()); annotation = getAnnotation(equivalentMethod, annotationType); } catch (NoSuchMethodException ex) { // No equivalent method found } if (annotation == null) { annotation = searchOnInterfaces(method, annotationType, clazz.getInterfaces()); } } return annotation; }
From source file:ch.aonyx.broker.ib.api.util.AnnotationUtils.java
/** * Find the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code> (including the * specified <code>clazz</code> itself) which declares an annotation for the specified <code>annotationType</code>, * or <code>null</code> if not found. If the supplied <code>clazz</code> is <code>null</code>, <code>null</code> * will be returned./*from w w w . j a va 2 s . co m*/ * <p> * If the supplied <code>clazz</code> is an interface, only the interface itself will be checked; the inheritance * hierarchy for interfaces will not be traversed. * <p> * The standard {@link Class} API does not provide a mechanism for determining which class in an inheritance * hierarchy actually declares an {@link Annotation}, so we need to handle this explicitly. * * @param annotationType * the Class object corresponding to the annotation type * @param clazz * the Class object corresponding to the class on which to check for the annotation, or <code>null</code> * @return the first {@link Class} in the inheritance hierarchy of the specified <code>clazz</code> which declares * an annotation for the specified <code>annotationType</code>, or <code>null</code> if not found * @see Class#isAnnotationPresent(Class) * @see Class#getDeclaredAnnotations() */ public static Class<?> findAnnotationDeclaringClass(final Class<? extends Annotation> annotationType, final Class<?> clazz) { Validate.notNull(annotationType, "Annotation type must not be null"); if ((clazz == null) || clazz.equals(Object.class)) { return null; } return (isAnnotationDeclaredLocally(annotationType, clazz)) ? clazz : findAnnotationDeclaringClass(annotationType, clazz.getSuperclass()); }