Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Sapphire » Working with Java Types
Working with Java Types [message #1021050] Tue, 19 March 2013 12:41 Go to next message
Harsha G is currently offline Harsha GFriend
Messages: 11
Registered: February 2013
Junior Member
Hi,

I was wondering if anyone has used Sapphire dealing with Java Types and in particularly with Java Reflection Utilities.

I have implemented a Java Type picker and i have to retrieve the fields available in that particular selected Java Class and pull that out onto the XML as attributes. Screenshot attached !

index.php/fa/13918/0/


@Type( base = JavaTypeName.class )
    @Reference( target = JavaType.class)
    @MustExist
    @Required
    @XmlBinding( path = "@name" )
    @Label( standard = "& Module Name" )

    ValueProperty PROP_MODULE_NAME = new ValueProperty( TYPE, "ModuleName" );
    ReferenceValue<JavaTypeName,JavaType> getModuleName();
  

    void setModuleName( String value );
    void setModuleName( JavaTypeName value );


Now i want to run Reflection Utils like getDeclaredFields() on the Java Class selected from this window. To get this done i am making use of a service which extends PossibleValuesService .

This custom service class has the Java Reflection code and in this segment i need the class which i have picked earlier to pass a reference to this.
 Class c = Class.forName(ClsName);
    		
           
             System.out.println("Class Name :"+c.getName()+"\n");
             Field allFieldArray[] = c.getDeclaredFields();
          
             int size = allFieldArray.length;
             for(int i = 0 ; i < size ; i++)
              {
                 System.out.println("Field ( "+i+" ) :"+allFieldArray[i]);
                 values.add(allFieldArray[i].toString());
                
              }

I am passing the ClsName ( Java String ) of the earlier selected Class using the getModuleName method of the model, for which it says class not found. This is problem number one.

Also is there a way to get hold of the class reference directly along with just the string name of the class so that i can pass it to the reflection utils directly as an object.

Any lights shed on this would be helpful !

Thanks in Advance,

Harsha
  • Attachment: Sapphire.jpg
    (Size: 233.88KB, Downloaded 886 times)
Re: Working with Java Types [message #1021171 is a reply to message #1021050] Tue, 19 March 2013 15:48 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
If you call element.getModuleName().resolve(), you will get a JavaType object (unless type is not found). From a JavaType, you can call artifact() to get a Class object. Note that artifact() will return null if the referenced type is a Java source file in a project. For those types, there is no Class object in existence and you have to use JDT's type API instead of reflection. The JavaType class does provide some methods for accessing type metadata without differentiating between Java reflection and JDT, but there is no API today to get at the fields. I am not opposed to expanding JavaType API if there is a need.

- Konstantin
Re: Working with Java Types [message #1021557 is a reply to message #1021171] Wed, 20 March 2013 10:09 Go to previous messageGo to next message
Harsha G is currently offline Harsha GFriend
Messages: 11
Registered: February 2013
Junior Member
Hi Konstatin, Thanks for the tip, appreciate it.

I was scratching my head on getting NPE from the JavaType object until i read your line which says you will get null from a java source file after reflection. Irony is that i only have access to the source files ! I have to work around that !

That points me to the next part , is there a way to specify a path to the Java Type picker (the browse button) to choose the class files from?
Or may be some filter which lets you select class files from a specified package (something similar to the @JavaTypeConstraint functionality).

Regards,
Harsha
Re: Working with Java Types [message #1021820 is a reply to message #1021557] Wed, 20 March 2013 18:28 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
The JavaType object you get has a reference to JDT's IType which can be used similarly to reflection, but it doesn't make IType accessible. If this would be useful, please open an enhancement request.

The Java type picker and other Java type handling functionality (validation, jump to, etc.) all operate in the Java context relative to the file that your model is editing. You will see types from the project the file is in as well as types from everything that the project references. There is no facility to define other contexts.

- Konstantin
Re: Working with Java Types [message #1022580 is a reply to message #1021820] Fri, 22 March 2013 07:00 Go to previous messageGo to next message
Harsha G is currently offline Harsha GFriend
Messages: 11
Registered: February 2013
Junior Member
Oh okay okay !

But what are my options if i dont have to deal with JDT Type API and to go ahead with Reflection ?

Also, I have problems loading any class file using class.forName(clsName). Its not resolving . Tried it with the 3 parameters forName, it ain't working.

Do you think the problem also lies with the class loaders since the launched Eclipse RCP app points its classpath to somewhere inside the Eclipse / Plugins directory and not to the project specific workspace where i intend it to be !


For examples, If i search the classpath it is pointing to
C:/Eclipse%20Plugin%20DE/eclipse-rcp-juno-SR1-win32/eclipse/plugins/org.eclipse.equinox.launcher_1.3.0.v20120522-1813.jar,

whereas the classes which i need to select are under
C:/MyLocalPC/TestProject/Workspace/NewCodeGenerator/bin/


Obviously my class.forName is failing, i dont know how to come out of this.

[Updated on: Fri, 22 March 2013 09:19]

Report message to a moderator

Re: Working with Java Types [message #1022825 is a reply to message #1022580] Fri, 22 March 2013 15:49 Go to previous message
Konstantin Komissarchik is currently offline Konstantin KomissarchikFriend
Messages: 1077
Registered: July 2009
Senior Member
There is no class loader with your project's classes when in development, as such you cannot use Java Reflection and have to use JDT's API if you need to inspect the types. Your project classes are only in a class loader when you are running it.

- Konstantin
Previous Topic:Implementing a list of ReferenceValues?
Next Topic:How to enable/disable Action Button
Goto Forum:
  


Current Time: Wed Apr 24 22:30:01 GMT 2024

Powered by FUDForum. Page generated in 0.03088 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top