?
Solved

java programming

Posted on 2004-10-01
15
Medium Priority
?
187 Views
Last Modified: 2010-03-31
Class cc=Class.forName(className);
Field[] ff=cc.getDeclaredFields();

for(int i=0;i<ff.length;i++)
         {

                    Class type =ff[i].getType();
                    
                    if(type.isPrimitive()==false)
                    {
                      Class treeset =Class.forName(ff[i]);
                            }
 }
}
Iam getting error from the above code,how to retrieve non-primitive field ,class and store in a class treeset?
0
Comment
Question by:sraveend
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12204400
You should be OK

            String className = "java.lang.String";
            Class cc = Class.forName(className);
            Field[] ff = cc.getDeclaredFields();

            for (int i = 0; i < ff.length; i++) {

                  Class type = ff[i].getType();

                  if (type.isPrimitive() == false) {
                        //Class treeset = Class.forName(ff[i]);
                        System.out.println("Found " + type);
                  }
            }

0
 
LVL 92

Expert Comment

by:objects
ID: 12204415
Class.forName() expects a String, and not a Field instance.
and you don't need to call Class.forName() as you alreay have the Class instance 'type'.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 1600 total points
ID: 12206511
>> Class treeset =Class.forName(ff[i]);

I think you want to do:

TreeSet trList = new TreeSet () ;
Class cc = Class.forName ( className ) ;
Field[] ff = cc.getDeclaredFields () ;

for ( int i = 0 ; i < ff.length ; i ++)
{
  Class type = ff[i].getType () ;

  if ( type.isPrimitive () == false )
    trList.add ( ff[i] ) ; // end if

} // end for
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 12206514
You can then display them using:

Iterator itTreeItems = trList.iterator () ;

while ( itTreeItems.hasNext () )
{
  Field f = ( Field ) itTreeItems.next () ;
  System.out.println ( f.getName () ) ;

} // end while
0
 

Author Comment

by:sraveend
ID: 12206849
Is there any option to find a package from a field type?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12206855
The class name of the field will give the package
0
 

Author Comment

by:sraveend
ID: 12206860
Can we declare a field as a package?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12206884
Not sure what you mean by that
0
 

Author Comment

by:sraveend
ID: 12206904
U've given class name of the field gives the package name,i just want an example
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 12206919
while ( itTreeItems.hasNext () )
{
  Field f = ( Field ) itTreeItems.next () ;
  System.out.println ( f.getName () ) ;
  String fqName = f.getDeclaringClass().toString();
  String packageName = fqName.substring(0, fqName.lastIndexOf("."));
  System.out.println (packageName) ;
} // end while
0
 

Author Comment

by:sraveend
ID: 12207000
Class cc=Class.forName(className);
Field[] ff=cc.getDeclaredFields();

for(int i=0;i<ff.length;i++)
         {

                    Class type =ff[i].getType();


                    if(type.isPrimitive()==false)
                    {
                    String fqName = type.toString();
                    int x=fqName.length();
                      String cName = fqName.substring(fqName.lastIndexOf(".")+1,x);
                      System.out.println("class name="+cName);

Class tset=Class.forName(TreeSet);
Field[] f=tset.set(String,cName);

                      String packageName = fqName.substring(0, fqName.lastIndexOf("."));
                      System.out.println("packagename="+packageName);

                           }
 }

Iam getting correct result for cName&packageName
But how to store class name (cName) in a class TreeSet.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12207044
someSet.add(cName);
0
 

Author Comment

by:sraveend
ID: 12207088
Class cc=Class.forName(className);
Field[] ff=cc.getDeclaredFields();
TreeSet trList = new TreeSet();
for(int i=0;i<ff.length;i++)
         {

                    Class type =ff[i].getType();


                    if(type.isPrimitive()==false)
                    {
                    String fqName = type.toString();
                    int x=fqName.length();
                      String cName = fqName.substring(fqName.lastIndexOf(".")+1,x);
                      System.out.println("class name="+cName);

//Class tset=trList.class;
trList.add(cName);

                      String packageName = fqName.substring(0, fqName.lastIndexOf("."));
                      System.out.println("packagename="+packageName);

                           }
 }
Iam getting error in add?
0
 

Author Comment

by:sraveend
ID: 12208738
AM sorry, i've corrected my error.....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12208852
8-)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question