Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Finding the Classpath from an annotation processor
Finding the Classpath from an annotation processor [message #519168] Sun, 07 March 2010 11:43 Go to next message
Eclipse UserFriend
I realise that anything I do in this regard is not going to be usable
with Javac, but I need to find the classpath for the current project
while doing annotation processing for a java source file?

David
Re: Finding the Classpath from an annotation processor [message #519690 is a reply to message #519168] Tue, 09 March 2010 12:01 Go to previous messageGo to next message
Eclipse UserFriend
David Goodenough wrote:

> I realise that anything I do in this regard is not going to be usable
> with Javac, but I need to find the classpath for the current project
> while doing annotation processing for a java source file?
>
> David

No-one got any ideas?

David
Re: Finding the Classpath from an annotation processor [message #519828 is a reply to message #519690] Wed, 10 March 2010 01:13 Go to previous messageGo to next message
Eclipse UserFriend
"David Goodenough" <david.goodenough@btconnect.com> wrote in message
news:hn5uu2$ec8$1@build.eclipse.org...
> David Goodenough wrote:
>
>> I realise that anything I do in this regard is not going to be usable
>> with Javac, but I need to find the classpath for the current project
>> while doing annotation processing for a java source file?
>>
>> David
>
> No-one got any ideas?

Hi, David. Sorry for the delay in answering.

The 'classpath' within an Eclipse project is a bit of a complex animal; see
IJavaProject.getRawClasspath() and getResolvedClasspath() for some of the
gory details. If you just need to find a particular file or resource there
are probably better ways to do it; what exactly are you trying to do?

If you need full-on Eclipse access within a Java 5 annotation processor,
though, you can implement your processor as a plugin, add a dependency on
the org.eclipse.jdt.apt.core plugin, and cast the
AnnotationProcessorEnvironment to an EclipseAnnotationProcessorEnvironment.
That will let you get the IJavaProject.

If you're working with a Java 6 annotation processor I'm not sure we've got
a comparable answer; tell me more about what you're doing and I'll see if I
can come up with a solution.
Re: Finding the Classpath from an annotation processor [message #520195 is a reply to message #519690] Thu, 11 March 2010 04:36 Go to previous messageGo to next message
Eclipse UserFriend
David Goodenough wrote:

> David Goodenough wrote:
>
>> I realise that anything I do in this regard is not going to be usable
>> with Javac, but I need to find the classpath for the current project
>> while doing annotation processing for a java source file?
>>
>> David
>
> No-one got any ideas?
>
> David

I take it that this is either impossible, or no-one has tried it?

David
Re: Finding the Classpath from an annotation processor [message #520200 is a reply to message #519828] Thu, 11 March 2010 09:41 Go to previous messageGo to next message
Eclipse UserFriend
Walter Harley wrote:

> "David Goodenough" <david.goodenough@btconnect.com> wrote in message
> news:hn5uu2$ec8$1@build.eclipse.org...
>> David Goodenough wrote:
>>
>>> I realise that anything I do in this regard is not going to be usable
>>> with Javac, but I need to find the classpath for the current project
>>> while doing annotation processing for a java source file?
>>>
>>> David
>>
>> No-one got any ideas?
>
> Hi, David. Sorry for the delay in answering.
>
> The 'classpath' within an Eclipse project is a bit of a complex animal;
> see IJavaProject.getRawClasspath() and getResolvedClasspath() for some of
> the
> gory details. If you just need to find a particular file or resource
> there are probably better ways to do it; what exactly are you trying to
> do?
>
> If you need full-on Eclipse access within a Java 5 annotation processor,
> though, you can implement your processor as a plugin, add a dependency on
> the org.eclipse.jdt.apt.core plugin, and cast the
> AnnotationProcessorEnvironment to an
> EclipseAnnotationProcessorEnvironment. That will let you get the
> IJavaProject.
>
> If you're working with a Java 6 annotation processor I'm not sure we've
> got a comparable answer; tell me more about what you're doing and I'll see
> if I can come up with a solution.
I specifically need a Java 6 solution. I need to be able to take a string,
find the class of that name, load it, and get information from that loaded
class.

David
Re: Finding the Classpath from an annotation processor [message #520269 is a reply to message #520200] Thu, 11 March 2010 13:51 Go to previous message
Eclipse UserFriend
"David Goodenough" <david.goodenough@btconnect.com> wrote in message
news:hnave5$slb$1@build.eclipse.org...
> I specifically need a Java 6 solution. I need to be able to take a
> string,
> find the class of that name, load it, and get information from that loaded
> class.

Hm, what you're asking for is a bit ugly. You're saying you want to load a
user class, within the compiler's memory space (which includes the
annotation processor; annotation processing is part of compilation). I
would really advise against that. There are a bunch of reasons, one of them
being that you're unlikely to be able to unload the class again.

If you're talking about trying to load one of the classes that you're
actually compiling, then I would *strenuously* advise against it. This is
one of those cases where the command-line compilation process lets you get
away with doing something just because at the end of the compilation the
whole VM is torn down, unlike what happens inside an IDE.

But if you're talking about trying to load a class from a jar file that
happens to be referenced from your project, well, I still wouldn't recommend
it but you can probably get away with it. One way to do that would be to
pass the relative location of the jar file in a -A option, that is, one of
the options that you specify from the project's Annotation Process
properties dialog and access from ProcessingEnvironment.getOptions().

Or, you could use Filer.getResource() and pass in one of the classpath
locations specified in StandardLocation; and then build a classloader to
load the class from the input stream that you obtain. Once you dispose of
that classloader the VM *may* unlock the loaded class. The problem there is
that the JSR269 (Java 6 APT API) spec only specifies that a processing
environment must support the output locations, CLASS_OUTPUT and
SOURCE_OUTPUT (see
http://java.sun.com/javase/6/docs/api/javax/annotation/proce ssing/Filer.html).
I don't recall whether the Eclipse JSR269 implementation supports any
additional locations.
Previous Topic:Jar making problem
Next Topic:Cannot resolve type, but F3 works to open declaration
Goto Forum:
  


Current Time: Wed Mar 26 08:14:35 EDT 2025

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

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

Back to the top