java.lang.Class for current file in editor

How can I get it if it exists? Currently I'm trying to create an
URLClassloader with the module's classpath and loading the class that way
but it isn't working. Is there a way using psi?


7 comments

Please, someone throw me a bone here.

"Brad" <none@nowhere.org> wrote in message
news:c34k5t$njo$1@is.intellij.net...

How can I get it if it exists? Currently I'm trying to create an
URLClassloader with the module's classpath and loading the class that way
but it isn't working. Is there a way using psi?

>
>


0

Hi,

PsiClass is the same meaning object for IDEA (during editing) as
java.lang.Class for java VM (during running) and should provide
reflection on fields/methods/etc.
I wonder what kind of other stuff you are going to use with
java.lang.Class.

Given that you need to get PsiClass for active editor and have a
'project' instance this looks like following:

final Editor selectedEditor =
FileEditorManager.getInstance(project).getSelectedTextEditor();
final PsiFile file =
PsiDocumentManager.getInstance(project).getPsiFile(selectedEditor.getDocument())
;

if (psifile instanceof PsiJavaFile) {
PsiClass[] clazzes = ((PsiJavaFile)psifile).getClasses();
if (clazzes.length > 0) {
// note, there could be more then one class in the file
// one needs to find public class or class with name = name of
// file w/o extension
// so adjust the index accordingly
System.out.println( clazzes[0].getQualifiedName() );
}
}
Brad wrote:

Please, someone throw me a bone here.

"Brad" <none@nowhere.org> wrote in message
news:c34k5t$njo$1@is.intellij.net...

>>How can I get it if it exists? Currently I'm trying to create an
>>URLClassloader with the module's classpath and loading the class that way
>>but it isn't working. Is there a way using psi?
>>
>>




--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

Thanks for the reply but...

I NEED +java.lang.Class +to pass to ObjectStreamClass::lookup(java.lang.Class). This is for the GenerateServialVersionUID plugin and once I get an instance of ObjectStreamClass I can use its +getSerialVersionUID() +method instead of using the serialver.exe tool, which is what I'm doing now.

0

Hi,
Thank you for the clarification, in this case PSI is of now interest for
the task. Maybe project/global libraries are missed from the classpath
of your ClassLoader.

Brad Lane wrote:

Thanks for the reply but...

I NEED +java.lang.Class +to pass to ObjectStreamClass::lookup(java.lang.Class). This is for the GenerateServialVersionUID plugin and once I get an instance of ObjectStreamClass I can use its +getSerialVersionUID() +method instead of using the serialver.exe tool, which is what I'm doing now.



--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"

0

I know I'm getting the correct classpath because I can use the same
classpath for the serialver.exe tool and it works fine. But, I may not be
creating the urls for the URLClassloader correctly.
I've tried several different formats but here's what I'm trying now (doesn't
work obviously):


"Maxim Mossienko" <Maxim.Mossienko@jetbrains.com> wrote in message
news:c3s4vc$ak8$1@is.intellij.net...

Hi,
Thank you for the clarification, in this case PSI is of now interest for
the task. Maybe project/global libraries are missed from the classpath
of your ClassLoader.

>

Brad Lane wrote:

>

Thanks for the reply but...

>

I NEED +java.lang.Class +to pass to

ObjectStreamClass::lookup(java.lang.Class). This is for the
GenerateServialVersionUID plugin and once I get an instance of
ObjectStreamClass I can use its +getSerialVersionUID() +method instead
of using the serialver.exe tool, which is what I'm doing now.

>

>
>

--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"



0

i have no real experience in this subject but as far as i remember, jar files have their own protocol.

I would guess that using

where dir is a file with jar extension doesn't do what you want.

have a look at http://javaalmanac.com/egs/java.net/JarUrl.html?l=rel it may help

0

Thank you! Thank you! Thank you! I thought I had tried that format already
but must have had something wrong.

<no_mail@jetbrains.com> wrote in message
news:21395699.1080225867130.JavaMail.itn@is.intellij.net...

i have no real experience in this subject but as far as i remember, jar

files have their own protocol.
>

I would guess that using


where dir is a file with jar extension doesn't do what you want.

>

have a look at http://javaalmanac.com/egs/java.net/JarUrl.html?l=rel it

may help


0

Please sign in to leave a comment.