Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

annotations in java enum


the_unwanted's Avatar
Member
0 0

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 ?


Arabian's Avatar
Member
0 0

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.

markupe.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.


the_unwanted's Avatar
Member
0 0

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


Arabian's Avatar
Member
0 0

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?