How do I get a list of class modifiers?
You can get access modifier details about a class, fields, and methods via the Class object. The table below shows a list of all modifier constants defined in Java. When you call the getModifiers() method, it will return to you an int where the bits are set accordingly. For further information, see class java.lang.reflect.Modifier.
List of Modifiers
Num # | Name |
---|---|
1 | ABSTRACT |
2 | FINAL |
3 | INTERFACE |
4 | NATIVE |
5 | PRIVATE |
6 | PROTECTED |
7 | PUBLIC |
8 | STATIC |
9 | STRICT |
10 | SYNCHRONIZED |
11 | TRANSIENT |
12 | VOLATILE |
We can inspect any class and get it’s modifiers using the getModifiers() method
Recursively Getting All Modifiers for a Hierarchical Class Tree
This program will recursively get all the modifiers for the class and it’s inherited classes, all the methods and all the fields and their modifiers and display them to you. I decided to use a HashSet to capture all the methods and all the fields being returned to us by the calls to getMethods(), getDeclaredMethods(), getFields() and getDeclaredFields().
Getting Modifiers for a given Class
Class<?> mc = Class.forName("com.avaldes.tutorials.Player"); int mods = mc.getModifiers(); System.out.println("Details for com.avaldes.tutorials.Player..."); System.out.println(" CLASS modifiers are " + Modifier.toString(mods));
Getting Modifiers for all methods in a given Class
HashSet<Method> allMethods = new HashSet<Method>(); // Get all Methods and their modifiers System.out.println(" ALL METHODS :"); Method[] declaredMethods = mc.getDeclaredMethods(); Method[] publicMethods = mc.getMethods(); allMethods.addAll(Arrays.asList(declaredMethods)); allMethods.addAll(Arrays.asList(publicMethods)); for (Method method : allMethods) { int mods = method.getModifiers(); System.out.println(" METHODS are " + method.getName() + "==> " + Modifier.toString(mods)); }
Getting Modifiers for all fields in a given Class
HashSet<Field> allFields = new HashSet<Field>(); // Get all Fields and their modifiers System.out.println(" ALL FIELDS :"); Field[] declaredFields = mc.getDeclaredFields(); Field[] publicFields = mc.getFields(); allFields.addAll(Arrays.asList(declaredFields)); allFields.addAll(Arrays.asList(publicFields)); for (Field field : allFields) { int mods = field.getModifiers(); System.out.println(" FIELDS are " + field.getName() + "==> " + Modifier.toString(mods)); }
Full Program Listing
package com.avaldes.tutorials; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.HashSet; public class GetAllModifersExample { public static void testModifiers(Class mc) throws IllegalArgumentException, IllegalAccessException { HashSet<Field> allFields = new HashSet<Field>(); HashSet<Method> allMethods = new HashSet<Method>(); if (mc.getSuperclass() == null) { System.out.println("\nDetails for " + mc.getCanonicalName()); // Get Class modifiers int classMods = mc.getModifiers(); System.out.println(" CLASS Modifiers: " + Modifier.toString(classMods)); // Get all Methods and their modifiers System.out.println(" ALL METHODS :"); Method[] declaredMethods = mc.getDeclaredMethods(); Method[] publicMethods = mc.getMethods(); allMethods.addAll(Arrays.asList(declaredMethods)); allMethods.addAll(Arrays.asList(publicMethods)); for (Method method : allMethods) { int mods = method.getModifiers(); System.out.println(" METHODS are " + method.getName() + "==> " + Modifier.toString(mods)); } // Get all Fields and their modifiers System.out.println(" ALL FIELDS :"); Field[] declaredFields = mc.getDeclaredFields(); Field[] publicFields = mc.getFields(); allFields.addAll(Arrays.asList(declaredFields)); allFields.addAll(Arrays.asList(publicFields)); for (Field field : allFields) { int mods = field.getModifiers(); System.out.println(" FIELDS are " + field.getName() + "==> " + Modifier.toString(mods)); } return; } testModifiers(mc.getSuperclass()); System.out.println("\nDetails for " + mc.getCanonicalName()); // Get Class modifiers int classMods = mc.getModifiers(); System.out.println(" CLASS Modifiers: " + Modifier.toString(classMods)); // Get all Methods and their modifiers System.out.println(" ALL METHODS :"); Method[] declaredMethods = mc.getDeclaredMethods(); Method[] publicMethods = mc.getMethods(); allMethods.addAll(Arrays.asList(declaredMethods)); allMethods.addAll(Arrays.asList(publicMethods)); for (Method method : allMethods) { int mods = method.getModifiers(); System.out.println(" METHODS are " + method.getName() + "==> " + Modifier.toString(mods)); } // Get all Fields and their modifiers System.out.println(" ALL FIELDS :"); Field[] declaredFields = mc.getDeclaredFields(); Field[] publicFields = mc.getFields(); allFields.addAll(Arrays.asList(declaredFields)); allFields.addAll(Arrays.asList(publicFields)); for (Field field : allFields) { int mods = field.getModifiers(); field.setAccessible(true); System.out.println(" FIELDS are " + field.getName() + "==> " + Modifier.toString(mods)); } } public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException { testModifiers(MyConcreteClass.class); } }
Full Program Listing (MyConcreteClass.java)
package com.avaldes.tutorials; public class MyConcreteClass extends MySuperClass implements MyInterfaceClass { public String CONC_myPublicString = "This is myPublicString"; protected String CONC_myProtectedString = "This is myProtectedString";; private int CONC_myPrivateField = 123; private String CONC_myPrivateString = "This is myPrivateString"; @Override public void method1() { System.out.println("Inside method1..."); } @Override public void method2() { System.out.println("Inside method2..."); } @Override public void method3() { System.out.println("Inside method3..."); } }
Full Program Listing (MySuperClass.java)
package com.avaldes.tutorials; public class MySuperClass { public transient int SUPR_transientInt = 456; public volatile int SUPR_Concurrent_Access = 789; protected int getInt() { System.out.println("Inside of getInt()..."); return 1; } protected void sayHello() { System.out.println("Inside of sayHello()..."); } }
Full Program Listing (MyInterfaceClass.java)
package com.avaldes.tutorials; public interface MyInterfaceClass { public static final int INTF_MAX_NUM_FILES = 1001; public static int INTF_INT_COUNT = 0; public String INTF_welcome = "Welcome, please come back!"; public void method1(); public void method2(); void method3(); }
Output
Details for java.lang.Object CLASS Modifiers: public ALL METHODS : METHODS are equals==> public METHODS are notify==> public final native METHODS are getClass==> public final native METHODS are registerNatives==> private static native METHODS are clone==> protected native METHODS are notifyAll==> public final native METHODS are hashCode==> public native METHODS are finalize==> protected METHODS are toString==> public METHODS are wait==> public final METHODS are wait==> public final METHODS are wait==> public final native ALL FIELDS : Details for com.avaldes.tutorials.MySuperClass CLASS Modifiers: public ALL METHODS : METHODS are getInt==> protected METHODS are equals==> public METHODS are notify==> public final native METHODS are getClass==> public final native METHODS are sayHello==> protected METHODS are notifyAll==> public final native METHODS are hashCode==> public native METHODS are toString==> public METHODS are wait==> public final METHODS are wait==> public final METHODS are wait==> public final native ALL FIELDS : FIELDS are SUPR_transientInt==> public transient FIELDS are SUPR_Concurrent_Access==> public volatile Details for com.avaldes.tutorials.MyConcreteClass CLASS Modifiers: public ALL METHODS : METHODS are equals==> public METHODS are notify==> public final native METHODS are getClass==> public final native METHODS are method3==> public METHODS are method2==> public METHODS are method1==> public METHODS are notifyAll==> public final native METHODS are hashCode==> public native METHODS are toString==> public METHODS are wait==> public final METHODS are wait==> public final METHODS are wait==> public final native ALL FIELDS : FIELDS are SUPR_transientInt==> public transient FIELDS are INTF_welcome==> public static final FIELDS are CONC_myPrivateString==> private FIELDS are INTF_MAX_NUM_FILES==> public static final FIELDS are CONC_myPrivateField==> private FIELDS are INTF_INT_COUNT==> public static final FIELDS are CONC_myProtectedString==> protected FIELDS are SUPR_Concurrent_Access==> public volatile FIELDS are CONC_myPublicString==> public
Java Reflection Examples
- Class Example
Simple example shows you how to dynamically load and instantiate a class, get class name, package name, and superclass name. - Modifier Example
Showing you how to get information about classes, fields and methods using modifier method. - Method Example
Providing full details of how to get all methods, and invoke methods using reflection.
Frequently Asked Questions on Java Reflection API (FAQs)
- Different ways that I can get Class objects in Java using Reflection?
- How do I instantiate a class with a private constructor?
- How do I a list of all of the class's constructors?
- How do I list public methods of a class using Reflection?
- How do I list declared methods of a class using Reflection?
- How do I list all the methods of a class using Reflection?
- How do I get a list of class modifiers?
- Hacking Immutable class using Java Reflection
- Hacking the Immutable String class using Java Reflection
Leave a Reply