Solved

java programming

Posted on 2004-10-01
15
180 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
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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:
mayankeagle earned 400 total points
Comment Utility
>> 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
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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
Comment Utility
Is there any option to find a package from a field type?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
The class name of the field will give the package
0
 

Author Comment

by:sraveend
Comment Utility
Can we declare a field as a package?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Not sure what you mean by that
0
 

Author Comment

by:sraveend
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
someSet.add(cName);
0
 

Author Comment

by:sraveend
Comment Utility
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
Comment Utility
AM sorry, i've corrected my error.....
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 video teaches viewers about errors in exception handling.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now