Created
February 8, 2012 16:29
-
-
Save fcamblor/1770855 to your computer and use it in GitHub Desktop.
Full JavaPPuzzler class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.puzzlers; | |
import org.springframework.core.io.Resource; | |
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; | |
import org.springframework.core.io.support.ResourcePatternResolver; | |
import org.springframework.core.type.classreading.CachingMetadataReaderFactory; | |
import org.springframework.core.type.classreading.MetadataReader; | |
import org.springframework.core.type.classreading.MetadataReaderFactory; | |
import org.springframework.util.ClassUtils; | |
import org.springframework.util.SystemPropertyUtils; | |
import java.io.IOException; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* @author fcamblor | |
*/ | |
public class JavaPPuzzler implements Serializable { | |
public static void main(String[] args) throws ClassNotFoundException, IOException { | |
List<Class> classesInPuzzlerPkg = findTypesInPackage("com.puzzlers"); | |
// Guessing how many classes are there in package com.puzzlers ? | |
System.out.println(classesInPuzzlerPkg.size()); // Answer is in (1) | |
System.out.println(String.format("%n%n%n")); | |
// Now, displaying every superclass for every retrieved classes | |
for(Class clazz : classesInPuzzlerPkg){ | |
System.out.println(clazz.getName() + " has superclass : " + clazz.getSuperclass()); | |
// and compare it with what javap displays ... :) (2) | |
} | |
System.out.println(String.format("%n%n%n")); | |
for(Class clazz : classesInPuzzlerPkg){ | |
System.out.println(clazz.getName() + " implements interfaces : " + Arrays.toString(clazz.getInterfaces())); | |
// and compare it with what javap displays ... :) (3) | |
} | |
} | |
/** | |
* Retrieving types in given package... 'works fine, puzzler doesn't reside here ;) | |
*/ | |
private static List<Class> findTypesInPackage(String basePackage) throws IOException, ClassNotFoundException { | |
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); | |
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); | |
List<Class> candidates = new ArrayList<Class>(); | |
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + | |
resolveBasePackage(basePackage) + "/" + "**/*.class"; | |
Resource[] resources = resourcePatternResolver.getResources(packageSearchPath); | |
for (Resource resource : resources) { | |
if (resource.isReadable()) { | |
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource); | |
//if (isCandidate(metadataReader)) { | |
candidates.add(Class.forName(metadataReader.getClassMetadata().getClassName())); | |
//} | |
} | |
} | |
return candidates; | |
} | |
private static String resolveBasePackage(String basePackage) { | |
return ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(basePackage)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment