Solved

java programming

Posted on 2004-10-01
15
184 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
Industry Leaders: 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
Selenium docs api java index 3 65
Java class and jar 3 42
Java array 10 57
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

756 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