List of usage examples for java.lang Class getInterfaces
public Class<?>[] getInterfaces()
From source file:org.gradle.model.internal.manage.schema.store.ModelSchemaExtractor.java
public <T> void validateType(ModelType<T> type) { Class<T> typeClass = type.getConcreteClass(); if (!isManaged(typeClass)) { throw invalid(type, String.format("must be annotated with %s", Managed.class.getName())); }// w ww . j av a 2s .c om if (!typeClass.isInterface()) { throw invalid(type, "must be defined as an interface"); } if (typeClass.getInterfaces().length > 0) { throw invalid(type, "cannot extend other types"); } if (typeClass.getTypeParameters().length > 0) { throw invalid(type, "cannot be a parameterized type"); } }
From source file:org.apache.poi.util.MethodUtils.java
/** * <p>Return an accessible method (that is, one that can be invoked via * reflection) that implements the specified method, by scanning through * all implemented interfaces and subinterfaces. If no such method * can be found, return <code>null</code>.</p> * * <p> There isn't any good reason why this method must be private. * It is because there doesn't seem any reason why other classes should * call this rather than the higher level methods.</p> * * @param clazz Parent class for the interfaces to be checked * @param methodName Method name of the method we wish to call * @param parameterTypes The parameter type signatures *//*w w w . j a v a 2s .c om*/ private static Method getAccessibleMethodFromInterfaceNest(Class clazz, String methodName, Class[] parameterTypes) { Method method = null; // Search up the superclass chain for (; clazz != null; clazz = clazz.getSuperclass()) { // Check the implemented interfaces of the parent class Class[] interfaces = clazz.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { // Is this interface public? if (!Modifier.isPublic(interfaces[i].getModifiers())) { continue; } // Does the method exist on this interface? try { method = interfaces[i].getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException e) { /* Swallow, if no method is found after the loop then this * method returns null. */ } if (method != null) { return method; } // Recursively check our parent interfaces method = getAccessibleMethodFromInterfaceNest(interfaces[i], methodName, parameterTypes); if (method != null) { return method; } } } // If we found a method return it if (method != null) { return (method); } // We did not find anything return (null); }
From source file:org.apache.axis2.jaxws.description.builder.converter.JavaClassToDBCConverter.java
private void establishInterfaceHierarchy(Class[] interfaces) { if (interfaces.length > 0) { for (Class inter : interfaces) { classes.add(inter);/* w ww . jav a 2 s .co m*/ establishInterfaceHierarchy(inter.getInterfaces()); } } }
From source file:org.broadinstitute.gatk.tools.walkers.help.WalkerDocumentationHandler.java
/** * Utility function that checks which parallelism options are available for an instance of class c. * * @param myClass the class to query for the interfaces * @param parallelOptions an empty HashSet in which to collect the info * @return a hash set of parallelism options, otherwise an empty set *///from w w w . j av a 2 s . c om private HashSet<HashMap<String, Object>> getParallelism(Class myClass, HashSet<HashMap<String, Object>> parallelOptions) { // // Retrieve interfaces Class[] implementedInterfaces = myClass.getInterfaces(); for (Class intfClass : implementedInterfaces) { final HashMap<String, Object> nugget = new HashMap<String, Object>(); if (intfClass.getSimpleName().equals("TreeReducible")) { nugget.put("name", intfClass.getSimpleName()); nugget.put("arg", HelpConstants.ARG_TREEREDUCIBLE); nugget.put("link", CMDLINE_GATK_URL + "#" + HelpConstants.ARG_TREEREDUCIBLE); } else if (intfClass.getSimpleName().equals("NanoSchedulable")) { nugget.put("name", intfClass.getSimpleName()); nugget.put("arg", HelpConstants.ARG_NANOSCHEDULABLE); nugget.put("link", CMDLINE_GATK_URL + "#" + HelpConstants.ARG_NANOSCHEDULABLE); } else { continue; } parallelOptions.add(nugget); } // Look up superclasses recursively final Class mySuperClass = myClass.getSuperclass(); if (mySuperClass.getSimpleName().equals("Object")) { return parallelOptions; } return getParallelism(mySuperClass, parallelOptions); }
From source file:org.grouplens.grapht.util.Types.java
/** * Return the type distance between the child and parent types. The child type * must be a subtype of the parent. The type distance between a class and itself is 0; * the distance from a class to one of its immediate supertypes (superclass or a directly * implemented interface) is 1; deeper distances are computed recursively. * //from w w w .ja v a 2 s .co m * @param child The child type * @param parent The parent type * @return The type distance * @throws IllegalArgumentException if {@code child} is not a subtype of {@code parent}. */ public static int getTypeDistance(@Nonnull Class<?> child, @Nonnull Class<?> parent) { Preconditions.notNull("child class", child); Preconditions.notNull("parent class", parent); if (child.equals(parent)) { // fast-path same-class tests return 0; } else if (!parent.isAssignableFrom(child)) { // if child does not extend from the parent, return -1 throw new IllegalArgumentException("child not a subclass of parent"); } else if (!parent.isInterface()) { // if the parent is not an interface, we only need to follower superclasses int distance = 0; Class<?> cur = child; while (!cur.equals(parent)) { distance++; cur = cur.getSuperclass(); } return distance; } else { // worst case, recursively compute the type // recursion is safe, as types aren't too deep except in crazy-land int minDepth = Integer.MAX_VALUE; Class<?> sup = child.getSuperclass(); if (sup != null && parent.isAssignableFrom(sup)) { minDepth = getTypeDistance(sup, parent); } for (Class<?> iface : child.getInterfaces()) { if (parent.isAssignableFrom(iface)) { int d = getTypeDistance(iface, parent); if (d < minDepth) { minDepth = d; } } } // minDepth now holds the depth of the superclass with shallowest depth return minDepth + 1; } }
From source file:com.nanyou.framework.jdbc.sql.beans.ClassInfo.java
private Method[] getAllMethodsForClass(Class cls) { Set uniqueMethodNames = new HashSet(); List allMethods = new ArrayList(); Class currentClass = cls; while (currentClass != null) { addMethods(currentClass, uniqueMethodNames, allMethods); Class[] interfaces = currentClass.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { addMethods(interfaces[i], uniqueMethodNames, allMethods); }/* www.ja v a2 s . c om*/ currentClass = currentClass.getSuperclass(); } return (Method[]) allMethods.toArray(new Method[allMethods.size()]); }
From source file:org.mule.devkit.doclet.Doclava.java
/** * Filters out hidden elements.//from ww w .j a v a 2 s . co m */ private static Object filterHidden(Object o, Class<?> expected) { if (o == null) { return null; } Class<?> type = o.getClass(); if (type.getName().startsWith("com.sun.")) { // TODO: Implement interfaces from superclasses, too. return Proxy.newProxyInstance(type.getClassLoader(), type.getInterfaces(), new HideHandler(o)); } else if (o instanceof Object[]) { Class<?> componentType = expected.getComponentType(); Object[] array = (Object[]) o; List<Object> list = new ArrayList<Object>(array.length); for (Object entry : array) { if ((entry instanceof Doc) && isHidden((Doc) entry)) { continue; } list.add(filterHidden(entry, componentType)); } return list.toArray((Object[]) Array.newInstance(componentType, list.size())); } else { return o; } }
From source file:com.dianping.resource.io.util.ClassUtils.java
/** * Return all interfaces that the given class implements as Set, * including ones implemented by superclasses. * <p>If the class itself is an interface, it gets returned as sole interface. * @param clazz the class to analyze for interfaces * @param classLoader the ClassLoader that the interfaces need to be visible in * (may be {@code null} when accepting all declared interfaces) * @return all interfaces that the given object implements as Set *//*from w w w.j ava2 s .c o m*/ public static Set<Class<?>> getAllInterfacesForClassAsSet(Class<?> clazz, ClassLoader classLoader) { Assert.notNull(clazz, "Class must not be null"); if (clazz.isInterface() && isVisible(clazz, classLoader)) { return Collections.<Class<?>>singleton(clazz); } Set<Class<?>> interfaces = new LinkedHashSet<Class<?>>(); while (clazz != null) { Class<?>[] ifcs = clazz.getInterfaces(); for (Class<?> ifc : ifcs) { interfaces.addAll(getAllInterfacesForClassAsSet(ifc, classLoader)); } clazz = clazz.getSuperclass(); } return interfaces; }
From source file:com.eviware.soapui.plugins.LoaderBase.java
protected List<Class<? extends SoapUIListener>> registerListeners( List<Class<? extends SoapUIListener>> listeners) { for (Class<?> listenerClass : listeners) { Class currentListenerClass = listenerClass; while (currentListenerClass != null) { for (Class<?> implementedInterface : currentListenerClass.getInterfaces()) { if (SoapUIListener.class.isAssignableFrom(implementedInterface)) { listenerRegistry.addListener(implementedInterface, listenerClass, null); }/*www.jav a 2 s . c o m*/ } currentListenerClass = currentListenerClass.getSuperclass(); } } return listeners; }
From source file:javadz.beanutils.MethodUtils.java
/** * <p>Return an accessible method (that is, one that can be invoked via * reflection) that implements the specified method, by scanning through * all implemented interfaces and subinterfaces. If no such method * can be found, return <code>null</code>.</p> * * <p> There isn't any good reason why this method must be private. * It is because there doesn't seem any reason why other classes should * call this rather than the higher level methods.</p> * * @param clazz Parent class for the interfaces to be checked * @param methodName Method name of the method we wish to call * @param parameterTypes The parameter type signatures *//*ww w .j av a2 s.c o m*/ private static Method getAccessibleMethodFromInterfaceNest(Class clazz, String methodName, Class[] parameterTypes) { Method method = null; // Search up the superclass chain for (; clazz != null; clazz = clazz.getSuperclass()) { // Check the implemented interfaces of the parent class Class[] interfaces = clazz.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { // Is this interface public? if (!Modifier.isPublic(interfaces[i].getModifiers())) { continue; } // Does the method exist on this interface? try { method = interfaces[i].getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException e) { /* Swallow, if no method is found after the loop then this * method returns null. */ } if (method != null) { return method; } // Recursively check our parent interfaces method = getAccessibleMethodFromInterfaceNest(interfaces[i], methodName, parameterTypes); if (method != null) { return method; } } } // We did not find anything return (null); }