participate


Java Programming - Do not know how to process @Deprecated annotation for local variable
<<   Back to Forum  |   Give us Feedback
2 Duke Stars available
This topic has 5 replies on 1 page.
a3cchan
Posts:104
Registered: 2005.11.13.
Do not know how to process @Deprecated annotation for local variable   
Jan 10, 2007 12:21 AM

 
Hallo,

I'd like to use @Depreacated annotation in method body to mark parts of code (local variables) that are 'not written well'. DO NOT analyse why should/shoudn't i do it, I wish to. :-) Something like this:
void function() {
    @Deprecated int a; 
    a = 101;
    System.out.println(a);
}
But Suns javac 1.6.0 is not generating any warning, or to be precise Sun javac is processing depreacated annotations on api level only.

Knowing this, my second tought was to create simple tool that load all project classes, check annotations in all methods and write out warnings and wrap it as ant taks. Something like this:
Class<?> c = Some.class;
Method m = c.getDeclaredMethod("function");
for(Annotation a : m.getAnnotations()) {
    if (a instanceof Deprecated)
        System.err.println("Warning: "+m.toString()+ " contains deprecated code");
}
But method reflection is not what i want of course!

Do you have any idea how to process local variables annotation at runtime please? Is this information stored somewhere? Should be coz its, retention is runtime.

~~
Adam
 
kajbj
Posts:28,981
Registered: 3/21/00
Re: Do not know how to process @Deprecated annotation for local variable   
Jan 10, 2007 12:50 AM (reply 1 of 5)  (In reply to original post )

 
I'd like to use @Depreacated annotation in
method body to mark parts of code (local variables)
that are 'not written well'. DO NOT analyse why
should/shoudn't i do it, I wish to. :-)

I can't resist. Deprecated means that the user should use something else, but the user can't say that he only wants to execute parts of a method, so why use the deprecated annotation?

Kaj
 
a3cchan
Posts:104
Registered: 2005.11.13.
Re: Do not know how to process @Deprecated annotation for local variable   
Jan 10, 2007 1:22 AM (reply 2 of 5)  (In reply to #1 )

 
Hehe :) Ok, I'll tell :-) It's attempt to (ab)use deprecated annotation in development process. I have to maintain piece of sh.. I mean, code that's awful -- sometimes I cry when I look at it, sometimes it's just really funny (i've attached sample to the bottom).

I am trying to change at least part of it to maintainable and if possible more efficient form. In process, I analyse code and "annotate" code by TODO comments. So in this context deprecated means "programmers are discouraged from using it, and it should be removed" :-P

If you wish to play with words :-) @Deprecated javadoc says: "A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. ...so in my case it's bit not typical usage of deprecated annotation. :-P

Promised piece of ill-defined code:
// Observe superb nameing convetion
/**
 *  Loads this panel with data
 */
public abstract boolean loadedElement(XMLElement el) throws Exception;
Wonder why it's not haveHadLoadedElement(XMLElement). Function simply bind xml data to form but plain simple bind(XMLElement) was too hard to write.

Now observe superless code:
// Should be like
class PropertyPanel {
  protected XPDLElementEditor editor;
  PropertyPanel(XPDLElementEditor editor) { }
}
But w/o knowledge of super keyword and inheritance you create something like this:
public class PropertyPanel {
   abstract public XPDLElementEditor getEditor();
   abstract public void setEditor(XPDLElementEditor  editor);
 
public class ActivityTransactionLink extends PropertyPanel {    
    private XPDLElementEditor editor;
    
public class ActivitysParticipant extends PropertyPanel {
    private XPDLElementEditor editor;
 
public class ActualParamsPreviewPanel extends PropertyPanel {
    private XPDLElementEditor editor;
 
public class AttributeActivityLink extends PropertyPanel {    
    private XPDLElementEditor editor;
 
public class AttributePreviewPanel extends PropertyPanel {
    XPDLElementEditor editor;
 
public class AttributeTransactionLink extends PropertyPanel {
    private XPDLElementEditor editor;
 
public class DeadlinePreview extends PropertyPanel {
    XPDLElementEditor editor;
 
public class FormalParamsPreviewPanel extends PropertyPanel {
    private XPDLElementEditor editor;
 
public class ProcessGeneralInfo extends PropertyPanel {
    private XPDLElementEditor editor; 
 
public class ProcessHeaderProperty extends PropertyPanel {
    private XPDLElementEditor editor;
 
// And all other PropertyPanel childs 
Suicide seems like reasonable option...

~~
Adam
 
kajbj
Posts:28,981
Registered: 3/21/00
Re: Do not know how to process @Deprecated annotation for local variable      
Jan 10, 2007 3:13 AM (reply 3 of 5)  (In reply to #2 )

 
Is there a reason that you don't use TODO as comment? Some IDEs displays TODO comments in a certain pane.

Kaj
 
a3cchan
Posts:104
Registered: 2005.11.13.
Re: Do not know how to process @Deprecated annotation for local variable   
Jan 10, 2007 3:37 AM (reply 4 of 5)  (In reply to #3 )

 
Of course TODO is enough. Netbeans have TODO window. But TODO in comment is not 'fearsome' enough :) Afaik java 1.5 annotations were created to move class meta data out from comments (xdoclet) and external resources (many o/r frameworks). So leaving TODO in comments seems like step back -- don't you think it might be nice to have standardized @Todo annotation in language? It can be processed by compiler, IDEs and other tools.

Now i am simply interested if missing @ElementType in @Deprecated defintion is bad design/sun-programmer mistake^_^. Or if it's simply left for enhancments and appropriate changes will be made in future javac versions.

~~
Adam
 
kajbj
Posts:28,981
Registered: 3/21/00
Re: Do not know how to process @Deprecated annotation for local variable      
Jan 10, 2007 4:02 AM (reply 5 of 5)  (In reply to #4 )

 
Now i am simply interested if missing
@ElementType in @Deprecated defintion
is bad design/sun-programmer mistake^_^. Or if it's
simply left for enhancments and appropriate changes
will be made in future javac versions.

No, I don't think that it's bad design from Sun. Deprecation is used for elements that you call / use, that is: methods, classes, interfaces and methods. You can avoid calling methods and avoid using classes but you can't avoid using statements within a method, so I don't see the point of using deprecation for that.

Kaj
 
This topic has 5 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 129

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums