Results 1 to 20 of 25
- 05-15-2009, 10:24 AM #1
[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 */ }
- 05-15-2009, 10:50 AM #2
Hi,
What you want to read here?.Please explain it clearly.Ramya:cool:
- 05-15-2009, 10:59 AM #3
I want to read the modifiers...
Java Code:Integer _m = Employee.class.getClass().getModifiers(); Modifier _modifier = _m; //converting the integer to a modifier?
- 05-15-2009, 11:56 AM #4
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:
- 05-15-2009, 11:59 AM #5
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
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 ™
- 05-15-2009, 12:01 PM #6
Sorry for the misclarification guys. Well, I would like to see private, protected etc, rather than something like:
[Ljava.lang.reflect.Method;@19821f
- 05-15-2009, 12:04 PM #7
Java Language Specification (3rd edition) specification at http://java.sun.com/docs/books/jls/d...ngspec-3.0.pdf
- 05-15-2009, 12:53 PM #8
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;
- 05-15-2009, 01:13 PM #9
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.ModifierDon't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-15-2009, 01:33 PM #10
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
- 05-15-2009, 01:38 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Quoting from the API for getModifiers (on the Class):
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.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.
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.
- 05-15-2009, 01:45 PM #12
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?
- 05-15-2009, 02:00 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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:
That's how they work....and similarly for methods.Java Code:Class c = SomeClass.class; int mod = c.getModifiers(); if (Modifier.isFinal(mod)) { System.out.println("SomeClass is marked as final."); }
- 05-15-2009, 02:43 PM #14
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 */ }
- 05-15-2009, 02:50 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 05-15-2009, 04:23 PM #16
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
- 05-15-2009, 07:59 PM #17
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?
Output: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; } } }
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?
- 05-15-2009, 11:08 PM #18
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
- 05-15-2009, 11:41 PM #19
Sorry I just did not understand completely. Why should I use int rather than Integer?
Last edited by jon80; 05-15-2009 at 11:46 PM.
- 05-16-2009, 12:04 AM #20
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
Similar Threads
-
[SOLVED] [newbie] java.lang.NullPointerException
By jon80 in forum New To JavaReplies: 5Last Post: 05-07-2009, 04:19 PM -
Regarding default access modifier?
By makpandian in forum New To JavaReplies: 2Last Post: 03-14-2009, 08:21 AM -
Error: cannot resolve symbol' on Person (java.lang.String, java.lang.String)
By baltimore in forum New To JavaReplies: 2Last Post: 09-18-2008, 07:30 AM -
why we use public access modifier?
By vichet in forum New To JavaReplies: 1Last Post: 04-04-2008, 07:04 AM -
Help with Integer in java
By susan in forum New To JavaReplies: 1Last Post: 07-14-2007, 05:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks