Class object from sources of an project

Hi,
I try to develop my first intellij idea plugin.
Now I need the compiled class of an source in the project.
Something like :

Project

com/company/MyClass.java

Plugin {


  ClassLoader cl = project.getClassLoader();
  for( Class c =cl.loadClass( "com.company.MyClass" ); c != null; c = c.getSuperClass() )
       System.out.println( c );
}

Is this possible ?

Thanx,
André

6 comments
Comment actions Permalink

Hello Andre,

What are you trying to accomplish? Usually it's not a good approach to load
the compiled classes of your application into IDEA's classloader.

Hi,
I try to develop my first intellij idea plugin.
Now I need the compiled class of an source in the project.
Something like :
Project

com/company/MyClass.java

Plugin {
+
+
+  ClassLoader cl = project.getClassLoader();+
+  for( Class c =cl.loadClass( "com.company.MyClass" ); c != null; c =
c.getSuperClass() )+
+       System.out.println( c );+
}
Is this possible ?

Thanx,
André
---
Original message URL:
http://www.jetbrains.net/devnet/message/5255841#5255841

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Hi Dmitry,

thanks for reply.

I try to find out the class hierarchy.

The plugin I develop shows an preview of gui layout defined in xml. Every xml belongs to an class. The translations are stored In properties files. So it is possible to override information in properties and xml in and child class.

The parser needs the class and the xml as input to render the layout preview.

Currently my code looks like

                URL buildRoot =

                        new File( project.getBaseDir().getPath() + "/out/production/" + project.getName() ).toURI().toURL();

                URL srcRoot =
                        new File( project.getBaseDir().getPath() + "/src" ).toURI().toURL();

                URLClassLoader cl = new URLClassLoader( new URL[]{ srcRoot, buildRoot }, null );

                Class<?> c = cl.loadClass( tmp );


The problems are
1 ) there can be more than on src path, how will i know the src paths ?
2 ) same with the out path.
3 ) i need the classpath of the project, to include in the URLClassLoader, how will i know the classpath ?

0
Comment actions Permalink

Hello Andre,

You don't need access to compiled classes in order to find out the class
hierarchy. See PsiClass.getSuperClass() in the OpenAPI.

Hi Dmitry,

thanks for reply.

I try to find out the class hierarchy.

The plugin I develop shows an preview of gui layout defined in xml.
Every xml belongs to an class. The translations are stored In
properties files. So it is possible to override information in
properties and xml in and child class.

The parser needs the class and the xml as input to render the layout
preview.

Currently my code looks like

URL buildRoot =
new File( project.getBaseDir().getPath() +
"/out/production/" + project.getName() ).toURI().toURL();
URL srcRoot =
new File( project.getBaseDir().getPath() +
"/src" ).toURI().toURL();
URLClassLoader cl = new URLClassLoader( new URL[]{
srcRoot, buildRoot }, null );

Class<?> c = cl.loadClass( tmp );

The problems are

1 ) there can be more than on src path, how will i know the src paths
?

2 ) same with the out path.

3 ) i need the classpath of the project, to include in the
URLClassLoader, how will i know the classpath ?

---
Original message URL:
http://www.jetbrains.net/devnet/message/5255845#5255845

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

He,

ok psi looks interesting.
To get an PsiClass instance I using

                    PsiJavaFile j = (PsiJavaFile) PsiManager.getInstance( project ).findFile( file );
                    for( PsiClass c : j.getClasses() ) {
                         find the right
                    }

Is there an way more simple to get an PsiClass, like

                    PsiManager.getInstance( project ).findClass( "com.company." );

Thanks,
André

0
Comment actions Permalink

Hello Andre,

JavaPsiFacade.getInstance(project).findClass(String qualifiedName)

He,

ok psi looks interesting.
To get an PsiClass instance I using
PsiJavaFile j = (PsiJavaFile)
PsiManager.getInstance( project ).findFile( file );
for( PsiClass c : j.getClasses() ) {
find the right
}
Is there an way more simple to get an PsiClass, like

PsiManager.getInstance( project ).findClass(
"com.company." );

Thanks,
André
---
Original message URL:
http://www.jetbrains.net/devnet/message/5255884#5255884

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Ah I see,

thanks a lot !!

0

Please sign in to leave a comment.