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é
Please sign in to leave a comment.
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.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
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 ?
Hello Andre,
You don't need access to compiled classes in order to find out the class
hierarchy. See PsiClass.getSuperClass() in the OpenAPI.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
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é
Hello Andre,
JavaPsiFacade.getInstance(project).findClass(String qualifiedName)
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Ah I see,
thanks a lot !!