Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] [newbie] converting Integer to java.lang.Enum.Modifier

    Any idea how to convert the value retrieved from getModifiers to a Modifier so I can read it?

    :confused:

    Java Code:
    package homenetwork.bkr.training;
    import java.lang.reflect.*;
    import java.lang.Enum.*;
    
    public class ReflectionTest {
    
    	static boolean debug = true;
    	
    	public static void main(String[] args) {
    		
    		Class _cl = Employee.class.getClass();
    		Integer _m = Employee.class.getClass().getModifiers();
    		Modifier _modifier = _m;
    		//converting the integer to a modifier?
    		
    	}
    	
    	private static void printSomething() {
    		if (debug) System.out.println("Something");
    	}
    	
    	/*NOTE:
    	 * When using static methods, always make:
    	 1. variables used by static methods as static
    	 2. methods that are to be called without instantiating them (statically) as static
    	*/
    
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    844
    Rep Power
    6

    Default

    Hi,
    What you want to read here?.Please explain it clearly.
    Ramya:cool:

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    I want to read the modifiers...

    Java Code:
    Integer _m = Employee.class.getClass().getModifiers();
    		Modifier _modifier = _m;
    		//converting the integer to a modifier?

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    844
    Rep Power
    6

    Default

    Hi,
    Still your question is not clear.Might be you are asking this.If you want to get the integer value details returned by this,u can try with method
    int i = Employee.class.getClass().getModifiers();
    System.out.println(Modifier.toString(i));

    The below method will be useful
    -------------------------------
    toString
    public static String toString(int mod)Return a string describing the access modifier flags in the specified modifier. For example:
    public final synchronized strictfp
    The modifier names are returned in an order consistent with the suggested modifier orderings given in The Java Language Specification, Second Edition sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1. The full modifier ordering used by this method is:
    public protected private abstract static final transient volatile synchronized native strictfp interface
    The interface modifier discussed in this class is not a true modifier in the Java language and it appears after all other modifiers listed by this method. This method may return a string of modifiers that are not valid modifiers of a Java entity; in other words, no checking is done on the possible validity of the combination of modifiers represented by the input.

    Parameters:
    mod - a set of modifers
    Returns:
    a string representation of the set of modifers represented by mod

    --------------------------------------------------------------------------------
    Ramya:cool:

  5. #5
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    Ok,
    If you want to be able to retrieve a modifier from a class object, why dont you make the getModifiers() method to return exactly what you call it; ie a type of modifier. is a modifier a class that you created yourself? Are you getting an error? is it data type related?

    The problem here is that you assign an integer which is a java primitive data type to an object. The two types are incompatible......what do you really want to do here?
    Tshegofatso Manakana
    a.k.a Untouchable

  6. #6
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Sorry for the misclarification guys. Well, I would like to see private, protected etc, rather than something like:

    [Ljava.lang.reflect.Method;@19821f

  7. #7
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    Still your question is not clear.Might be you are asking this.If you want to get the integer value details returned by this,u can try with method
    int i = Employee.class.getClass().getModifiers();
    System.out.println(Modifier.toString(i));

    The below method will be useful
    -------------------------------
    toString
    public static String toString(int mod)Return a string describing the access modifier flags in the specified modifier. For example:
    public final synchronized strictfp
    The modifier names are returned in an order consistent with the suggested modifier orderings given in The Java Language Specification, Second Edition sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1. The full modifier ordering used by this method is:
    public protected private abstract static final transient volatile synchronized native strictfp interface
    The interface modifier discussed in this class is not a true modifier in the Java language and it appears after all other modifiers listed by this method. This method may return a string of modifiers that are not valid modifiers of a Java entity; in other words, no checking is done on the possible validity of the combination of modifiers represented by the input.

    Parameters:
    mod - a set of modifers
    Returns:
    a string representation of the set of modifers represented by mod

    --------------------------------------------------------------------------------
    Java Language Specification (3rd edition) specification at http://java.sun.com/docs/books/jls/d...ngspec-3.0.pdf

  8. #8
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Syntactically, I would like to do something like this, which is not allowed because it is an invalid cast:
    Java Code:
    java.lang.Enum.Modifier _modifier = (java.lang.Enum.Modifier) _m;

  9. #9
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Ramya has given you your answer. The Language Specification isn't very helpful, you want the API docs.
    Java Platform SE 6

    There's no such thing as a java.lang.Enum.Modifier
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  10. #10
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Language specification is usually helpful (to me) when supporting a bug report in the language itself or for an overview.

    I've read the API, but I couldn't find anything that specifically indicates.. this is how you have to change to Modifiers[].

    Java Platform SE 6

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    Quoting from the API for getModifiers (on the Class):

    Returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface; they should be decoded using the methods of class Modifier.
    So you need to use the Modifier class, which has a bunch of static methods (such as isPublic) into which you can pass the in returned by the getModifiers() method.

    This will only give you the modfiiers for the class, of course...you'll need to do this for all the methods you're interested in. So, you'll probably want to write a reusable snippet of code to turn this int into a String, since that seems to be what you want.

  12. #12
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Yeah, but since they are static enums, presumably having static data, how do I tell the compiler to get the modifiers from the particular object or of the particular class that I want?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    Quote Originally Posted by jon80 View Post
    Yeah, but since they are static enums, presumably having static data, how do I tell the compiler to get the modifiers from the particular object or of the particular class that I want?
    I really don't understand what you want, here.

    getModifiers returns an int. This is Java's internal representation of the modifiers (final, static, public etc). It is not an enumeration. You use the static methods in Modifiers to find out specific bits of information on this int.

    So:
    Java Code:
    Class c = SomeClass.class;
    int mod = c.getModifiers();
    
    if (Modifier.isFinal(mod))
    {
        System.out.println("SomeClass is marked as final.");
    }
    That's how they work....and similarly for methods.

  14. #14
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Oh thanks :) Still there's a missing line I'm not getting...see code below

    Java Code:
    package homenetwork.bkr.training;
    import java.lang.reflect.*;
    import java.lang.Enum.*;
    
    public class ReflectionTest {
    
    	static boolean debug = true;
    	
    	public static void main(String[] args) {
    		
    	try {	
    		Class _cl = Employee.class.getClass();
    		
    		if (debug) System.out.println("DEBUG 1: " + Employee.class.getClass());
    		if (debug) System.out.println("DEBUG 2: " + Employee.class.getSuperclass());
    			
    		System.out.println(_cl.getClass().getMethods().toString());
    		 
    		[B]Integer _m = Employee.class.getClass().getModifiers(); 
    		Modifier _modifierName = 0;[/B]
    		switch (_m) {
    			case Modifier.PUBLIC :
    				System.out.println("Modifier is public");
    				break;
    			case Modifier.PRIVATE :
    				System.out.println("Modifier is private");
    				break;
    			case Modifier.ABSTRACT :
    				System.out.println("Modifier is abstract");
    			case Modifier.FINAL :
    				System.out.println("Modifier is native");
    			case Modifier.PROTECTED :
    				System.out.println("Modifier is protected");
    			case Modifier.STATIC :
    				System.out.println("Modifier is static");
    			case Modifier.STRICT :
    				System.out.println("Modifier is strict");
    			default:
    				break;
    		}
    	}
    	catch (Exception ex){
    		System.out.println(ex.getMessage());
    	}
    		
    		}
    		
    	}
    	
    	private static void printSomething() {
    		if (debug) System.out.println("Something");
    	}
    	
    	/*NOTE:
    	 * When using static methods, always make:
    	 1. variables used by static methods as static
    	 2. methods that are to be called without instantiating them (statically) as static
    	*/
    
    }

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,122
    Rep Power
    20

    Default

    You need to read up on java.lang.reflect.Modifier.
    You don't instantiate it...(which does beg the question of why it has an exposed constructor, but someone else'll have to explain that one).

    You need to pass the int you have into the various methods in Modifier. Ignore the static fields. You cannot simply switch on the int against these fields, since they are (probably) simply bit flags, and your int will have a number of them set.

  16. #16
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Or use the toString(int) method as described before.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  17. #17
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Thanks. Well I got the class that reads some info from my class. I'm not sure why I'm passing an Employee object and getting a class java.lang.Class (bug?)

    Is it possible to convert it so that I can pass "any object". I tried using this to no avail:
    Java Code:
    public static void readClass (Class<Object> class1) {

    Modifier value '17' (see output) seems like an undocumented mystery to me, any ideas?

    Java Code:
    HelperClassesforClasses.java 
    package homenetwork.bkr.training;
    
    import java.lang.reflect.Modifier;
    
    public class HelperClassesforClasses {
    	
    	@SuppressWarnings("unchecked")
    	public static void readClass (Class<Employee> class1) {
    		
    		/*FIX: to update so that an object or a class can be passed to this method.
    		 * To review so that the following information (at least) is obtained:
    		 * constructors:
    		 * methods and their access and modifiers
    		 * instance fields and their access
    		 * other information (to research reflection)
    		 * In the meantime this function is saved within the main() method of ReflectionTest.java.
    		*/
    		final boolean debug = true;
    		
    		Class _cl = class1.getClass().getClass();
    		
    		if (debug) System.out.println("DEBUG 1: class.getClass=" + Employee.class.getClass());
    		if (debug) System.out.println("DEBUG 2: class.getSuperclass=" + Employee.class.getSuperclass());
    			
    		Integer _m = class1.getClass().getModifiers(); 
    		
    		switch (_m) {
    		
    			case Modifier.ABSTRACT :
    				System.out.println("Modifier is ABSTRACT");
    				break;
    			case Modifier.FINAL :
    				System.out.println("Modifier is FINAL");
    				break;
    			case Modifier.NATIVE :
    				System.out.println("Modifier is NATIVE");
    				break;
    			case Modifier.PRIVATE :
    				System.out.println("Modifier is PRIVATE");
    				break;	
    			case Modifier.PROTECTED :
    				System.out.println("Modifier is PROTECTED");	
    			case Modifier.PUBLIC :
    				System.out.println("Modifier is PUBLIC");
    				break;
    			case Modifier.STATIC :
    				System.out.println("Modifier is STATIC");
    				break;
    			case Modifier.STRICT :	//documented as STRICTFP
    				System.out.println("Modifier is STRICT");
    				break;
    			case Modifier.SYNCHRONIZED:
    				System.out.println("Modifier is SYNCHRONIZED");
    				break;
    			case Modifier.TRANSIENT:
    				System.out.println("Modifier is TRANSIENT");
    				break;
    			case Modifier.VOLATILE:
    				System.out.println("Modifier is VOLATILE");
    				break;	
    			default:
    				System.out.println(_m.toString());
    				break;
    		}
    	}
    
    }
    Output:
    DEBUG 1: class.getClass=class java.lang.Class //shouldn't this be Employee..heh before it was working with other packages.
    DEBUG 2: class.getSuperclass=class java.lang.Object
    17 //er?

  18. #18
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    If it's not solved, don't mark as such.

    The type for an unknown class is Class<?>, not Class<Object>. Why are you calling getClass() so much? Calling this on a Class object will give you a Class<Class>, and the superclass of Class is Object.

    You have been repeatedly told how to solve your problem, yet you persist in ignoring people. You should also be using the type int, not Integer.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  19. #19
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Sorry I just did not understand completely. Why should I use int rather than Integer?
    Last edited by jon80; 05-16-2009 at 12:46 AM.

  20. #20
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    int is a primitive type, stored as a literal in the stack. Integer is a class, used to wrap ints when an Object is needed. It is stored on the heap and takes up lots of unnecessary space. ints will be converted automatically to Integers if necessary so there is usually no need to worry about when to use an Integer.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] [newbie] java.lang.NullPointerException
    By jon80 in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 05:19 PM
  2. Regarding default access modifier?
    By makpandian in forum New To Java
    Replies: 2
    Last Post: 03-14-2009, 09:21 AM
  3. Replies: 2
    Last Post: 09-18-2008, 08:30 AM
  4. why we use public access modifier?
    By vichet in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 08:04 AM
  5. Help with Integer in java
    By susan in forum New To Java
    Replies: 1
    Last Post: 07-14-2007, 06:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •