Java/Reflection/Modifier

Материал из Java эксперт
Перейти к: навигация, поиск

find an accessible method passed in

   <source lang="java">
  

import java.util.Map; import java.util.HashMap; import java.util.Collection; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.HashSet; import java.util.SortedSet; import java.util.TreeSet; import java.util.Queue; import java.util.LinkedList; import java.util.SortedMap; import java.util.TreeMap; import java.util.Arrays; import java.util.concurrent.ConcurrentHashMap; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import static java.lang.reflect.Modifier.isPublic; import java.beans.PropertyDescriptor; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.IntrospectionException; /**

* Common utilty methods that are useful when working with reflection.
*
* @author Tim Fennell
*/

public class ReflectUtil {

   /** A cache of property descriptors by class and property name */
   private static Map<Class<?>, Map<String, PropertyDescriptor>> propertyDescriptors =
           new ConcurrentHashMap<Class<?>, Map<String, PropertyDescriptor>>();
   /** Static helper class, shouldn"t be constructed. */
   private ReflectUtil() {}
   /**
    * Holds a map of commonly used interface types (mostly collections) to a class that
    * implements the interface and will, by default, be instantiated when an instance
    * of the interface is needed.
    */
   protected static final Map<Class<?>,Class<?>> interfaceImplementations = new HashMap<Class<?>,Class<?>>();
   /**
    * Holds a map of primitive type to the default value for that primitive type.  Isn"t it
    * odd that there"s no way to get this programmatically from the Class objects?
    */
   protected static final Map<Class<?>,Object> primitiveDefaults = new HashMap<Class<?>,Object>();
   static {
       interfaceImplementations.put(Collection.class, ArrayList.class);
       interfaceImplementations.put(List.class,       ArrayList.class);
       interfaceImplementations.put(Set.class,        HashSet.class);
       interfaceImplementations.put(SortedSet.class,  TreeSet.class);
       interfaceImplementations.put(Queue.class,      LinkedList.class);
       interfaceImplementations.put(Map.class,        HashMap.class);
       interfaceImplementations.put(SortedMap.class,  TreeMap.class);
       primitiveDefaults.put(Boolean.TYPE,    false);
       primitiveDefaults.put(Character.TYPE, "\0");
       primitiveDefaults.put(Byte.TYPE,       new Byte("0"));
       primitiveDefaults.put(Short.TYPE,      new Short("0"));
       primitiveDefaults.put(Integer.TYPE,    new Integer(0));
       primitiveDefaults.put(Long.TYPE,       new Long(0l));
       primitiveDefaults.put(Float.TYPE,      new Float(0f));
       primitiveDefaults.put(Double.TYPE,     new Double(0.0));
   }
   /**
    * The set of method that annotation classes inherit, and should be avoided when
    * toString()ing an annotation class.
    */
   private static final Set<String> INHERITED_ANNOTATION_METHODS =
           new HashSet(Arrays.asList("toString", "equals", "hashCode", "annotationType"));
   /**
    * Looks up the default implementing type for the supplied interface. This is done
    * based on a static map of known common interface types and implementing classes.
    *
    * @param iface an interface for which an implementing class is needed
    * @return a Class object representing the implementing type, or null if one is
    *         not found
    */
   public static Class<?> getImplementingClass(Class<?> iface) {
       return interfaceImplementations.get(iface);
   }
   /**
    * Attempts to determine an implementing class for the interface provided and instantiate
    * it using a default constructor.
    *
    * @param interfaceType an interface (or abstract class) to make an instance of
    * @return an instance of the interface type supplied
    * @throws InstantiationException if no implementation type has been configured
    * @throws IllegalAccessException if thrown by the JVM during class instantiation
    */
   @SuppressWarnings("unchecked")
 public static <T> T getInterfaceInstance(Class<T> interfaceType)
           throws InstantiationException, IllegalAccessException {
       Class impl = getImplementingClass(interfaceType);
       if (impl == null) {
           throw new InstantiationException(
                   "Stripes needed to instantiate a property who"s declared type as an " +
                   "interface (which obviously cannot be instantiated. The interface is not " +
                   "one that Stripes is aware of, so no implementing class was known. The " +
                   "interface type was: "" + interfaceType.getName() + "". To fix this " +
                   "you"ll need to do one of three things. 1) Change the getter/setter methods " +
                   "to use a concrete type so that Stripes can instantiate it. 2) in the bean"s " +
                   "setContext() method pre-instantiate the property so Stripes doesn"t have to. " +
                   "3) Bug the Stripes author ;)  If the interface is a JDK type it can easily be " +
                   "fixed. If not, if enough people ask, a generic way to handle the problem " +
                   "might get implemented.");
       }
       else {
           return (T) impl.newInstance();
       }
   }
   /**
    * Utility method used to load a class.  Any time that Stripes needs to load of find a
    * class by name it uses this method.  As a result any time the classloading strategy
    * needs to change it can be done in one place!  Currently uses
    * {@code Thread.currentThread().getContextClassLoader().loadClass(String)}.
    *
    * @param name the fully qualified (binary) name of the class to find or load
    * @return the Class object representing the class
    * @throws ClassNotFoundException if the class cannot be loaded
    */
   @SuppressWarnings("unchecked") // this allows us to assign without casting
 public static Class findClass(String name) throws ClassNotFoundException {
       return Thread.currentThread().getContextClassLoader().loadClass(name);
   }
   /**
*

A better (more concise) toString method for annotation types that yields a String * that should look more like the actual usage of the annotation in a class. The String produced * is similar to that produced by calling toString() on the annotation directly, with the * following differences:

    *
*
    *
  • Uses the classes simple name instead of it"s fully qualified name.
  • *
  • Only outputs attributes that are set to non-default values.
  •     *
    
    *

    If, for some unforseen reason, an exception is thrown within this method it will be * caught and the return value will be {@code ann.toString()}. * * @param ann the annotation to convert to a human readable String * @return a human readable String form of the annotation and it"s attributes */ public static String toString(Annotation ann) { try { Class<? extends Annotation> type = ann.annotationType(); StringBuilder builder = new StringBuilder(128); builder.append("@"); builder.append(type.getSimpleName()); boolean appendedAnyParameters = false; Method[] methods = type.getMethods(); for (Method method : methods) { if (!INHERITED_ANNOTATION_METHODS.contains(method.getName())) { Object defaultValue = method.getDefaultValue(); Object actualValue = method.invoke(ann); // If we have arrays, they have to be treated a little differently Object[] defaultArray = null, actualArray = null; if ( Object[].class.isAssignableFrom(method.getReturnType()) ) { defaultArray = (Object[]) defaultValue; actualArray = (Object[]) actualValue; } // Only print an attribute if it isn"t set to the default value if ( (defaultArray != null && !Arrays.equals(defaultArray, actualArray)) || (defaultArray == null && !actualValue.equals(defaultValue)) ) { if (appendedAnyParameters) { builder.append(", "); } else { builder.append("("); } builder.append(method.getName()); builder.append("="); if (actualArray != null) { builder.append( Arrays.toString(actualArray) ); } else { builder.append(actualValue); } appendedAnyParameters = true; } } } if (appendedAnyParameters) { builder.append(")"); } return builder.toString(); } catch (Exception e) { return ann.toString(); } } /** * Fetches all methods of all access types from the supplied class and super * classes. Methods that have been overridden in the inheritance hierarchy are * only returned once, using the instance lowest down the hierarchy. * * @param clazz the class to inspect * @return a collection of methods */ public static Collection<Method> getMethods(Class<?> clazz) { Collection<Method> found = new ArrayList<Method>(); while (clazz != null) { for (Method m1 : clazz.getDeclaredMethods()) { boolean overridden = false; for (Method m2 : found) { if ( m2.getName().equals(m1.getName()) && Arrays.deepEquals(m1.getParameterTypes(), m2.getParameterTypes())) { overridden = true; break; } } if (!overridden) found.add(m1); } clazz = clazz.getSuperclass(); } return found; } /** * Fetches all fields of all access types from the supplied class and super * classes. Fieldss that have been overridden in the inheritance hierarchy are * only returned once, using the instance lowest down the hierarchy. * * @param clazz the class to inspect * @return a collection of fields */ public static Collection<Field> getFields(Class<?> clazz) { Map<String,Field> fields = new HashMap<String, Field>(); while (clazz != null) { for (Field field : clazz.getDeclaredFields()) { if ( !fields.containsKey(field.getName()) ) { fields.put(field.getName(), field); } } clazz = clazz.getSuperclass(); } return fields.values(); } /** * Fetches the property descriptor for the named property of the supplied class. To * speed things up a cache is maintained of propertyName to PropertyDescriptor for * each class used with this method. If there is no property with the specified name, * returns null. * * @param clazz the class who"s properties to examine * @param property the String name of the property to look for * @return the PropertyDescriptor or null if none is found with a matching name */ public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String property) { Map<String,PropertyDescriptor> pds = propertyDescriptors.get(clazz); if (pds == null) { try { BeanInfo info = Introspector.getBeanInfo(clazz); PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); pds = new HashMap<String, PropertyDescriptor>(); for (PropertyDescriptor descriptor : descriptors) { pds.put(descriptor.getName(), descriptor); } propertyDescriptors.put(clazz, pds); } catch (IntrospectionException ie) { throw new RuntimeException("Could not examine class "" + clazz.getName() + "" using Introspector.getBeanInfo() to determine property information.", ie); } } return pds.get(property); } /** * <p>Attempts to find an accessible version of the method passed in, where accessible * is defined as the method itself being public and the declaring class being public. * Mostly useful as a workaround to the situation when * {@link PropertyDescriptor#getReadMethod()} and/or * {@link java.beans.PropertyDescriptor#getWriteMethod()} returns methods that are not * accessible (usually due to public implementations of interface methods in private * classes).

        *
    
    *

    Checks the method passed in and if it already meets these criteria it is returned * immediately. In general this leads to very little performance overhead

        *
    
    *

    If the method does not meet the criteria then the class" interfaces are scanned * for a matching method. If one is not found, then the class" superclass hierarchy * is searched. Finally, if no matching method can be found the original method is * returned.

        *
        * @param m a method that may or may not be accessible
        * @return either an accessible version of the same method, or the method passed in if
        *         an accessible version cannot be found
        */
       public static Method findAccessibleMethod(final Method m) {
           // If the passed in method is accessible, then just give it back.
           if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers())) return m;
           if (m.isAccessible()) return m;
           final Class<?> clazz    = m.getDeclaringClass();
           final String name    = m.getName();
           final Class<?>[] ptypes = m.getParameterTypes();
           // Else, loop through the interfaces for the declaring class, looking for a
           // public version of the method that we can call
           for (Class<?> iface : clazz.getInterfaces()) {
               try {
                   Method m2 = iface.getMethod(name, ptypes);
                   if (m2.isAccessible()) return m2;
                   if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers())) return m2;
               }
               catch (NoSuchMethodException nsme) { /* Not Unexpected. */ }
           }
           // Else loop through the superclasses looking for a public method
           Class<?> c = clazz.getSuperclass();
           while (c != null) {
               try {
                   Method m2 = c.getMethod(name, ptypes);
                   if (m2.isAccessible()) return m2;
                   if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers())) return m2;
               }
               catch (NoSuchMethodException nsme) { /* Not Unexpected. */ }
               c = c.getSuperclass();
           }
           // If we haven"t found anything at this point, just give up!
           return m;
       }
    
       /**
        * Looks for an instance (i.e. non-static) public field with the matching name and
        * returns it if one exists.  If no such field exists, returns null.
        *
        * @param clazz the clazz who"s fields to examine
        * @param property the name of the property/field to look for
        * @return the Field object or null if no matching field exists
        */
       public static Field getField(Class<?> clazz, String property) {
           try {
               Field field = clazz.getField(property);
               return !Modifier.isStatic(field.getModifiers()) ? field : null;
           }
           catch (NoSuchFieldException nsfe) {
               return null;
           }
       }
       /**
        * Returns an appropriate default value for the class supplied. Mirrors the defaults used
        * when the JVM initializes instance variables.
        *
        * @param clazz the class for which to find the default value
        * @return null for non-primitive types and an appropriate wrapper instance for primitives
        */
       public static Object getDefaultValue(Class<?> clazz) {
           if (clazz.isPrimitive()) {
               return primitiveDefaults.get(clazz);
           }
           else {
               return null;
           }
       }
       
       /**
        * Returns a set of all interfaces implemented by class supplied. This includes all
        * interfaces directly implemented by this class as well as those implemented by
        * superclasses or interface superclasses.
        * 
        * @param clazz
        * @return all interfaces implemented by this class
        */
       public static Set<Class<?>> getImplementedInterfaces(Class<?> clazz)
       {
           Set<Class<?>> interfaces = new HashSet<Class<?>>();
           
           if (clazz.isInterface())
               interfaces.add(clazz);
           while (clazz != null) {
               for (Class<?> iface : clazz.getInterfaces())
                   interfaces.addAll(getImplementedInterfaces(iface));
               clazz = clazz.getSuperclass();
           } 
           return interfaces;
       }
       /**
        * Returns an array of Type objects representing the actual type arguments
        * to targetType used by clazz.
        * 
        * @param clazz the implementing class (or subclass)
        * @param targetType the implemented generic class or interface
        * @return an array of Type objects or null
        */
       public static Type[] getActualTypeArguments(Class<?> clazz, Class<?> targetType) {
           Set<Class<?>> classes = new HashSet<Class<?>>();
           classes.add(clazz);
           if (targetType.isInterface())
               classes.addAll(getImplementedInterfaces(clazz));
           Class<?> superClass = clazz.getSuperclass();
           while (superClass != null) {
               classes.add(superClass);
               superClass = superClass.getSuperclass();
           }
           for (Class<?> search : classes) {
               for (Type type : (targetType.isInterface() ? search.getGenericInterfaces()
                       : new Type[] { search.getGenericSuperclass() })) {
                   if (type instanceof ParameterizedType) {
                       ParameterizedType parameterizedType = (ParameterizedType) type;
                       if (targetType.equals(parameterizedType.getRawType()))
                           return parameterizedType.getActualTypeArguments();
                   }
               }
           }
           return null;
       }
    

    }


     </source>
       
      
     
      
    



    Gets a method and forces it to be accessible, even if it is not.

       <source lang="java">
    
    

    import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main {

     /**
      * Gets a method and forces it to be accessible, even if it is not.
      *
      * @param clazz The class from which the method will be got.
      * @param methodName The name of the method.
      * @param parameterTypes The parameter types that the method must match.
      * @return The method, if it is found.
      * @since 2.0.7
      */
     public static Method getForcedAccessibleMethod(Class<?> clazz,
             String methodName, Class<?>... parameterTypes) {
         Method method;
         try {
             method = clazz.getMethod(methodName, parameterTypes);
         } catch (SecurityException e) {
             throw new RuntimeException("Cannot access method ""
                     + methodName + "" in class "" + clazz.getName()
                     + "" for security reasons", e);
         } catch (NoSuchMethodException e) {
             throw new RuntimeException("The method ""
                     + methodName + "" in class "" + clazz.getName()
                     + "" does not exist", e);
         }
         if (!method.isAccessible()) {
             method.setAccessible(true);
         }
         return method;
     }
    

    }

     </source>
       
      
     
      
    



    Is Accessible

       <source lang="java">
      
    

    /**

    * 
    * The ObjectStyle Group Software License, version 1.1
    * ObjectStyle Group - http://objectstyle.org/
    * 
    * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
    * of the software. All rights reserved.
    * 
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
    * 
    * 1. Redistributions of source code must retain the above copyright
    *    notice, this list of conditions and the following disclaimer.
    * 
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in
    *    the documentation and/or other materials provided with the
    *    distribution.
    * 
    * 3. The end-user documentation included with the redistribution, if any,
    *    must include the following acknowlegement:
    *    "This product includes software developed by independent contributors
    *    and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
    *    Alternately, this acknowlegement may appear in the software itself,
    *    if and wherever such third-party acknowlegements normally appear.
    * 
    * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
    *    or promote products derived from this software without prior written
    *    permission. For written permission, email
    *    "andrus at objectstyle dot org".
    * 
    * 5. Products derived from this software may not be called "ObjectStyle"
    *    or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
    *    names without prior written permission.
    * 
    * THIS SOFTWARE IS PROVIDED ``AS IS"" AND ANY EXPRESSED OR IMPLIED
    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    * DISCLAIMED.  IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    * SUCH DAMAGE.
    * 
    * 
    * This software consists of voluntary contributions made by many
    * individuals and hosted on ObjectStyle Group web site.  For more
    * information on the ObjectStyle Group, please see
    * <http://objectstyle.org/>.
    */
    

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import java.lang.reflect.Member; import java.lang.reflect.Modifier; import java.net.URL; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.ruparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.regex.Pattern; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.SAXException; import org.xml.sax.XMLReader; /**

    * Contains various unorganized static utility methods used across Cayenne.
    * 
    * @author Andrei Adamchik
    */
    

    public class Util {

     /**
      * Returns true if a Member is accessible via reflection under normal Java access
      * controls.
      * 
      * @since 1.2
      */
     public static boolean isAccessible(Member member) {
         return Modifier.isPublic(member.getModifiers())
                 && Modifier.isPublic(member.getDeclaringClass().getModifiers());
     }
    

    }


     </source>
       
      
     
      
    



    Listing the Modifiers of a Class Object

       <source lang="java">
       
    

    import java.lang.reflect.Modifier; public class Main {

     public static void main(String[] argv) throws Exception {
       int mods = String.class.getModifiers();
       if (Modifier.isPublic(mods)) {
         System.out.println("class is public");
       }
     }
    

    }



     </source>
       
      
     
      
    



    Listing the Modifiers of a Member Object: Field, Constructor, and Method are all subclasses of Member.

       <source lang="java">
       
    

    import java.lang.reflect.Modifier; public class Main {

     public static void main(String[] argv) throws Exception {
       int mods = String.class.getDeclaredMethods()[0].getModifiers();
       if (Modifier.isPublic(mods)) {
         System.out.println("member is public");
       }
     }
    

    }



     </source>
       
      
     
      
    



    Make field accessible, explicitly setting it accessible

       <source lang="java">
      
    

    /*

    * Copyright 2002-2008 the original author or authors.
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
    * Unless required by applicable law or agreed to in writing, software
    * distributed under the License is distributed on an "AS IS" BASIS,
    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
    

    import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.sql.SQLException; import java.util.Arrays; import java.util.LinkedList; import java.util.List; /**

    * Simple utility class for working with the reflection API and handling
    * reflection exceptions.
    *
    
    *

    Only intended for internal use. * * @author Juergen Hoeller * @author Rob Harrop * @author Rod Johnson * @author Costin Leau * @since 1.2.2 */ public abstract class ReflectionUtils { /** * Make the given field accessible, explicitly setting it accessible if necessary. * The setAccessible(true) method is only called when actually necessary, * to avoid unnecessary conflicts with a JVM SecurityManager (if active). * @param field the field to make accessible * @see java.lang.reflect.Field#setAccessible */ public static void makeAccessible(Field field) { if (!Modifier.isPublic(field.getModifiers()) ||  !Modifier.isPublic(field.getDeclaringClass().getModifiers())) { field.setAccessible(true); } } } </source>

    Overriding Default Access

       <source lang="java">
       
    

    import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main {

     public static void main(String[] argv) throws Exception {
       Class cls = java.lang.String.class;
       Method method = cls.getMethods()[0];
       Field field = cls.getFields()[0];
       Constructor constructor = cls.getConstructors()[0];
       field.setAccessible(true);
       constructor.setAccessible(true);
       method.setAccessible(true);
     }
    

    }



     </source>
       
      
     
      
    



    Return true if the integer argument includes the abstract modifier, false otherwise.

       <source lang="java">
       
    

    import java.lang.reflect.Modifier; public class Main {

     public static void main(String[] args) throws Exception {
       getClassModifier(String.class);
       getClassModifier(TestA.class);
       getClassModifier(TestB.class);
     }
     private static void getClassModifier(Class clazz) {
       int modifier = clazz.getModifiers();
       if (Modifier.isAbstract(modifier)) {
         System.out.println(clazz.getName() + " class modifier is abstract");
       }
     }
     protected static final class TestA {
     }
     private abstract class TestB {
     }
    

    }



     </source>
       
      
     
      
    



    Return true if the integer argument includes the final modifier, false otherwise.

       <source lang="java">
       
    

    import java.lang.reflect.Modifier; public class Main {

     public static void main(String[] args) throws Exception {
       getClassModifier(String.class);
       getClassModifier(TestA.class);
       getClassModifier(TestB.class);
     }
     private static void getClassModifier(Class clazz) {
       int modifier = clazz.getModifiers();
       if (Modifier.isFinal(modifier)) {
         System.out.println(clazz.getName() + " class modifier is final");
       }
     }
     protected static final class TestA {
     }
     private abstract class TestB {
     }
    

    }




     </source>
       
      
     
      
    



    Return true if the integer argument includes the private modifier, false otherwise.

       <source lang="java">
       
    

    import java.lang.reflect.Modifier; public class Main {

     public static void main(String[] args) throws Exception {
       getClassModifier(String.class);
       getClassModifier(TestA.class);
       getClassModifier(TestB.class);
     }
     private static void getClassModifier(Class clazz) {
       int modifier = clazz.getModifiers();
    
       if (Modifier.isPrivate(modifier)) {
         System.out.println(clazz.getName() + " class modifier is private");
       }
     }
     protected static final class TestA {
     }
     private abstract class TestB {
     }
    

    }




     </source>
       
      
     
      
    



    Return true if the integer argument includes the protected modifier,false otherwise.

       <source lang="java">
       
    

    import java.lang.reflect.Modifier; public class Main {

     public static void main(String[] args) throws Exception {
       getClassModifier(String.class);
       getClassModifier(TestA.class);
       getClassModifier(TestB.class);
     }
     private static void getClassModifier(Class clazz) {
       int modifier = clazz.getModifiers();
       if (Modifier.isProtected(modifier)) {
         System.out.println(clazz.getName() + " class modifier is protected");
       }
     }
     protected static final class TestA {
     }
     private abstract class TestB {
     }
    

    }




     </source>
       
      
     
      
    



    Return true if the integer argument includes the public modifier, false otherwise

       <source lang="java">
       
    

    import java.lang.reflect.Modifier; public class Main {

     public static void main(String[] args) throws Exception {
       getClassModifier(String.class);
       getClassModifier(TestA.class);
       getClassModifier(TestB.class);
     }
     private static void getClassModifier(Class clazz) {
       int modifier = clazz.getModifiers();
       if (Modifier.isPublic(modifier)) {
         System.out.println(clazz.getName() + " class modifier is public");
       }
     }
     protected static final class TestA {
     }
     private abstract class TestB {
     }
    

    }




     </source>
       
      
     
      
    



    Return true if the integer argument includes the static modifier, false otherwise.

       <source lang="java">
       
    

    import java.lang.reflect.Modifier; public class Main {

     public static void main(String[] args) throws Exception {
       getClassModifier(String.class);
       getClassModifier(TestA.class);
       getClassModifier(TestB.class);
     }
     private static void getClassModifier(Class clazz) {
       int modifier = clazz.getModifiers();
       if (Modifier.isStatic(modifier)) {
         System.out.println(clazz.getName() + " class modifier is static");
       }
     }
     protected static final class TestA {
     }
     private abstract class TestB {
     }
    

    }




     </source>
       
      
     
      
    



    Set Accessibility

       <source lang="java">
     
    

    // $Id: ReflectionHelper.java 16271 2009-04-07 20:20:12Z hardy.ferentschik $ /*

    • JBoss, Home of Professional Open Source
    • Copyright 2008, Red Hat Middleware LLC, and individual contributors
    • by the @authors tag. See the copyright.txt in the distribution for a
    • full listing of individual contributors.
    • Licensed under the Apache License, Version 2.0 (the "License");
    • you may not use this file except in compliance with the License.
    • You may obtain a copy of the License at
    • http://www.apache.org/licenses/LICENSE-2.0
    • Unless required by applicable law or agreed to in writing, software
    • distributed under the License is distributed on an "AS IS" BASIS,
    • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    • See the License for the specific language governing permissions and
    • limitations under the License.
    • /

    import java.beans.Introspector; import java.lang.annotation.Annotation; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Map;

    /**

    * Some reflection utility methods.
    *
    * @author Hardy Ferentschik
    */
    

    public class ReflectionHelper {

     public static void setAccessibility(Member member) {
       if ( !Modifier.isPublic( member.getModifiers() ) ) {
         //Sun"s ease of use, sigh...
         ( ( AccessibleObject ) member ).setAccessible( true );
       }
     }
    

    }


     </source>