Follow us on Twitter!
Imagination is more valuable than knowledge - Albert Einstein
Saturday, April 19, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 27
Guests Online: 25
Members Online: 2

Registered Members: 82841
Newest Member: and3rv1sh
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

annotations in java enum

the_unwanted
Member

Your avatar

Posts: 11
Location:
Joined: 03.12.10
Rank:
Newbie
Posted on 08-05-12 19:28
Code


import java.lang.reflect.*;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@interface anno{
      int value();
      String str();     
}
enum planet{earth(12),mars(11),pluto(2),venus(3);
int val;
planet()
{
val=-1;
}
planet(int k)
{
    val=k;
}
@anno(value=100,str="sun")
void getval()
{
    System.out.println(val);
    //return val;
}
void annomethod()
{
    planet p;
    p=planet.earth;
    //int k=p.getval();
   // System.out.println(k);
    try
    {
        Class c=p.getClass();
        Method m=c.getMethod("getval");
        anno a=m.getAnnotation(anno.class);
        System.out.println(a.str()+"\n value"+a.value());
               
    }
     catch(NoSuchMethodException e)
    {
            System.out.println("caught"+e);
    }
}
}

public class annoexam {
    public static void main(String ... v)
    {
        planet p;
        p=planet.earth;
        p.annomethod();
    }
   
}






y getting NoSuchMethod found exception ?
Author

RE: annotations in java enum

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 08-05-12 20:01
Code
package lol;

import java.lang.reflect.*;
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@interface anno {
   int value();
        String str();     
}

enum planet {
   earth(12),mars(11),pluto(2),venus(3);
   int val;
   
   planet() {
      val = -1;
   }
   planet(int k) {
                val = k;
   }

   @anno(value=100,str="sun")
   public void getval() {
               System.out.println(val);
               //return val;
   }

   void annomethod() {
               planet p = planet.earth;
           //int k=p.getval();
           // System.out.println(k);
               try {
                  Method m = p.getClass().getMethod("getval", null);
          
                  anno a = m.getAnnotation(anno.class);
                   System.out.println(a.str() + "\nvalue: " + a.value());
           }
           catch(NoSuchMethodException e) {
                  e.printStackTrace();
           }
   }
}

public class annoexam {
    public static void main(String[] args) {
        planet p = planet.earth;
        p.annomethod();
    }
   
}






Next time you post, if you do post again, format your code correctly. It's shit work having to deal with formatting when you're trying to help someone. Your code was full of useless instantiation and bad code. You clearly didn't read the API before attempting to do this, otherwise you would've known getMethod() takes 2 arguments. That being said, I fixed it, and removed unnecessary vars.

Exceptions have built in methods that can throw or be thrown as certain errors and methods.

Code
e.printStackTrace()


is one of them. I recommend getting a better IDE or editor that can highlight what you're doing and help you debug your code yourself. Eclipse is a good one.


G'bye y'all! I was an asshole, So korg banned me.

Edited by Arabian on 11-05-12 02:38
Author

RE: annotations in java enum

the_unwanted
Member

Your avatar

Posts: 11
Location:
Joined: 03.12.10
Rank:
Newbie
Posted on 08-05-12 21:15
im just experimenting .. whether annotations work in enums
getMethod can take one argument .. ..
my code is correct except i forgot to declare getVal method as public..
i will post code without unnecessary initiations next time
thanks for ur reply

Author

RE: annotations in java enum

Arabian
Banned



Posts: 332
Location: inside you.
Joined: 22.09.10
Rank:
Apprentice
Posted on 08-05-12 21:19
the_unwanted wrote:
im just experimenting .. whether annotations work in enums
getMethod can take one argument .. ..
my code is correct except i forgot to declare getVal method as public..
i will post code without unnecessary initiations next time
thanks for ur reply



please pass it the second value. That way, anyone wanting to mod it will be able to do so without hunting for the API again. Also, this Annotation is useless. I've never actually seen it used this way IRL outside of this code. I've only ever had to use @deprecated, @override, @suppresswarnings (when using generics), and @inherited.

I really don't see the point in making an annotated interface, when Java's interface qualifier is robust and flexible as it is. Care to elaborate?


G'bye y'all! I was an asshole, So korg banned me.

Edited by Arabian on 08-05-12 21:21
Author

RE: annotations in java enum

dopeboimag1k
Member

Your avatar

Posts: 30
Location: the Internet
Joined: 01.05.11
Rank:
Moderate
Posted on 11-05-12 19:28
Arabian wrote:
I've only ever had to use @deprecated, @override, @suppresswarnings (when using generics), and @inherited.


I've also only ever seen those annotations and @test, and @before, but those are used for unit tests.


thirdgen.net16.net/images/signature.jpg

"Everybody wants greatness but nobody wants to work for it."

"If it was easy, everybody would do it."