Solved

java programming

Posted on 2004-10-01
15
186 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 400 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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 100 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

688 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