Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Strange: compiles as Annotation but not as a method

Thread view: 
Olli Plough - 06 Dec 2007 11:19 GMT
Hello,

I have an annotation like this:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)

public @interface ContextualPath
{
    public Class<? extends MyInterface>[] nodes();
}

Now I define this annotation for a class:

@ContextualPath(nodes = { MyObject.class })
public class Foo
{
}

where MyObject implements MyInterface.

This compiles fine without problem. However it does not compile if
expressed within a method. Compiler errors for method
Bar.getContextualPath1:

* Type mismatch: cannot convert from Class<MyObject> to
Class<MyInterface>
* Cannot create a generic array of Class<MyInterface>

On the contrary, Bar.getContextualPath2 compiles fine.

class Bar
{

    public Class<? extends IModel>[] getContextualPath1()
    {
                              new Class<IModel>[]
{ MyObject.class };
    }

    public Class<? extends IModel>[] getContextualPath2()
    {
                              // compiles without problem
                  return
getClass().getAnnotation(ContextualPathNodes.class).nodes();
    }
}

There seems to be something special with annotations that I don't get.
Most of all I don't understand why it is allowed for an annotation but
not within a class. Would be gratefull if some knowledgeable person
out there could shed some light on this for me.

Thanks, Oliver
Daniel Pitts - 06 Dec 2007 20:25 GMT
> Hello,
>
[quoted text clipped - 7 lines]
>     public Class<? extends MyInterface>[] nodes();
> }

You're mixing generics with arrays... Can't do that unfortunately. I
don't really see a way around that in this case either.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Olli Plough - 07 Dec 2007 09:40 GMT
> You're mixing generics with arrays... Can't do that unfortunately. I
> don't really see a way around that in this case either.
>
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Hi Daniel,

thanks for that one. Really didn't know this.

Cheers, Olli


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.