[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

java programming

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
sraveend
Asked:
sraveend
  • 6
  • 6
  • 2
  • +1
2 Solutions
 
CEHJCommented:
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
 
objectsCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
sraveendAuthor Commented:
Is there any option to find a package from a field type?
0
 
CEHJCommented:
The class name of the field will give the package
0
 
sraveendAuthor Commented:
Can we declare a field as a package?
0
 
CEHJCommented:
Not sure what you mean by that
0
 
sraveendAuthor Commented:
U've given class name of the field gives the package name,i just want an example
0
 
CEHJCommented:
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
 
sraveendAuthor Commented:
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
 
CEHJCommented:
someSet.add(cName);
0
 
sraveendAuthor Commented:
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
 
sraveendAuthor Commented:
AM sorry, i've corrected my error.....
0
 
CEHJCommented:
8-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now