List of usage examples for java.lang Class getSuperclass
@HotSpotIntrinsicCandidate public native Class<? super T> getSuperclass();
From source file:jef.tools.reflect.ClassEx.java
/** * CGLib?class/*from ww w . j a v a 2s . co m*/ * * @param clz * @return */ public static Class<?> getRealClass(Class<?> clz) { if (isCGLibProxy(clz)) { return clz.getSuperclass(); } return clz; }
From source file:ca.sqlpower.testutil.TestUtils.java
/** * Returns the set of property names which have both a getter and a setter * method and are annotated to be persisted through the {@link SPPersister} * classes.//from w w w . jav a 2 s . c om * * @param objectUnderTest * The object that contains the persistable properties we want to * find. * @param includeTransient * If true the properties marked as transient will also be * included. If false only the properties that are persisted and * not transient are returned. * @param includeConstructorMutators * If true the properties that have getters but can only be set * through a constructor due to being final will be included. If * false the persisted properties provided must have a setter. */ public static Set<String> findPersistableBeanProperties(SPObject objectUnderTest, boolean includeTransient, boolean includeConstructorMutators) throws Exception { Set<String> getters = new HashSet<String>(); Set<String> setters = new HashSet<String>(); for (Method m : objectUnderTest.getClass().getMethods()) { if (m.getName().equals("getClass")) continue; //skip non-public methods as they are not visible for persisting anyways. if (!Modifier.isPublic(m.getModifiers())) continue; //skip static methods if (Modifier.isStatic(m.getModifiers())) continue; if (m.getName().startsWith("get") || m.getName().startsWith("is")) { Class<?> parentClass = objectUnderTest.getClass(); boolean accessor = false; boolean ignored = false; boolean isTransient = false; parentClass.getMethod(m.getName(), m.getParameterTypes());//test while (parentClass != null) { Method parentMethod; try { parentMethod = parentClass.getMethod(m.getName(), m.getParameterTypes()); } catch (NoSuchMethodException e) { parentClass = parentClass.getSuperclass(); continue; } if (parentMethod.getAnnotation(Accessor.class) != null) { accessor = true; if (parentMethod.getAnnotation(Transient.class) != null) { isTransient = true; } break; } else if (parentMethod.getAnnotation(NonProperty.class) != null || parentMethod.getAnnotation(NonBound.class) != null) { ignored = true; break; } parentClass = parentClass.getSuperclass(); } if (accessor) { if (includeTransient || !isTransient) { if (m.getName().startsWith("get")) { getters.add(m.getName().substring(3)); } else if (m.getName().startsWith("is")) { getters.add(m.getName().substring(2)); } } } else if (ignored) { //ignored so skip } else { fail("The method " + m.getName() + " of " + objectUnderTest.toString() + " is a getter that is not annotated " + "to be an accessor or transient. The exiting annotations are " + Arrays.toString(m.getAnnotations())); } } else if (m.getName().startsWith("set")) { if (m.getAnnotation(Mutator.class) != null) { if ((includeTransient || m.getAnnotation(Transient.class) == null) && (includeConstructorMutators || !m.getAnnotation(Mutator.class).constructorMutator())) { setters.add(m.getName().substring(3)); } } else if (m.getAnnotation(NonProperty.class) != null || m.getAnnotation(NonBound.class) != null) { //ignored so skip and pass } else { fail("The method " + m.getName() + " is a setter that is not annotated " + "to be a mutator or transient."); } } } Set<String> beanNames = new HashSet<String>(); for (String beanName : getters) { if (setters.contains(beanName)) { String firstLetter = new String(new char[] { beanName.charAt(0) }); beanNames.add(beanName.replaceFirst(firstLetter, firstLetter.toLowerCase())); } } return beanNames; }
From source file:de.micromata.genome.util.runtime.ClassUtils.java
/** * Collect class annotations./*from w w w . j av a2 s . c om*/ * * @param <T> the generic type * @param clazz the clazz * @param annotClass the annot class * @param annots the annots * @param visitedClasses the visited classes */ public static <T extends Annotation> void collectClassAnnotations(Class<?> clazz, Class<T> annotClass, List<T> annots, Set<Class<?>> visitedClasses) { if (visitedClasses.contains(clazz) == true) { return; } visitedClasses.add(clazz); T[] ana = clazz.getAnnotationsByType(annotClass); if (ana != null) { for (T an : ana) { annots.add(an); } } Class<?> superclz = clazz.getSuperclass(); if (superclz == null || superclz == Object.class) { return; } collectClassAnnotations(superclz, annotClass, annots, visitedClasses); Class<?>[] ifaces = clazz.getInterfaces(); if (ifaces == null || ifaces.length == 0) { return; } for (Class<?> iclazz : ifaces) { collectClassAnnotations(iclazz, annotClass, annots, visitedClasses); } }
From source file:io.werval.runtime.util.TypeResolver.java
private static Map<TypeVariable<?>, Type> getTypeVariableMap(final Class<?> targetType) { Reference<Map<TypeVariable<?>, Type>> ref = CACHE.get(targetType); Map<TypeVariable<?>, Type> map = ref != null ? ref.get() : null; if (map == null) { map = new HashMap<>(); // Populate interfaces buildTypeVariableMap(targetType.getGenericInterfaces(), map); // Populate super classes and interfaces Type genericType = targetType.getGenericSuperclass(); Class<?> type = targetType.getSuperclass(); while (type != null && !Object.class.equals(type)) { if (genericType instanceof ParameterizedType) { buildTypeVariableMap((ParameterizedType) genericType, map); }// w w w .j a v a 2 s. c o m buildTypeVariableMap(type.getGenericInterfaces(), map); genericType = type.getGenericSuperclass(); type = type.getSuperclass(); } // Populate enclosing classes type = targetType; while (type.isMemberClass()) { genericType = type.getGenericSuperclass(); if (genericType instanceof ParameterizedType) { buildTypeVariableMap((ParameterizedType) genericType, map); } type = type.getEnclosingClass(); } if (cacheEnabled) { CACHE.put(targetType, new WeakReference<>(map)); } } return map; }
From source file:de.beyondjava.angularFaces.core.ELTools.java
private static Field getField(String p_expression) { synchronized (fields) { if (fields.containsKey(p_expression)) { return fields.get(p_expression); }/* w ww . j a v a 2s . co m*/ } if (p_expression.startsWith("#{") && p_expression.endsWith("}")) { int delimiterPos = p_expression.lastIndexOf('.'); if (delimiterPos < 0) { LOGGER.log(Level.WARNING, "There's no field to access: #{" + p_expression + "}"); return null; } String beanExp = p_expression.substring(0, delimiterPos) + "}"; String fieldName = p_expression.substring(delimiterPos + 1, p_expression.length() - 1); Object container = evalAsObject(beanExp); if (null == container) { LOGGER.severe("Can't read the bean '" + beanExp + "'. Thus JSR 303 annotations can't be read, let alone used by the AngularDart client."); return null; } Class<? extends Object> c = container.getClass(); while (c != null) { Field declaredField; try { declaredField = c.getDeclaredField(fieldName); synchronized (fields) { fields.put(p_expression, declaredField); } return declaredField; } catch (NoSuchFieldException e) { // let\"s try with the super class c = c.getSuperclass(); } catch (SecurityException e) { LOGGER.log(Level.SEVERE, "Unable to access a field", e); e.printStackTrace(); return null; } } } return null; }
From source file:com.espertech.esper.event.bean.BeanEventType.java
private static void getSuperClasses(Class clazz, Set<Class> result) { Class superClass = clazz.getSuperclass(); if (superClass == null) { return;/*from ww w . ja va 2s. c o m*/ } result.add(superClass); getSuper(superClass, result); }
From source file:de.alpharogroup.lang.ClassExtensions.java
/** * Gets the parent base class from the given child class. * * @param childClass/*from w w w. j ava 2s. c o m*/ * the child class * @return the parent base class from the given child class. */ public static Class<?> getBaseClass(final Class<?> childClass) { if (childClass == null) { return childClass; } Class<?> superClass = childClass.getSuperclass(); while ((superClass != null) && !superClass.getSuperclass().equals(Object.class)) { superClass = superClass.getSuperclass(); } if (superClass == null) { return childClass; } return superClass; }
From source file:gr.abiss.calipso.jpasearch.specifications.GenericSpecifications.java
public static Field getField(Class<?> clazz, String fieldName) { Field field = null;//from w w w.j a va 2 s . c om if (!IGNORED_FIELD_NAMES.contains(fieldName)) { String key = clazz.getName() + "#" + fieldName; field = FIELD_CACHE.get(key); // find it if not cached if (field == null && !FIELD_CACHE.containsKey(key)) { Class<?> tmpClass = clazz; do { for (Field tmpField : tmpClass.getDeclaredFields()) { String candidateName = tmpField.getName(); if (candidateName.equals(fieldName)) { // field.setAccessible(true); FIELD_CACHE.put(key, tmpField); field = tmpField; break; } } tmpClass = tmpClass.getSuperclass(); } while (tmpClass != null && field == null); } if (field == null) { LOGGER.warn("Field '" + fieldName + "' not found on class " + clazz); // HashMap handles null values so we can use containsKey to cach // invalid fields and hence skip the reflection scan FIELD_CACHE.put(key, null); } // throw new RuntimeException("Field '" + fieldName + // "' not found on class " + clazz); } return field; }
From source file:com.bosscs.spark.commons.utils.Utils.java
/** * @param object//from ww w. j a v a 2s . c o m * @param fieldName * @param fieldValue * @return if the operation has been correct. */ public static boolean setFieldWithReflection(Object object, String fieldName, Object fieldValue) { Class<?> clazz = object.getClass(); while (clazz != null) { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); field.set(object, fieldValue); return true; } catch (NoSuchFieldException e) { clazz = clazz.getSuperclass(); } catch (Exception e) { throw new IllegalStateException(e); } } return false; }
From source file:jenkins.plugins.git.MethodUtils.java
/** * <p>Retrieves a method whether or not it's accessible. If no such method * can be found, return {@code null}.</p> * * @param cls The class that will be subjected to the method search * @param methodName The method that we wish to call * @param parameterTypes Argument class types * @return The method//from w ww .j a va 2 s. c o m */ static Method getMethodImpl(final Class<?> cls, final String methodName, final Class<?>... parameterTypes) { Validate.notNull(cls, "Null class not allowed."); Validate.notEmpty(methodName, "Null or blank methodName not allowed."); // fast path, check if directly declared on the class itself for (final Method method : cls.getDeclaredMethods()) { if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) { return method; } } if (!cls.isInterface()) { // ok, now check if directly implemented on a superclass // Java 8: note that super-interface implementations trump default methods for (Class<?> klass = cls.getSuperclass(); klass != null; klass = klass.getSuperclass()) { for (final Method method : klass.getDeclaredMethods()) { if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) { return method; } } } } // ok, now we are looking for an interface method... the most specific one // in the event that we have two unrelated interfaces both declaring a method of the same name // we will give up and say we could not find the method (the logic here is that we are primarily // checking for overrides, in the event of a Java 8 default method, that default only // applies if there is no conflict from an unrelated interface... thus if there are // default methods and they are unrelated then they don't exist... if there are multiple unrelated // abstract methods... well they won't count as a non-abstract implementation Method res = null; for (final Class<?> klass : (List<Class<?>>) ClassUtils.getAllInterfaces(cls)) { for (final Method method : klass.getDeclaredMethods()) { if (methodName.equals(method.getName()) && Arrays.equals(parameterTypes, method.getParameterTypes())) { if (res == null) { res = method; } else { Class<?> c = res.getDeclaringClass(); if (c == klass) { // match, ignore } else if (c.isAssignableFrom(klass)) { // this is a more specific match res = method; } else if (!klass.isAssignableFrom(c)) { // multiple overlapping interfaces declare this method and there is no common ancestor return null; } } } } } return res; }