Results 1 to 6 of 6
- 06-24-2011, 11:52 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
Finding and Creating Instance of Every Class in Package
I've been trying to understand how one would go about generically creating an object of every single class in a package.
After some searching I found this code posted on the internet:
Java Code:private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList<Class> classes = new ArrayList<Class>(); for (File directory : dirs) { classes.addAll(findClasses(directory, packageName)); } return classes.toArray(new Class[classes.size()]); }It seemed pretty promising, but after continuous attempts to achieve my goal, I ultimately found no solution.Java Code:private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException { List<Class> classes = new ArrayList<Class>(); if (!directory.exists()) { return classes; } File[] files = directory.listFiles(); for (File file : files) { if (file.isDirectory()) { assert !file.getName().contains("."); classes.addAll(findClasses(file, packageName + "." + file.getName())); } else if (file.getName().endsWith(".class")) { classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6))); } } return classes; }
This snippet of code was what I had come up with, but never actually worked. I haven't found a single thing on the internet that would make me believe this is possible as it seems you still have to know the class of the object when you declare it -- and that just sounds like a really complicated way to declare an object.Java Code:Class[] classes = getClasses("com.gmail.jtripled.Test.Listeners"); for(Class cl : classes) { Constructor con = cl.getConstructor(Test.class); Object newInstance = con.newInstance(this); }
Inside the package are the classes:
com.gmail.jtripled.Test.Listeners.PlayerListener.c lass
com.gmail.jtripled.Test.Listeners.EntityListener.c lass
They are for the game Minecraft where I'm trying to develop a plugin -- not that that's really important, but just mentioning that they don't have some main function or anything. That previously posted attempt at instantiating each of the classes does take place in the method called on the enabling of said plugin.
Sorry if I'm not clear enough, my Java vocabulary isn't the best, and if you need more explanation or code to look at I'd be glad to post it!
- 06-24-2011, 01:17 PM #2
how one would go about generically creating an object of every single class in a package.If the class files are all in a folder, then you can get the contents of that folder.you still have to know the class of the object when you declare it
What was the problem with that approach?after continuous attempts to achieve my goal, I ultimately found no solution.
A problem could be if the classes required arguments for its constructor.
- 06-24-2011, 01:36 PM #3
... or if the class is abstract, or if the .class file represents an interface or enum ...
db
- 06-24-2011, 10:37 PM #4
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
None of the classes are abstract, or interfaces, etc. They are simply classes.
My problem is when I declare it, normally I would just say:
private APlayerListener playerListener;
...
playerListener = new APlayerListener(this);
But suppose I just didn't know that bold part. How do I generically replace that?
- 06-24-2011, 10:42 PM #5
I'm not sure you do it generically. Generics are a compiler thing to help with typing.
I thought you wanted to do something at execution time. That would be with reflection.
- 06-25-2011, 08:34 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
All you can do is obtain the Class object:
You can even get a new object given the yourClass object, but only if the class at hand has a no-arg constructor:Java Code:Class yourClass= Class.forName("APlayerListener");
The type of yourObject is APlayerListener but you don't know that (as you have written yourself). Read the API documentation and see what you can do with a Class object; it'll lead you to the reflection functionality which is a can of slimey worms if you're not prepared for it ...Java Code:Object yourObject= yourClass.newInstance();
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Help with creating an instance of a class extension
By Inferno719 in forum New To JavaReplies: 19Last Post: 05-04-2011, 03:53 AM -
Instance of GNUPlot object of the package JavaPlot
By Übermenschen in forum Advanced JavaReplies: 2Last Post: 03-30-2010, 06:51 PM -
get java.lang.NullPointerException while creating an instance
By Basit56 in forum New To JavaReplies: 10Last Post: 01-06-2010, 08:33 AM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM -
Class Reflection: Finding class modifiers
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks