Results 1 to 17 of 17
- 05-13-2009, 01:47 PM #1
[newbie] getting class members from Arraylist
I'm trying to retrieve the methods of classes from objects stored within an Arraylist, is this possible?
:confused:
Output:Java Code:Employee[] staff = new Employee[3]; ... ArrayList newStaff = new ArrayList(); //fill newStaff for (Integer i = 0; i < staff.length; i++) { newStaff.add(staff[i]); } //display newStaff for (Integer i = 0; i < staff.length; i++) { System.out.println(newStaff.get(i).getClass().getName()); //hmm? }
//name of the class; I need the methods
homenetwork.bkr.training.Employee
homenetwork.bkr.training.Employee
homenetwork.bkr.training.Employee
- 05-13-2009, 02:47 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 05-13-2009, 03:08 PM #3
I looked but I didn't find anything useful...that is why I ask for help.
I tried experimenting with the .getClass.getField and getClass.getDeclaredField, but still no joy.
API is not too clear for dummies like myself either :)
- 05-13-2009, 03:11 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 05-13-2009, 03:13 PM #5
That's fine, I'll dig further. Thanks for your help.
- 05-13-2009, 03:49 PM #6
I'm trying to use the following code to read the methods of the underlying class,but I am getting the methods of java.util.ArrayList instead.
?
Java Code:package homenetwork.bkr.training; import java.util.ArrayList; import java.lang.reflect.*; public class EmployeeTest { public static void main(String[] args) { //1. create an array and create new employees Employee[] staff = new Employee[3]; staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15); staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15); ArrayList newStaff = new ArrayList(); //2a. store array staff in an ArrayList for (Integer i = 0; i < staff.length; i++) { newStaff.add(staff[i]); } /* Does not work because staff is an Employee object not a collection. newStaff.addAll((Collection) staff); */ //1. read the methods into _methods Method[] _methods = newStaff.getClass().getMethods(); //erm? //2. run the Employee.Name() method ?? //for ... ?? } }Last edited by jon80; 05-13-2009 at 04:05 PM.
- 05-13-2009, 04:22 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Knew you'd find the right method...:)
Right, you've called getClass on ArrayList, so the subsequent getMethods is on the ArrayList class...so you get those methods.Java Code:ArrayList newStaff = new ArrayList(); ... Method[] _methods = newStaff.getClass().getMethods();
If you want the methods of something contained within the ArrayList you'll have to get one of the items out first, then do the getClass().getMethods() call on that.
ETA: Or, just use one the Employee objects you already have...Last edited by Tolls; 05-13-2009 at 04:26 PM.
- 05-13-2009, 04:44 PM #8
Thanks, well the output of:
...seems to list the methods of the ArrayList, which is not what I am expecting, since I wanted the methods of the Employee class.Java Code:for (Integer i = 0; i < _methods.length; i++) { System.out.println(_methods[i].toString()); }
To be honest I wanted to create a code "template" with ArrayLists, since they are convenient to use :)
See http://javabeanz.wordpress.com/2007/...ce-tips-part-1 (although I'm sure you would be aware of these issues).
NOTE: It would be nice to have the possibilty to use <a href> </a> tags for typing in links etc.
public boolean java.util.ArrayList.add(java.lang.Object)
public void java.util.ArrayList.add(int,java.lang.Object)
public java.lang.Object java.util.ArrayList.get(int)
public java.lang.Object java.util.ArrayList.clone()
public int java.util.ArrayList.indexOf(java.lang.Object)
public void java.util.ArrayList.clear()
public boolean java.util.ArrayList.contains(java.lang.Object)
public boolean java.util.ArrayList.isEmpty()
public int java.util.ArrayList.lastIndexOf(java.lang.Object)
public boolean java.util.ArrayList.addAll(int,java.util.Collectio n)
public boolean java.util.ArrayList.addAll(java.util.Collection)
public int java.util.ArrayList.size()
public java.lang.Object[] java.util.ArrayList.toArray(java.lang.Object[])
public java.lang.Object[] java.util.ArrayList.toArray()
public boolean java.util.ArrayList.remove(java.lang.Object)
public java.lang.Object java.util.ArrayList.remove(int)
public java.lang.Object java.util.ArrayList.set(int,java.lang.Object)
public void java.util.ArrayList.ensureCapacity(int)
public void java.util.ArrayList.trimToSize()
public int java.util.AbstractList.hashCode()
public boolean java.util.AbstractList.equals(java.lang.Object)
public java.util.Iterator java.util.AbstractList.iterator()
public java.util.List java.util.AbstractList.subList(int,int)
public java.util.ListIterator java.util.AbstractList.listIterator(int)
public java.util.ListIterator java.util.AbstractList.listIterator()
public java.lang.String java.util.AbstractCollection.toString()
public boolean java.util.AbstractCollection.containsAll(java.util .Collection)
public boolean java.util.AbstractCollection.removeAll(java.util.C ollection)
public boolean java.util.AbstractCollection.retainAll(java.util.C ollection)
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()Last edited by jon80; 05-13-2009 at 05:04 PM.
- 05-13-2009, 05:04 PM #9
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Well, as Tolls said, you need to retrieve the object from the ArrayList, and THEN call the methods on it. So, the line to retrieve the methods should be
BTW: ArrayList should be parameterized because it uses Generics, so your declaration should beJava Code:/*0 can be replaced with the index of whichever object you want*/ Method[] _methods = newStaff.get(0)getClass().getMethods();
Java Code:ArrayList<Employee> newStaff = new ArrayList<Employee>(); //Instead of ArrayList newStaff = new ArrayList();
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-13-2009, 05:49 PM #10
lol, well I started reading a book about Java to grasp the basics, coz I like programming languages; I'm more familiar with .NET namespaces, but the idea of "write once, deploy anywhere" is the motivating part; besides Visual Studio 2008 / ASP.NET on .NET 3.5 has a few unpleasant easter eggs that the books don't always mention :)
The procedure now lists all the methods including those that pertain to "java.lang.Object.*". I tried blocking them out with some code unsuccessfully:
1. I still have to find a way to call this dynamically, but in any case it is not working atm.Java Code:... final String _PACKAGENAME = "package homenetwork.bkr.training"; ... if(newStaff.get(i).getClass().getPackage().toString().trim().equals(_PACKAGENAME)) _methods = newStaff.get(i).getClass().getMethods(); //NOTE does not work, excuse the repetition. I also tried using ..startsWith() at the end to no avail. ... if (newStaff.get(0).getClass().getPackage().toString().trim().startsWith(_PACKAGENAME)) _methods = newStaff.get(i).getClass().getMethods(); ...
2. Why should I parameterize the Arraylist to be bound to a specific object, when it works fine as a Generic? I did update the code of course and it works fine just the same.
3. Any idea how to call any of these methods?
- 05-14-2009, 01:11 AM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Why parameterize the ArrayList? So that if you ever go back to your code, you A: know exactly what it stores just by looking at the declaration. And B: You can't go brain-dead at some point and to add the wrong type of Object to the ArrayList. Also means that you don't have to use casts on the get() methods. As a final bonus, you won't get compiler errors, and it's a good habit to have.
As for retrieving only the methods declared by the Employee class, just use getDeclaredMethods(). It will not return inherited methods, only the methods defined by the class.
If by calling it dynamically, you mean calling it from other code without writing it out again, just create a method that prints out what you want.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
-
I'm curious why you are doing this? What's the story behind the story? Why this particular assignment?
- 05-14-2009, 02:21 PM #13
lol
training...
-
You'll likely get a lot more out of reviewing more of the basics of Java than this exercise in Reflection, something that if you think you need to use in your program at this point, you probably don't (with 99.99% certainty, trust me).training...Last edited by Fubarable; 05-14-2009 at 11:20 PM.
- 05-14-2009, 06:48 PM #15
Thanks. The documentation is a bit hard to read mostly coz there's lots of room for "check yourself", and, incomplete examples.
I would like to learn more on development for mobile phones and J2EE (for applications with the concept of thin clients) eventually.
(haven't yet decided which, but who says I have to choose :)
- 05-15-2009, 01:57 AM #16
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
So... you figured it out yet? And yes, java.lang.reflect is one of those rather useless packages in Java that are there only because it is very difficult to implement it yourself.
Let me know if you need more help :DIf the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-15-2009, 07:45 AM #17
Not really I'm stuck asking myself why the .contains does not seem to work.
Then I have to figure out how to run the methods I want :)
Java Code:.... Method[] _methods = null; ArrayList <String> _methodsFilter1 = new ArrayList<String>(); final String _PACKAGENAME = "homenetwork.bkr.training"; //TODO retrieve dynamically final short _MAX_METHODS_TO_RUN = 2; final String[] _methodsToRun = new String[_MAX_METHODS_TO_RUN]; _methodsToRun[0] = "Salary()"; //FIX: Declaration does not work _methodsToRun[1] = "Name()"; //FIX: Declaration does not work //if (debug) System.out.println("DEBUG :" + _methodsToRun[0]); //if (debug) System.out.println("DEBUG :" + _methodsToRun[1]); //1. Get the methods from employee _methods = Employee.class.getMethods(); for (Method _m : _methods) { if (debug) {System.out.println("DEBUG (1.1) - before checking for package name:" + _m.toString());} if ((_m.toString().contains(_PACKAGENAME)) && (_m.toString().contains(_methodsToRun[0])) && //FIX...doesn't evaluate to true (_m.toString().contains(_methodsToRun[1]))) //FIX...doesn't evaluate to true { if (debug) { System.out.println("DEBUG (1.2) - after checking for package name:" + _m.toString()); System.out.println("DEBUG (1.3) - after checking for package name:" + _methodsToRun[0]); System.out.println("DEBUG (1.4) - after checking for package name:" + _methodsToRun[1]); } } } //2. Run the methods which I require ....
Similar Threads
-
Newbie - Robot Class
By drasgear in forum New To JavaReplies: 2Last Post: 11-14-2008, 04:46 PM -
[SOLVED] is final class members are also final ?
By haoberoi in forum New To JavaReplies: 4Last Post: 11-10-2008, 03:01 PM -
Newbie help with ArrayList!
By oldmanhouse in forum New To JavaReplies: 2Last Post: 11-05-2008, 11:46 PM -
Doclet that prints out all members of the class
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:06 PM -
Help passing arraylist to another class
By adlb1300 in forum New To JavaReplies: 3Last Post: 11-06-2007, 08:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks