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 / Tools / July 2005

Tip: Looking for answers? Try searching our database.

turning off AN error message

Thread view: 
Roedy Green - 15 Jul 2005 05:27 GMT
I often have code like this:

private static final String EmbeddedCopyright = "copyright (c)
1998-2005 Roedy Green, Canadian Mind Products, http://mindprod.com";

All it does it embed a string in a class file without ever displaying
it.  Eclipse gets quite excited about his since it does not do
anything.  

Normally constants I don't use are errors, but not this one. Is there
something I can do to mollify eclipse without turning of this sort of
checking in general?

I saw in passing something about an @ignore ?? tag.

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Dale King - 16 Jul 2005 03:41 GMT
> I often have code like this:
>
[quoted text clipped - 10 lines]
>
> I saw in passing something about an @ignore ?? tag.

Making it public would be one way.

The other option, if you just want that piece of text to be embedded
into the class file is to make a copyright annotation with Retention
policy of CLASS.

A copyright annotation is even one of the examples in documentation for
annotations. You can even have the annotation specify the default value
so that the copyright text appears in only one place:

So how about something like this:

@Retention( RetentionPolicy.CLASS )
public @interface CopyrightCanadianMindProducts
{
    String text()
       dafault "copyright (c) 1998-2005 Roedy Green, "
             + "Canadian Mind Products, http://mindprod.com";
}

So then to use it:

@CopyrightCanadianMindProducts
public class RoedysClass
{ ... }

When 2006 rolls around and you want to update the dates you only have to
update the one piece of text.

Signature

 Dale King

Henry Townsend - 16 Jul 2005 04:07 GMT
> A copyright annotation is even one of the examples in documentation for
> annotations. You can even have the annotation specify the default value
[quoted text clipped - 18 lines]
> When 2006 rolls around and you want to update the dates you only have to
> update the one piece of text.

Nice trick. I wonder, though: this will have the effect of embedding the
full copyright notice in class files. But does it protect the source
itself as well as the low-tech one-string-per-sourcefile technique does?

IANAL and all that.

Signature

Henry Townsend

Dale King - 16 Jul 2005 20:02 GMT
>> A copyright annotation is even one of the examples in documentation
>> for annotations. You can even have the annotation specify the default
[quoted text clipped - 24 lines]
>
> IANAL and all that.

For the source, I think you would want the copyright message to be in a
Javadoc comment so that it also makes it into the API documentation.

By the way, the annotation element should be named value(), not text().
(and of course that was supposed to be default not dafault).

If you want the text to appear in the source file then you can pass the
text instead of relying on the default.

So cleaning up a little more and adding @Documented so that it also
appears in the documentation we have:

@Documented
@Retention( RetentionPolicy.CLASS )
@interface CopyrightCanadianMindProducts
{
    String defaultCopyright =
        "copyright (c) 1998-2005 Roedy Green, "
        + "Canadian Mind Products, http://mindprod.com";
;
    String value()  default defaultCopyright;
}

This then gives us three ways to use it.

@CopyrightCanadianMindProducts
class Class1{}

@CopyrightCanadianMindProducts
(
    "copyright (c) 1998-2005 Roedy Green, "
    + "Canadian Mind Products, http://mindprod.com"
)
class Class2{}

@CopyrightCanadianMindProducts
    ( CopyrightCanadianMindProducts.defaultCopyright )
class Class3{}

The difference between 2 and 3 are whether the text appears in the
source file. Both will include the text in the javadocs.

Signature

 Dale King

Roedy Green - 17 Jul 2005 00:07 GMT
>This then gives us three ways to use it.
>
[quoted text clipped - 14 lines]
>The difference between 2 and 3 are whether the text appears in the
>source file. Both will include the text in the javadocs.

I am just echoing this back to you expanded a bit to make sure I
understood:

Annotations are used in the form @interface or @Retention. These are
annotations, a feature of JDK 1.5+. You can use them to add metadata
to your programs useful to various utilities that process the text,
e.g. for persistent object store implementations. They can also be
used for inserting boiler plate in programs. You use them as you would
the attribute static, declaring a class, variable or method to be
marked with a given annotation.

These @annotations not JavaDoc tags! They are part of the program
proper.

@Retention(RetentionPolicy.RUNTIME) means the following annotation
should be retained in the class files.

@Target(ElementType.METHOD) means the following annotation only
applies to methods in the class, not the whole class.

@Documents means the following annotation should show up in the
JavaDoc


// this code lives is a file called CopyrightCanadianMindproducts.java

package com.mindprod.common15;

import java.lang.annotation.*;

@Documented
@Retention( RetentionPolicy.RUNTIME )
@interface CopyrightCanadianMindProducts
{
  String defaultCopyright =
  "copyright (c) 1998-2005 Roedy Green, "
  + "Canadian Mind Products, http://mindprod.com";

  String value() default defaultCopyright;
}

Now that you have defined it, you can annotate any class with it,
which embeds the copyright notice in the class file, with otherwise
changing the class file. Here is how you use it:

// @CopyrightCanadianMindProducts now behaves like other modifiers
// such as public or static.  It can be used to tag a class,
// embedding the copyright notice, but otherwise not changing the
class.
@CopyrightCanadianMindProducts public class MyClass1{ ... }

// -------------------------------------------------------------------

// Or I can tag a class with a slightly different copyright notice,
// but still using the CopyrightCanadianMindProducts tag name.
// I could also use this technique to ensure the copyright notice
// wording is in the source text as well.
@CopyrightCanadianMindProducts
 {   // note { not (
 "copyright (c) 2005 Roedy Green, "
 + "Canadian Mind Products, http://mindprod.com"
 }
public class MyClass2{ ... }

// -------------------------------------------------------------------

// Or I can be explicit that I am using the default
@CopyrightCanadianMindProducts
 {
 CopyrightCanadianMindProducts.defaultCopyright
 }
public class MyClass3{ ... }

for more info see:

J:\Program Files\java\jdk1.5.0_04\docs\guide\language\annotations.html

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes

Dale King - 18 Jul 2005 02:36 GMT
>>This then gives us three ways to use it.
>>
[quoted text clipped - 19 lines]
>
> Annotations are used in the form @interface or @Retention.

They are declared using @interface. They are used by using @ and the
name of the annotation. Retention is an annotation that you apply to
another annotation to specify how long the annotation is maintained.

> These are
> annotations, a feature of JDK 1.5+. You can use them to add metadata
[quoted text clipped - 3 lines]
> the attribute static, declaring a class, variable or method to be
> marked with a given annotation.

They can serve man different purposes. Java itself uses some for its own
purposes. For example there is the @Override annotation to specify that
a method has to override a method in the superclass or the compiler
generates an error.

They are kind of like marker interfaces except that they actually can
contain some simple data.

> These @annotations not JavaDoc tags! They are part of the program
> proper.

Yes. In fact there is a Deprecated annotation that can be used instead
of the @deprecated java doc tags.

> @Retention(RetentionPolicy.RUNTIME) means the following annotation
> should be retained in the class files.

Yes. In addition to being retained in the class file it can be read at
runtime using reflection. With RetentionPolicy.CLASS the information is
discarded when the class is read into the VM.

> @Target(ElementType.METHOD) means the following annotation only
> applies to methods in the class, not the whole class.

It means that the annotation can only be used on method declarations.

> @Documents means the following annotation should show up in the
> JavaDoc

It's @Documented, not @Documents, but yes.

> // this code lives is a file called CopyrightCanadianMindproducts.java
>
[quoted text clipped - 14 lines]
>
> Now that you have defined it, you can annotate any class with it,

Since, you didn't declare a Target annotation on it you can annotate
anything with it.

> which embeds the copyright notice in the class file, with otherwise
> changing the class file. Here is how you use it:

And you can also read it using reflection at runtime. Do you really need
to do that? If not, then would use RetentionPolicy.CLASS.

> // @CopyrightCanadianMindProducts now behaves like other modifiers
> // such as public or static.  It can be used to tag a class,
> // embedding the copyright notice, but otherwise not changing the
> class.
> @CopyrightCanadianMindProducts public class MyClass1{ ... }

Yes. This will embed the defaultCopyright in the class file. The javdocs
will only say @CopyrightCanadianMindProducts.

> // Or I can tag a class with a slightly different copyright notice,
> // but still using the CopyrightCanadianMindProducts tag name.
[quoted text clipped - 6 lines]
>   }
> public class MyClass2{ ... }

No, it's parentheses not braces.

And in this case the javadocs will contain the text that you specify.

> // Or I can be explicit that I am using the default
> @CopyrightCanadianMindProducts
>   {
>   CopyrightCanadianMindProducts.defaultCopyright
>   }
> public class MyClass3{ ... }

Once again, its parentheses not braces. This will also include the text
in the javadocs.

> J:\Program Files\java\jdk1.5.0_04\docs\guide\language\annotations.html

This link might work a little better for others ;-)

http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html

Signature

 Dale King



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



©2008 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.