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:de.metanome.backend.algorithm_loading.AlgorithmFinder.java
/** * Finds out which subclass of Algorithm is implemented by the source code in the * algorithmJarFile.//from w w w . j av a 2 s. c o m * * @param algorithmJarFile the algorithm's jar file * @return the interfaces of the algorithm implementation in algorithmJarFile * @throws java.io.IOException if the algorithm jar file could not be opened * @throws java.lang.ClassNotFoundException if the algorithm contains a not supported interface */ public Set<Class<?>> getAlgorithmInterfaces(File algorithmJarFile) throws IOException, ClassNotFoundException { JarFile jar = new JarFile(algorithmJarFile); Manifest man = jar.getManifest(); Attributes attr = man.getMainAttributes(); String className = attr.getValue(bootstrapClassTagName); URL[] url = { algorithmJarFile.toURI().toURL() }; ClassLoader loader = new URLClassLoader(url, Algorithm.class.getClassLoader()); Class<?> algorithmClass; try { algorithmClass = Class.forName(className, false, loader); } catch (ClassNotFoundException e) { System.out.println("Could not find class " + className); return new HashSet<>(); } finally { jar.close(); } return new HashSet<>(ClassUtils.getAllInterfaces(algorithmClass)); }
From source file:com.link_intersystems.lang.ContextAware.java
/** * Creates a java proxy that executes every call to the target object within * the context that this {@link ContextAware} defines (defined by * subclasses)./*w w w . j a va 2s .c o m*/ * * @param targetObject * the target object to create a proxy for. * @return a java proxy that executes every call to the target object's * interfaces in this {@link ClassLoaderContextAware}. */ @SuppressWarnings("unchecked") public <T> T createContextProxy(T targetObject) { Assert.notNull("targetObject", targetObject); Class<T> targetObjectClass = (Class<T>) targetObject.getClass(); List<Class<?>> allInterfaces = ClassUtils.getAllInterfaces(targetObjectClass); if (allInterfaces.isEmpty()) { throw new IllegalArgumentException("Unable to create java proxy, because target object's " + targetObjectClass + " does not implement any interface."); } Class<?>[] allInterfacesAsArray = (Class<?>[]) allInterfaces.toArray(new Class<?>[allInterfaces.size()]); ClassLoader classLoader = targetObject.getClass().getClassLoader(); ContextAwareInvocationHandler classLoaderContextInvocationHandler = new ContextAwareInvocationHandler(this, targetObject); T classLoaderContextAwareProxy = (T) Proxy.newProxyInstance(classLoader, allInterfacesAsArray, classLoaderContextInvocationHandler); return classLoaderContextAwareProxy; }
From source file:net.mindengine.blogix.Blogix.java
private boolean implementsInterface(Class<?> clazz, Class<Markup> interfaceClass) { return ClassUtils.getAllInterfaces(clazz).contains(interfaceClass); }
From source file:de.tolina.common.validation.AnnotationValidation.java
/** * Calls dependent on the type of the given Object: * <br> - {@link AnnotationUtils#getAnnotations(Method)} or * <br> - {@link AnnotationUtils#getAnnotations(java.lang.reflect.AnnotatedElement)} */// w w w . j av a 2s.c om @Nullable private Annotation[] getAllAnnotationsFor(@Nonnull final Object annotated) { if (annotated instanceof Field) { return getAnnotations((Field) annotated); } if (annotated instanceof Method) { final Method annotatedMethod = (Method) annotated; final Class<?> declaringClass = annotatedMethod.getDeclaringClass(); final List<Class<?>> allClasses = new ArrayList<>(); allClasses.add(declaringClass); allClasses.addAll(ClassUtils.getAllSuperclasses(declaringClass)); final ArrayList<Annotation> allAnnotations = new ArrayList<>(); for (final Class<?> aClass : allClasses) { final ArrayList<Method> allMethods = new ArrayList<>(); allMethods.addAll(Arrays.asList(aClass.getDeclaredMethods())); final List<Class<?>> interfaces = ClassUtils.getAllInterfaces(aClass); for (final Class<?> anInterface : interfaces) { allMethods.addAll(Arrays.asList(anInterface.getDeclaredMethods())); } allMethods.stream().filter(method -> isSameMethod(method, annotatedMethod)) .forEachOrdered(method -> addIfNotPresent(allAnnotations, getAnnotations(method))); } return allAnnotations.toArray(new Annotation[] {}); } final Class<?> annotatedClass = (Class<?>) annotated; final List<Class<?>> allClasses = new ArrayList<>(); allClasses.add(annotatedClass); allClasses.addAll(ClassUtils.getAllSuperclasses(annotatedClass)); final ArrayList<Annotation> allAnnotations = new ArrayList<>(); for (final Class<?> aClass : allClasses) { addIfNotPresent(allAnnotations, getAnnotations(aClass)); final List<Class<?>> interfaces = ClassUtils.getAllInterfaces(aClass); for (final Class<?> anInterface : interfaces) { addIfNotPresent(allAnnotations, getAnnotations(anInterface)); } } return allAnnotations.toArray(new Annotation[] {}); }
From source file:io.github.moosbusch.lumpi.util.LumpiUtil.java
public static Set<Class<?>> getSuperTypes(Class<?> type, boolean includeType, boolean includeSuperClasses, boolean includeInterfaces) { Set<Class<?>> result = new LinkedHashSet<>(); List<Class<?>> interfaces = ClassUtils.getAllInterfaces(type); List<Class<?>> superClasses = ClassUtils.getAllSuperclasses(type); if (includeType) { result.add(type);//from w w w . j a v a2s. c om } if (includeSuperClasses) { result.addAll(superClasses); } if (includeInterfaces) { result.addAll(interfaces); } return result; }
From source file:org.apache.brooklyn.core.catalog.internal.CatalogClasspathDo.java
/** augments the given item with annotations and class data for the given class, then adds to catalog * @deprecated since 0.7.0 the classpath DO is replaced by libraries */ @Deprecated/*from www .j ava 2 s. c o m*/ public CatalogItem<?, ?> addCatalogEntry(CatalogItemDtoAbstract<?, ?> item, Class<?> c) { Catalog catalogAnnotation = c.getAnnotation(Catalog.class); item.setSymbolicName(c.getName()); item.setJavaType(c.getName()); item.setDisplayName(firstNonEmpty(c.getSimpleName(), c.getName())); if (catalogAnnotation != null) { item.setDisplayName(firstNonEmpty(catalogAnnotation.name(), item.getDisplayName())); item.setDescription(firstNonEmpty(catalogAnnotation.description())); item.setIconUrl(firstNonEmpty(catalogAnnotation.iconUrl())); } if (item instanceof CatalogEntityItemDto || item instanceof CatalogTemplateItemDto) { item.tags().addTag(BrooklynTags.newTraitsTag(ClassUtils.getAllInterfaces(c))); } if (log.isTraceEnabled()) log.trace("adding to catalog: " + c + " (from catalog " + catalog + ")"); catalog.addEntry(item); return item; }
From source file:org.apache.brooklyn.core.catalog.internal.CatalogDtoTest.java
public CatalogDto buildExampleCatalog() { CatalogDo root = new CatalogDo(managementContext, CatalogDto.newNamedInstance("My Local Catalog", "My favourite local settings, including remote catalogs -- intended partly as a teaching " + "example for what can be expressed, and how", "contents-built-in-test")); root.setClasspathScanForEntities(CatalogScanningModes.NONE); String bundleUrl = MavenRetriever .localUrl(BrooklynMavenArtifacts.artifact("", "brooklyn-core", "jar", "tests")); CatalogDo testEntitiesJavaCatalog = new CatalogDo(managementContext, CatalogDto.newNamedInstance("Test Entities from Java", null, "test-java")); testEntitiesJavaCatalog.setClasspathScanForEntities(CatalogScanningModes.NONE); testEntitiesJavaCatalog.addToClasspath(bundleUrl); testEntitiesJavaCatalog.addEntry(CatalogItemBuilder .newTemplate(TestApplication.class.getCanonicalName(), BasicBrooklynCatalog.NO_VERSION) .displayName("Test App from JAR").javaType(TestApplication.class.getCanonicalName()) .tag(BrooklynTags.newNotesTag("Some notes for catalog testing")) .tag(BrooklynTags.newYamlSpecTag("This is the spec for a test catalog item")) .tag(BrooklynTags.newTraitsTag(ClassUtils.getAllInterfaces(TestApplication.class))).build()); testEntitiesJavaCatalog.addEntry(/*from w ww . j a v a 2 s. c om*/ CatalogItemBuilder.newEntity(TestEntity.class.getCanonicalName(), BasicBrooklynCatalog.NO_VERSION) .displayName("Test Entity from JAR").javaType(TestEntity.class.getCanonicalName()).build()); root.addCatalog(testEntitiesJavaCatalog.dto); CatalogDo testEntitiesJavaCatalogScanning = new CatalogDo(managementContext, CatalogDto.newNamedInstance("Test Entities from Java Scanning", null, "test-java-scan")); testEntitiesJavaCatalogScanning.addToClasspath(bundleUrl); testEntitiesJavaCatalogScanning.setClasspathScanForEntities(CatalogScanningModes.ANNOTATIONS); root.addCatalog(testEntitiesJavaCatalogScanning.dto); CatalogDo osgiCatalog = new CatalogDo(managementContext, CatalogDto.newNamedInstance("Test Entities from OSGi", "A catalog whose entries define their libraries as a list of OSGi bundles", "test-osgi-defined")); osgiCatalog.setClasspathScanForEntities(CatalogScanningModes.NONE); CatalogEntityItemDto osgiEntity = CatalogItemBuilder .newEntity(TestEntity.class.getCanonicalName(), "Test Entity from OSGi") // NB: this is not actually an OSGi bundle, but it's okay as we don't instantiate the bundles ahead of time (currently) .libraries(ImmutableList.<CatalogBundle>of(new CatalogBundleDto(null, null, bundleUrl))).build(); testEntitiesJavaCatalog.addEntry(osgiEntity); root.addCatalog(osgiCatalog.dto); root.addCatalog(CatalogDto.newLinkedInstance("classpath://brooklyn-catalog-empty.xml")); return root.dto; }
From source file:org.apache.jena.permissions.impl.ItemHolder.java
/** * Creates the proxy, saves it as the securedItem and returns it. * //from ww w. ja va 2 s . c o m * @param handler * The SecuredItemInvoker to create the proxy with. * @return The proxy. */ @SuppressWarnings("unchecked") public final Secured setSecuredItem(final SecuredItemInvoker handler) { final Set<Class<?>> ifac = new LinkedHashSet<>(); if (baseItem.getClass().isInterface()) { ifac.add(baseItem.getClass()); } ifac.addAll(ClassUtils.getAllInterfaces(baseItem.getClass())); if (handler.securedItem.getClass().isInterface()) { ifac.add(handler.securedItem.getClass()); } ifac.addAll(ClassUtils.getAllInterfaces(handler.securedItem.getClass())); securedItem = (Secured) Proxy.newProxyInstance(SecuredItemImpl.class.getClassLoader(), ifac.toArray(new Class<?>[ifac.size()]), handler); return securedItem; }
From source file:org.apache.nifi.authorization.AccessPolicyProviderFactory.java
public static AccessPolicyProvider withNarLoader(final AccessPolicyProvider baseAccessPolicyProvider, final ClassLoader classLoader) { final AccessPolicyProviderInvocationHandler invocationHandler = new AccessPolicyProviderInvocationHandler( baseAccessPolicyProvider, classLoader); // extract all interfaces... baseAccessPolicyProvider is non null so getAllInterfaces is non null final List<Class<?>> interfaceList = ClassUtils.getAllInterfaces(baseAccessPolicyProvider.getClass()); final Class<?>[] interfaces = interfaceList.toArray(new Class<?>[interfaceList.size()]); return (AccessPolicyProvider) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler); }
From source file:org.apache.nifi.authorization.AuthorizerFactory.java
/** * Decorates the base authorizer to ensure the nar context classloader is used when invoking the underlying methods. * * @param baseAuthorizer base authorizer * @return authorizer/*from w w w .j a v a 2s. c o m*/ */ public static Authorizer withNarLoader(final Authorizer baseAuthorizer, final ClassLoader classLoader) { final AuthorizerInvocationHandler invocationHandler = new AuthorizerInvocationHandler(baseAuthorizer, classLoader); // extract all interfaces... baseAuthorizer is non null so getAllInterfaces is non null final List<Class<?>> interfaceList = ClassUtils.getAllInterfaces(baseAuthorizer.getClass()); final Class<?>[] interfaces = interfaceList.toArray(new Class<?>[interfaceList.size()]); return (Authorizer) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler); }