Created
February 8, 2012 17:06
-
-
Save fcamblor/1771095 to your computer and use it in GitHub Desktop.
JavaPPuzzler :)
This file contains 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
Given these 5 classes, could you guess : | |
(1) What is the number of total generated classes ? | |
(2) and (3) : Can you guess what are superclasses and implemented interfaces for every generated classes in (1) ? | |
Answers are here : https://gist.github.com/1771083 |
This file contains 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.a; | |
import java.io.Serializable; | |
public enum SimpleEnum implements Serializable { | |
VALUE; | |
} |
This file contains 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.b; | |
import java.io.Serializable; | |
public enum SimpleAnonymousEnum implements Serializable { | |
VALUE(){}; | |
} |
This file contains 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.c; | |
import java.io.Serializable; | |
public class ClassWithNestedEnum implements Serializable { | |
public static enum NestedEnum implements Serializable { | |
VALUE(); | |
} | |
} |
This file contains 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.d; | |
import java.io.Serializable; | |
public class ClassWithNestedAnonymousEnum { | |
public static enum NestedAnonymousEnum implements Serializable { | |
VALUE(){}; | |
} | |
} |
This file contains 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; | |
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 Serializable ? " + Serializable.class.isAssignableFrom(clazz)); | |
// 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