List of usage examples for java.lang Class getSuperclass
@HotSpotIntrinsicCandidate public native Class<? super T> getSuperclass();
From source file:com.github.helenusdriver.commons.lang3.reflect.ReflectionUtils.java
/** * Finds the first class from a given class' hierarchy that is not annotated * with the specified annotation.// ww w.j ava 2 s . c om * * @author paouelle * * @param <T> the type of the class t start searching from * * @param clazz the class from which to search * @param annotationClass the annotation to search for * @return the non-<code>null</code> first class in <code>clazz</code>'s * hierarchy not annotated with the specified annotation * @throws NullPointerException if <code>clazz</code> or * <code>annotationClass</code> is <code>null</code> */ public static <T> Class<? super T> findFirstClassNotAnnotatedWith(Class<T> clazz, Class<? extends Annotation> annotationClass) { org.apache.commons.lang3.Validate.notNull(clazz, "invalid null class"); org.apache.commons.lang3.Validate.notNull(annotationClass, "invalid null annotation class"); Class<? super T> c = clazz; while (c.getDeclaredAnnotationsByType(annotationClass).length > 0) { c = c.getSuperclass(); } return c; }
From source file:com.hbs.common.josn.JSONUtil.java
/** * Recursive method to visit all the interfaces of a class (and its superclasses and super-interfaces) if they * haven't already been visited./*from w w w . ja va2s . co m*/ * * Always visits itself if it hasn't already been visited * * @param thisClass the current class to visit (if not already done so) * @param classesVisited classes already visited * @param visitor this vistor is called for each class/interface encountered * @return true if recursion can continue, false if it should be aborted */ @SuppressWarnings("unchecked") private static boolean visitUniqueInterfaces(Class thisClass, ClassVisitor visitor, List<Class> classesVisited) { boolean okayToContinue = true; if (!classesVisited.contains(thisClass)) { classesVisited.add(thisClass); okayToContinue = visitor.visit(thisClass); if (okayToContinue) { Class[] interfaces = thisClass.getInterfaces(); int index = 0; while ((index < interfaces.length) && (okayToContinue)) { okayToContinue = visitUniqueInterfaces(interfaces[index++], visitor, classesVisited); } if (okayToContinue) { Class superClass = thisClass.getSuperclass(); if ((superClass != null) && (!Object.class.equals(superClass))) { okayToContinue = visitUniqueInterfaces(superClass, visitor, classesVisited); } } } } return okayToContinue; }
From source file:com.zfsoft.util.reflect.ReflectionUtils.java
/** * clazz???/*from w ww . ja v a 2 s . c o m*/ * @param clazz Class * @param targetInterface ?? * @return */ public static boolean isInterface(Class clazz, Class targetInterface) { Class[] face = clazz.getInterfaces(); for (int i = 0, j = face.length; i < j; i++) { if (face[i].getName().equals(targetInterface.getName())) { return true; } else { Class[] face1 = face[i].getInterfaces(); for (int x = 0; x < face1.length; x++) { if (face1[x].getName().equals(targetInterface.getName())) { return true; } else if (isInterface(face1[x], targetInterface)) { return true; } } } } if (null != clazz.getSuperclass()) { return isInterface(clazz.getSuperclass(), targetInterface); } return false; }
From source file:net.femtoparsec.jnlmin.utils.ReflectUtils.java
private static int findCIDistance(Class<?> lower, Class<?> upper) { if (!upper.isInterface() || lower.isInterface()) { throw new IllegalArgumentException( String.format("Invalid input class : upper=%s lower=%s", upper, lower)); }/*from www .j a v a 2 s .co m*/ if (lower == upper) { return 0; } if (!upper.isAssignableFrom(lower)) { return -1; } int distance = -1; int offset = 0; do { int tmp = findCIOneLevelDistance(lower, upper); if (tmp >= 0) { tmp += offset + 1; distance = distance < 0 ? tmp : Math.min(distance, tmp); } if (distance == 0) { break; } lower = lower.getSuperclass(); offset++; } while (lower != null); return distance; }
From source file:net.kamhon.ieagle.util.VoUtil.java
/** * To trim object String field//from ww w. j a va2s . c o m * * @param all * default is false. true means toTrim all String field, false toTrim the fields have * net.kamhon.ieagle.vo.core.annotation.ToTrim. * * @param objects */ public static void toTrimProperties(boolean all, Object... objects) { for (Object object : objects) { if (object == null) { continue; } // getter for String field only Map<String, Method> getterMap = new HashMap<String, Method>(); // setter for String field only Map<String, Method> setterMap = new HashMap<String, Method>(); Class<?> clazz = object.getClass(); Method[] methods = clazz.getMethods(); for (Method method : methods) { if (method.getName().startsWith("get") && method.getParameterTypes().length == 0 && method.getReturnType().equals(String.class)) { // if method name is getXxx String fieldName = method.getName().substring(3); fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1); getterMap.put(fieldName, method); } else if (method.getName().startsWith("set") && method.getParameterTypes().length == 1 && method.getParameterTypes()[0] == String.class && method.getReturnType().equals(void.class)) { // log.debug("setter = " + method.getName()); // if method name is setXxx String fieldName = method.getName().substring(3); fieldName = fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1); setterMap.put(fieldName, method); } } // if the key exists in both getter & setter for (String key : getterMap.keySet()) { if (setterMap.containsKey(key)) { try { Method getterMethod = getterMap.get(key); Method setterMethod = setterMap.get(key); // if not all, check on Field if (!all) { Field field = null; Class<?> tmpClazz = clazz; // looping up to superclass to get decleared field do { try { field = tmpClazz.getDeclaredField(key); } catch (Exception ex) { // do nothing } if (field != null) { break; } tmpClazz = tmpClazz.getSuperclass(); } while (tmpClazz != null); ToTrim toTrim = field.getAnnotation(ToTrim.class); if (toTrim == null || toTrim.trim() == false) { continue; } } String value = (String) getterMethod.invoke(object, new Object[] {}); if (StringUtils.isNotBlank(value)) setterMethod.invoke(object, value.trim()); } catch (Exception ex) { // log.error("Getter Setter for " + key + " has error ", ex); } } } } }
From source file:Main.java
public static <T extends Object, Result> Result callMethodCast(T receiver, Class<Result> resultClass, String methodName, Object... args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { if (receiver == null || methodName == null) { return null; }/* w ww . j a v a 2s . co m*/ Class<?> cls = receiver.getClass(); Method toInvoke = null; do { Method[] methods = cls.getDeclaredMethods(); methodLoop: for (Method method : methods) { if (!methodName.equals(method.getName())) { continue; } Class<?>[] paramTypes = method.getParameterTypes(); if (args == null && paramTypes == null) { toInvoke = method; break; } else if (args == null || paramTypes == null || paramTypes.length != args.length) { continue; } for (int i = 0; i < args.length; ++i) { if (!paramTypes[i].isAssignableFrom(args[i].getClass())) { continue methodLoop; } } toInvoke = method; } } while (toInvoke == null && (cls = cls.getSuperclass()) != null); Result t; if (toInvoke != null) { boolean accessible = toInvoke.isAccessible(); toInvoke.setAccessible(true); if (resultClass != null) { t = resultClass.cast(toInvoke.invoke(receiver, args)); } else { toInvoke.invoke(receiver, args); t = null; } toInvoke.setAccessible(accessible); return t; } else { throw new NoSuchMethodException("Method " + methodName + " not found"); } }
From source file:com.yiji.openapi.sdk.util.Reflections.java
public static Set<String> getSimpleFieldNames(Class<?> pojoClass) { Set<String> propertyNames = new HashSet<String>(); Class<?> clazz = pojoClass; do {//from ww w.j a v a 2 s .c om Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (!Modifier.isStatic(field.getModifiers()) && (field.getType().isPrimitive() || isWrapClass(field.getType()) || field.getType().isAssignableFrom(Timestamp.class) || field.getType().isAssignableFrom(Date.class) || field.getType().isAssignableFrom(String.class) || field.getType().isAssignableFrom(Calendar.class))) { propertyNames.add(field.getName()); } } clazz = clazz.getSuperclass(); } while (clazz != null && !clazz.getSimpleName().equalsIgnoreCase("Object")); return propertyNames; }
From source file:com.jk.util.JKObjectUtil.java
/** * Call method./*from www . j a v a2 s . c o m*/ * * @param obj * the obj * @param methodName * the method name * @param includePrivateMehtods * the include private mehtods * @param args * the args * @throws InvocationTargetException * the invocation target exception */ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void callMethod(final Object obj, final String methodName, final boolean includePrivateMehtods, final Object... args) throws InvocationTargetException { final Class[] intArgsClass = initParamsClasses(args); try { Class<?> current = obj.getClass(); Method method = null; while (current != Object.class) { try { method = current.getDeclaredMethod(methodName, intArgsClass); break; } catch (final NoSuchMethodException ex) { current = current.getSuperclass(); } } if (method == null) { throw new NoSuchMethodException("Mehtod is not found in " + current); } method.setAccessible(true); method.invoke(obj, args); } catch (final InvocationTargetException e) { throw new InvocationTargetException(e.getCause()); } catch (final Exception e) { throw new InvocationTargetException(e); } }
From source file:com.jeeframework.util.classes.ClassUtils.java
/** * Return the user-defined class for the given class: usually simply the given * class, but the original class in case of a CGLIB-generated subclass. * @param clazz the class to check//from www. java2 s . c o m * @return the user-defined class */ public static Class getUserClass(Class clazz) { return (clazz != null && clazz.getName().indexOf(CGLIB_CLASS_SEPARATOR) != -1 ? clazz.getSuperclass() : clazz); }
From source file:MyClass.java
public static String getClassDescription(Class c) { StringBuilder classDesc = new StringBuilder(); int modifierBits = 0; String keyword = ""; if (c.isInterface()) { modifierBits = c.getModifiers() & Modifier.interfaceModifiers(); if (c.isAnnotation()) { keyword = "@interface"; } else {//from www.j ava2 s . c om keyword = "interface"; } } else if (c.isEnum()) { modifierBits = c.getModifiers() & Modifier.classModifiers(); keyword = "enum"; } modifierBits = c.getModifiers() & Modifier.classModifiers(); keyword = "class"; String modifiers = Modifier.toString(modifierBits); classDesc.append(modifiers); classDesc.append(" " + keyword); String simpleName = c.getSimpleName(); classDesc.append(" " + simpleName); String genericParms = getGenericTypeParams(c); classDesc.append(genericParms); Class superClass = c.getSuperclass(); if (superClass != null) { String superClassSimpleName = superClass.getSimpleName(); classDesc.append(" extends " + superClassSimpleName); } String interfaces = Main.getClassInterfaces(c); if (interfaces != null) { classDesc.append(" implements " + interfaces); } return classDesc.toString(); }