List of usage examples for org.apache.commons.lang3 ClassUtils getAllInterfaces
public static List<Class<?>> getAllInterfaces(final Class<?> cls)
Gets a List of all interfaces implemented by the given class and its superclasses.
The order is determined by looking through each interface in turn as declared in the source file and following its hierarchy up.
From source file:org.jboss.qa.phaser.registry.SimpleInstanceRegistry.java
public void insert(Object o) { insert(o, o.getClass()); insert(o, ClassUtils.getAllInterfaces(o.getClass())); insert(o, ClassUtils.getAllSuperclasses(o.getClass())); }
From source file:org.mqnaas.core.impl.ApplicationProxyHolder.java
/** * Retrieves interfaces extending IApplication which are implemented by given clazz * /*from ww w. j a v a 2 s . co m*/ * @param clazz * @return interfaces extending IApplication which are implemented by given clazz */ private static Collection<Class<? extends IApplication>> computeApplications( Class<? extends IApplication> clazz) { Collection<Class<? extends IApplication>> applicationClasses = new ArrayList<Class<? extends IApplication>>(); for (Class<?> interfaze : ClassUtils.getAllInterfaces(clazz)) { // Ignore the IApplication interface itself if (interfaze.equals(IApplication.class)) continue; // Ignore the ICapability interface itself if (interfaze.equals(ICapability.class)) continue; // Ignore all interfaces that do not extend IApplication if (!IApplication.class.isAssignableFrom(interfaze)) continue; // Now do the cast: this one is safe because we explicitly checked it before @SuppressWarnings("unchecked") Class<? extends IApplication> applicationInterface = (Class<? extends IApplication>) interfaze; applicationClasses.add(applicationInterface); } return applicationClasses; }
From source file:org.mqnaas.core.impl.BinderDecider.java
@Override public boolean shouldBeBound(IResource resource, Class<? extends ICapability> capabilityClass) { boolean shouldBeBound = false; List<Class<?>> interfaces = ClassUtils.getAllInterfaces(capabilityClass); interfaces.remove(ICapability.class); // If their is no interface remaining, there's nothing to bind... if (interfaces.isEmpty()) return shouldBeBound; // Now for the process of binding, which for the moment is a very simple implementation: look for a static isSupporting method in the // capability and use it to determine the binding Method isSupportingForResourceMethod = getIsSupporting(capabilityClass, IResource.class); Method isSupportingForRootResourceMethod = getIsSupporting(capabilityClass, IRootResource.class); try {/*from ww w. j a v a2s. co m*/ shouldBeBound = (Boolean) isSupportingForResourceMethod.invoke(null, resource); if (!shouldBeBound && (resource instanceof IRootResource)) { shouldBeBound = (Boolean) isSupportingForRootResourceMethod.invoke(null, resource); } } catch (Exception e1) { if (resource instanceof IRootResource) { try { shouldBeBound = (Boolean) isSupportingForRootResourceMethod.invoke(null, resource); } catch (Exception e2) { // no way to establish bind StringBuilder sb = new StringBuilder(); sb.append("No way of establishing bind between "); sb.append(capabilityClass.getName()).append(" and resource ") .append(resource.getClass().getSimpleName()); if (resource instanceof IRootResource) { IRootResource rr = (IRootResource) resource; Specification specification = rr.getDescriptor().getSpecification(); sb.append("[type=").append(specification.getType()); if (specification.getModel() != null) { sb.append(", model=").append(specification.getModel()); } if (specification.getVersion() != null) { sb.append(", version=").append(specification.getVersion()); } sb.append("]"); } sb.append("."); if (isSupportingForResourceMethod != null) { sb.append(" Tried ").append(IS_SUPPORTING_METHOD_NAME).append("(") .append(IResource.class.getSimpleName()).append(")."); } else if (isSupportingForRootResourceMethod != null) { sb.append(" Tried ").append(IS_SUPPORTING_METHOD_NAME).append("(") .append(IRootResource.class.getSimpleName()).append(")."); } else { sb.append(" No ").append(IS_SUPPORTING_METHOD_NAME).append("(...) implementation found."); } log.info(sb.toString()); } } } log.debug(getClass().getSimpleName() + ".shouldBeBound(" + resource + ", " + capabilityClass + "): " + shouldBeBound); return shouldBeBound; }
From source file:org.mqnaas.core.impl.CapabilityInstance.java
protected static Collection<Class<? extends ICapability>> computeCapabilities( Class<? extends IApplication> clazz) { Collection<Class<? extends ICapability>> capabilityClasses = new ArrayList<Class<? extends ICapability>>(); for (Class<?> interfaze : ClassUtils.getAllInterfaces(clazz)) { // Ignore the ICapability interface itself if (interfaze.equals(ICapability.class)) continue; // Ignore all interfaces that do not extend ICapability if (!ICapability.class.isAssignableFrom(interfaze)) continue; // Now do the cast: this one is safe because we explicitly checked it before @SuppressWarnings("unchecked") Class<? extends ICapability> capabilityInterface = (Class<? extends ICapability>) interfaze; capabilityClasses.add(capabilityInterface); }/*from w ww. j a v a 2 s . com*/ return capabilityClasses; }
From source file:org.ofbiz.base.util.ObjectType.java
/** * Tests if a class properly implements the specified interface. * @param objectClass Class to test//w w w .ja v a 2 s. c o m * @param interfaceClass Class to test against * @return true if interfaceClass is an interface of objectClass */ public static boolean interfaceOf(Class<?> objectClass, Class<?> interfaceClass) { while (objectClass != null) { // SCIPIO (03/26/2018): objectClass.getInterfaces() only returned the interfaces directly implemented by the given objectClass. // That was wrong because it didn't look for interfaces implemented in parent classes or implemented within those explicit // implemented interfaces. List<Class<?>> ifaces = ClassUtils.getAllInterfaces(objectClass); for (Class<?> iface : ifaces) { if (iface == interfaceClass) return true; } objectClass = objectClass.getSuperclass(); } return false; }
From source file:org.wrml.runtime.schema.Prototype.java
/** * Creates a new Prototype to represent the identified schema. * * @param schemaLoader The schema loader for this prototype's schema. * @param schemaUri The schema identifier. * @throws PrototypeException Thrown if there are problems with the initial prototyping of the schema. */// w w w. j av a 2s. co m Prototype(final SchemaLoader schemaLoader, final URI schemaUri) throws PrototypeException { LOGGER.debug("Creating Prototype for schema ID: {}", new Object[] { schemaUri }); _SchemaLoader = schemaLoader; if (_SchemaLoader == null) { throw new PrototypeException("The SchemaLoader parameter value cannot be *null*.", null, this); } _SchemaUri = schemaUri; if (_SchemaUri == null) { throw new PrototypeException("The undefined (aka *null*) schema can not be prototyped.", null, this); } if (schemaUri.equals(schemaLoader.getResourceTemplateSchemaUri())) { LOGGER.debug("Creating Prototype for ResourceTemplate"); } _UniqueName = new UniqueName(_SchemaUri); // // Use the SchemaLoader and the schema uri to get the schema's Java Class // representation. // final Class<?> schemaInterface = getSchemaInterface(); if (ValueType.JAVA_TYPE_ABSTRACT.equals(schemaInterface)) { _IsAbstract = true; } else if (Document.class.equals(schemaInterface)) { _IsDocument = true; } // // Introspect the associated class, extracting metadata from the parent // schema's Java interfaces (up to but not including the Model // interface). // _SchemaBean = new JavaBean(schemaInterface, ValueType.JAVA_TYPE_MODEL, LinkSlot.class); _AllBaseSchemaUris = new LinkedHashSet<>(); _BaseSchemaUris = new LinkedHashSet<>(); _AllSlotNames = new TreeSet<>(); _ProtoSlots = new TreeMap<>(); _CollectionPropertyProtoSlots = new TreeMap<>(); _LinkRelationUris = new TreeMap<>(); _LinkProtoSlots = new TreeMap<>(); _SlotAliases = new TreeMap<>(); _SearchableSlots = new TreeSet<>(); // initBaseSchemas(...) { // // Use Java reflection to get all implemented interfaces and then turn // them into schema ids. With reflection we get de-duplication and // recursive traversal for free. // final List<Class<?>> allBaseInterfaces = ClassUtils.getAllInterfaces(schemaInterface); // Loop backwards to achieve desired key mapping precedence/overriding for (final Class<?> baseInterface : allBaseInterfaces) { if (ValueType.isSchemaInterface(baseInterface) && (baseInterface != ValueType.JAVA_TYPE_MODEL)) { final URI baseSchemaUri = _SchemaLoader.getTypeUri(baseInterface); _AllBaseSchemaUris.add(baseSchemaUri); if (Document.class.equals(baseInterface)) { _IsDocument = true; } if (AggregateDocument.class.equals(baseInterface)) { _IsAggregate = true; } } } // Store the immediate base schemas as well final Class<?>[] baseInterfaces = schemaInterface.getInterfaces(); if (baseInterfaces != null) { for (final Class<?> baseInterface : baseInterfaces) { if (ValueType.isSchemaInterface(baseInterface) && (baseInterface != ValueType.JAVA_TYPE_MODEL)) { final URI baseSchemaUri = _SchemaLoader.getTypeUri(baseInterface); _BaseSchemaUris.add(baseSchemaUri); } if (ValueType.JAVA_TYPE_ABSTRACT.equals(baseInterface)) { _IsAbstract = true; } } } } // End of base schema init // initKeys(...) { final WRML wrml = schemaInterface.getAnnotation(WRML.class); if (wrml != null) { final String[] keySlotNameArray = wrml.keySlotNames(); if ((keySlotNameArray != null) && (keySlotNameArray.length > 0)) { _KeySlotNames = new TreeSet<>(Arrays.asList(keySlotNameArray)); if (_KeySlotNames.size() == 1) { final String keySlotName = _KeySlotNames.first(); final Property property = _SchemaBean.getProperties().get(keySlotName); if (property != null) { _KeyType = property.getType(); } else { throw new PrototypeException("The named key slot, \"" + keySlotName + "\", is not defined for Schema: " + schemaUri + ".", null, this); } } else { // Schemas with Keys that use more than one slot value to // determine uniqueness use the CompositeKey type (at // runtime) as their key object. // _KeyType = ValueType.JAVA_TYPE_COMPOSITE_KEY; } } final String[] comparableSlotNameArray = wrml.comparableSlotNames(); if ((comparableSlotNameArray != null) && (comparableSlotNameArray.length > 0)) { _ComparableSlotNames = new LinkedHashSet<String>(Arrays.asList(comparableSlotNameArray)); } final String titleSlotName = wrml.titleSlotName(); if (StringUtils.isNotBlank(titleSlotName)) { _TitleSlotName = titleSlotName; } } } // End of the key initialization // initMiscAnnotations(...) { final Description schemaDescription = schemaInterface.getAnnotation(Description.class); if (schemaDescription != null) { _Description = schemaDescription.value(); } else { _Description = null; } final Title schemaTitle = schemaInterface.getAnnotation(Title.class); if (schemaTitle != null) { _Title = schemaTitle.value(); } else { _Title = schemaInterface.getSimpleName(); } final ThumbnailImage thumbnailImage = schemaInterface.getAnnotation(ThumbnailImage.class); if (thumbnailImage != null) { _ThumbnailLocation = URI.create(thumbnailImage.value()); } else { _ThumbnailLocation = null; } _ReadOnly = (schemaInterface.getAnnotation(ReadOnly.class) != null) ? true : false; final Version schemaVersion = schemaInterface.getAnnotation(Version.class); if (schemaVersion != null) { _Version = schemaVersion.value(); } else { // TODO: Look for the "static final long serialVersionUID" ? _Version = 1L; } final Tags tags = schemaInterface.getAnnotation(Tags.class); if (tags != null) { final String[] tagArray = tags.value(); if ((tagArray != null) && (tagArray.length > 0)) { _Tags = new TreeSet<String>(Arrays.asList(tagArray)); } } } // End of annotation-based initialization // initPropertySlots(...) { final Map<String, Property> properties = _SchemaBean.getProperties(); for (final String slotName : properties.keySet()) { final Property property = properties.get(slotName); final PropertyProtoSlot propertyProtoSlot; final CollectionSlot collectionSlot = property.getAnnotation(CollectionSlot.class); if (collectionSlot != null) { propertyProtoSlot = new CollectionPropertyProtoSlot(this, slotName, property); } else { propertyProtoSlot = new PropertyProtoSlot(this, slotName, property); } addProtoSlot(propertyProtoSlot); } } // initLinkSlots(...) { // // Map the the schema bean's "other" (non-Property) methods. // final SortedMap<String, SortedSet<JavaMethod>> otherMethods = _SchemaBean.getOtherMethods(); final Set<String> otherMethodNames = otherMethods.keySet(); for (final String methodName : otherMethodNames) { final SortedSet<JavaMethod> methodSet = otherMethods.get(methodName); if (methodSet.size() != 1) { throw new PrototypeException("The link method: " + methodName + " cannot be overloaded.", this); } final JavaMethod javaMethod = methodSet.first(); final Method method = javaMethod.getMethod(); final LinkSlot linkSlot = method.getAnnotation(LinkSlot.class); if (linkSlot == null) { throw new PrototypeException("The method: " + javaMethod + " is not a link method", null, this); } final String relationUriString = linkSlot.linkRelationUri(); final URI linkRelationUri = URI.create(relationUriString); if (_LinkProtoSlots.containsKey(linkRelationUri)) { throw new PrototypeException( "A schema cannot use the same link relation for more than one method. Duplicate link relation: " + linkRelationUri + " found in link method: " + javaMethod, this); } final org.wrml.model.rest.Method relMethod = linkSlot.method(); String slotName = methodName; if (relMethod == org.wrml.model.rest.Method.Get && slotName.startsWith(JavaBean.GET)) { slotName = slotName.substring(3); slotName = Character.toLowerCase(slotName.charAt(0)) + slotName.substring(1); } _LinkRelationUris.put(slotName, linkRelationUri); if (_ProtoSlots.containsKey(slotName)) { throw new PrototypeException( "A schema cannot use the same name for more than one slot. Duplicate slot name: " + slotName + " found in link method: " + javaMethod, this); } final LinkProtoSlot linkProtoSlot = new LinkProtoSlot(this, slotName, javaMethod); if ((linkProtoSlot.isEmbedded() || isAggregate()) && (relMethod == org.wrml.model.rest.Method.Get)) { _ContainsEmbeddedLink = true; } _LinkProtoSlots.put(linkRelationUri, linkProtoSlot); addProtoSlot(linkProtoSlot); } } // End of link slot init if (!_SlotAliases.isEmpty()) { for (final String alias : _SlotAliases.keySet()) { final ProtoSlot protoSlot = _ProtoSlots.get(alias); protoSlot.setAlias(true); final String realName = _SlotAliases.get(alias); protoSlot.setRealName(realName); } } }
From source file:org.xwiki.filter.internal.DefaultFilterDescriptorManager.java
@Override public <F> F createCompositeFilter(ClassLoader loader, Object... filters) { Set<Class<?>> interfaces = new HashSet<Class<?>>(); for (Object filter : filters) { interfaces.addAll(ClassUtils.getAllInterfaces(filter.getClass())); }//from ww w . jav a 2s .com return (F) Proxy.newProxyInstance(loader, interfaces.toArray(CLASS_ARRAY), new CompositeFilter(this, filters)); }
From source file:org.xwiki.job.internal.xstream.SafeArrayConverter.java
/** * @param item the item to serialize/* w w w . j ava2 s. c om*/ * @return true of the item looks like a component */ private boolean isComponent(Object item) { if (item != null) { List<Class<?>> interfaces = ClassUtils.getAllInterfaces(item.getClass()); for (Class<?> iface : interfaces) { if (iface.isAnnotationPresent(Role.class)) { return true; } } } return false; }
From source file:org.zanata.database.ProxyUtil.java
/** * Returns a proxy which implements all the same interfaces as the object *///from www . j a v a 2s.c om static Object newProxy(Object object, InvocationHandler handler) { Class<?> clazz = object.getClass(); ClassLoader cl = clazz.getClassLoader(); List<Class<?>> allInterfaces = ClassUtils.getAllInterfaces(clazz); Class<?>[] interfaces = allInterfaces.toArray(new Class<?>[0]); return Proxy.newProxyInstance(cl, interfaces, handler); }
From source file:yoyo.framework.standard.shared.commons.lang.ClassUtilsTest.java
@Test public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException { List<Class<?>> classes = new ArrayList<Class<?>>() { {/* w ww . j a v a2 s . c o m*/ add(Integer.class); add(Long.class); } }; assertThat(ClassUtils.convertClassesToClassNames(classes).toArray(), is(new Object[] { "java.lang.Integer", "java.lang.Long" })); List<String> classNames = new ArrayList<String>() { { add("java.lang.Integer"); add("java.lang.Long"); } }; assertThat(ClassUtils.convertClassNamesToClasses(classNames).toArray(), is(classes.toArray())); assertThat(ClassUtils.getAllInterfaces(String.class).toArray(), is(new Object[] { java.io.Serializable.class, java.lang.Comparable.class, java.lang.CharSequence.class })); assertThat(ClassUtils.getAllSuperclasses(HashMap.class).toArray(), is(new Object[] { java.util.AbstractMap.class, java.lang.Object.class })); assertThat(ClassUtils.getClass("java.lang.String").toString(), is("class java.lang.String")); assertThat(ClassUtils.getPackageCanonicalName(String.class), is("java.lang")); assertThat(ClassUtils.getPackageName(String.class), is("java.lang")); assertThat(ClassUtils.getPublicMethod(String.class, "length", new Class[] {}), is(not(nullValue()))); assertThat(ClassUtils.getShortCanonicalName(String.class), is("String")); assertThat(ClassUtils.getShortClassName(String.class), is("String")); assertThat(ClassUtils.getSimpleName(String.class), is("String")); assertThat(ClassUtils.isAssignable(String.class, Object.class), is(true)); assertThat(ClassUtils.isInnerClass(Foo.class), is(true)); assertThat(ClassUtils.isInnerClass(String.class), is(false)); assertThat(ClassUtils.isPrimitiveOrWrapper(Integer.class), is(true)); assertThat(ClassUtils.isPrimitiveOrWrapper(String.class), is(false)); assertThat(ClassUtils.isPrimitiveWrapper(Integer.class), is(true)); assertThat(ClassUtils.isPrimitiveWrapper(String.class), is(false)); assertThat(ClassUtils.primitivesToWrappers(new Class[] { Integer.class, Long.class }), is(not(nullValue()))); assertThat(ClassUtils.primitiveToWrapper(Integer.class), is(not(nullValue()))); assertThat(ClassUtils.toClass(1L, 2L), is(not(nullValue()))); assertThat(ClassUtils.wrappersToPrimitives(new Class[] { Integer.class, Long.class }), is(not(nullValue()))); assertThat(ClassUtils.wrapperToPrimitive(Integer.class), is(not(nullValue()))); }