List of usage examples for org.apache.commons.lang3 ClassUtils isInnerClass
public static boolean isInnerClass(final Class<?> cls)
Is the specified class an inner class or static nested class.
From source file:org.datavec.api.transform.TransformProcess.java
private static ObjectMapper reinitializeMapperWithSubtypes(ObjectMapper mapper) { //Register concrete subtypes for JSON serialization List<Class<?>> classes = Arrays.<Class<?>>asList(Transform.class, Condition.class, Filter.class, IReducer.class); List<String> classNames = new ArrayList<>(6); for (Class<?> c : classes) classNames.add(c.getName());//from w ww.j a v a 2s. c o m // First: scan the classpath and find all instances of the 'baseClasses' classes if (subtypesClassCache == null) { List<Class<?>> interfaces = Arrays.<Class<?>>asList(Transform.class, Condition.class, Filter.class, IReducer.class); List<Class<?>> classesList = Arrays.<Class<?>>asList(); Collection<URL> urls = ClasspathHelper.forClassLoader(); List<URL> scanUrls = new ArrayList<>(); for (URL u : urls) { String path = u.getPath(); if (!path.matches(".*/jre/lib/.*jar")) { //Skip JRE/JDK JARs scanUrls.add(u); } } Reflections reflections = new Reflections( new ConfigurationBuilder().filterInputsBy(new FilterBuilder().exclude("^(?!.*\\.class$).*$") //Consider only .class files (to avoid debug messages etc. on .dlls, etc //Exclude the following: the assumption here is that no custom functionality will ever be present // under these package name prefixes. .exclude("^org.nd4j.*").exclude("^org.bytedeco.*") //JavaCPP .exclude("^com.fasterxml.*")//Jackson .exclude("^org.apache.*") //Apache commons, Spark, log4j etc .exclude("^org.projectlombok.*").exclude("^com.twelvemonkeys.*").exclude("^org.joda.*") .exclude("^org.slf4j.*").exclude("^com.google.*").exclude("^org.reflections.*") .exclude("^ch.qos.*") //Logback ).addUrls(scanUrls).setScanners(new DataVecSubTypesScanner(interfaces, classesList))); org.reflections.Store store = reflections.getStore(); Iterable<String> subtypesByName = store.getAll(DataVecSubTypesScanner.class.getSimpleName(), classNames); Set<? extends Class<?>> subtypeClasses = Sets.newHashSet(ReflectionUtils.forNames(subtypesByName)); subtypesClassCache = new HashSet<>(); for (Class<?> c : subtypeClasses) { if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) { //log.info("Skipping abstract/interface: {}",c); continue; } subtypesClassCache.add(c); } } //Second: get all currently registered subtypes for this mapper Set<Class<?>> registeredSubtypes = new HashSet<>(); for (Class<?> c : classes) { AnnotatedClass ac = AnnotatedClass.construct(c, mapper.getSerializationConfig().getAnnotationIntrospector(), null); Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac, mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector()); for (NamedType nt : types) { registeredSubtypes.add(nt.getType()); } } //Third: register all _concrete_ subtypes that are not already registered List<NamedType> toRegister = new ArrayList<>(); for (Class<?> c : subtypesClassCache) { //Check if it's concrete or abstract... if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) { //log.info("Skipping abstract/interface: {}",c); continue; } if (!registeredSubtypes.contains(c)) { String name; if (ClassUtils.isInnerClass(c)) { Class<?> c2 = c.getDeclaringClass(); name = c2.getSimpleName() + "$" + c.getSimpleName(); } else { name = c.getSimpleName(); } toRegister.add(new NamedType(c, name)); if (log.isDebugEnabled()) { for (Class<?> baseClass : classes) { if (baseClass.isAssignableFrom(c)) { log.debug("Registering class for JSON serialization: {} as subtype of {}", c.getName(), baseClass.getName()); break; } } } } } mapper.registerSubtypes(toRegister.toArray(new NamedType[toRegister.size()])); //Recreate the mapper (via copy), as mapper won't use registered subtypes after first use mapper = mapper.copy(); return mapper; }
From source file:org.deeplearning4j.nn.conf.ComputationGraphConfiguration.java
/** * Create a computation graph configuration from json * * @param json the neural net configuration from json * @return {@link org.deeplearning4j.nn.conf.ComputationGraphConfiguration} *///from w w w. ja v a 2 s . com public static ComputationGraphConfiguration fromJson(String json) { //As per MultiLayerConfiguration.fromJson() ObjectMapper mapper = NeuralNetConfiguration.mapper(); try { return mapper.readValue(json, ComputationGraphConfiguration.class); } catch (IOException e) { //No op - try again after adding new subtypes } //Try: programmatically registering JSON subtypes for GraphVertex classes. This allows users to to add custom GraphVertex // implementations without needing to manually register subtypes //First: get all registered subtypes AnnotatedClass ac = AnnotatedClass.construct(GraphVertex.class, mapper.getSerializationConfig().getAnnotationIntrospector(), null); Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac, mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector()); Set<Class<?>> registeredSubtypes = new HashSet<>(); for (NamedType nt : types) { registeredSubtypes.add(nt.getType()); } //Second: get all subtypes of GraphVertex using reflection Reflections reflections = new Reflections(); Set<Class<? extends GraphVertex>> subTypes = reflections.getSubTypesOf(GraphVertex.class); //Third: register all subtypes that are not already registered List<NamedType> toRegister = new ArrayList<>(); for (Class<? extends GraphVertex> c : subTypes) { if (!registeredSubtypes.contains(c)) { String name; if (ClassUtils.isInnerClass(c)) { Class<?> c2 = c.getDeclaringClass(); name = c2.getSimpleName() + "$" + c.getSimpleName(); } else { name = c.getSimpleName(); } toRegister.add(new NamedType(c, name)); } } mapper = NeuralNetConfiguration.reinitMapperWithSubtypes(toRegister); try { return mapper.readValue(json, ComputationGraphConfiguration.class); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.deeplearning4j.nn.conf.NeuralNetConfiguration.java
private static synchronized void registerSubtypes(ObjectMapper mapper) { //Register concrete subtypes for JSON serialization List<Class<?>> classes = Arrays.<Class<?>>asList(InputPreProcessor.class, ILossFunction.class, IActivation.class, Layer.class, GraphVertex.class, ReconstructionDistribution.class); List<String> classNames = new ArrayList<>(6); for (Class<?> c : classes) classNames.add(c.getName());/*from ww w .ja v a 2 s .c o m*/ // First: scan the classpath and find all instances of the 'baseClasses' classes if (subtypesClassCache == null) { //Check system property: String prop = System.getProperty(CUSTOM_FUNCTIONALITY); if (prop != null && !Boolean.parseBoolean(prop)) { subtypesClassCache = Collections.emptySet(); } else { List<Class<?>> interfaces = Arrays.<Class<?>>asList(InputPreProcessor.class, ILossFunction.class, IActivation.class, ReconstructionDistribution.class); List<Class<?>> classesList = Arrays.<Class<?>>asList(Layer.class, GraphVertex.class); Collection<URL> urls = ClasspathHelper.forClassLoader(); List<URL> scanUrls = new ArrayList<>(); for (URL u : urls) { String path = u.getPath(); if (!path.matches(".*/jre/lib/.*jar")) { //Skip JRE/JDK JARs scanUrls.add(u); } } Reflections reflections = new Reflections( new ConfigurationBuilder().filterInputsBy(new FilterBuilder().exclude("^(?!.*\\.class$).*$") //Consider only .class files (to avoid debug messages etc. on .dlls, etc //Exclude the following: the assumption here is that no custom functionality will ever be present // under these package name prefixes. These are all common dependencies for DL4J .exclude("^org.nd4j.*").exclude("^org.datavec.*").exclude("^org.bytedeco.*") //JavaCPP .exclude("^com.fasterxml.*")//Jackson .exclude("^org.apache.*") //Apache commons, Spark, log4j etc .exclude("^org.projectlombok.*").exclude("^com.twelvemonkeys.*") .exclude("^org.joda.*").exclude("^org.slf4j.*").exclude("^com.google.*") .exclude("^org.reflections.*").exclude("^ch.qos.*") //Logback ).addUrls(scanUrls).setScanners(new DL4JSubTypesScanner(interfaces, classesList))); org.reflections.Store store = reflections.getStore(); Iterable<String> subtypesByName = store.getAll(DL4JSubTypesScanner.class.getSimpleName(), classNames); Set<? extends Class<?>> subtypeClasses = Sets.newHashSet(ReflectionUtils.forNames(subtypesByName)); subtypesClassCache = new HashSet<>(); for (Class<?> c : subtypeClasses) { if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) { //log.info("Skipping abstract/interface: {}",c); continue; } subtypesClassCache.add(c); } } } //Second: get all currently registered subtypes for this mapper Set<Class<?>> registeredSubtypes = new HashSet<>(); for (Class<?> c : classes) { AnnotatedClass ac = AnnotatedClass.construct(c, mapper.getSerializationConfig().getAnnotationIntrospector(), null); Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac, mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector()); for (NamedType nt : types) { registeredSubtypes.add(nt.getType()); } } //Third: register all _concrete_ subtypes that are not already registered List<NamedType> toRegister = new ArrayList<>(); for (Class<?> c : subtypesClassCache) { //Check if it's concrete or abstract... if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) { //log.info("Skipping abstract/interface: {}",c); continue; } if (!registeredSubtypes.contains(c)) { String name; if (ClassUtils.isInnerClass(c)) { Class<?> c2 = c.getDeclaringClass(); name = c2.getSimpleName() + "$" + c.getSimpleName(); } else { name = c.getSimpleName(); } toRegister.add(new NamedType(c, name)); if (log.isDebugEnabled()) { for (Class<?> baseClass : classes) { if (baseClass.isAssignableFrom(c)) { log.debug("Registering class for JSON serialization: {} as subtype of {}", c.getName(), baseClass.getName()); break; } } } } } mapper.registerSubtypes(toRegister.toArray(new NamedType[toRegister.size()])); }
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 w w . 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()))); }