/****************************************************************************** * Copyright (C) 2016 Yevgeny Krasik * * * * 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. * ******************************************************************************/ package com.github.ykrasik.jaci.reflection; import java.lang.annotation.Annotation; /** * Encapsulates all possible interactions with the Java reflection API - required for GWT, which doesn't support * anything under java.lang.reflect * * @author Yevgeny Krasik */ public interface ReflectionAccessor { /** * Returns a {@code ReflectionMethod} object that reflects the specified * declared method of the class or interface represented by this * {@code Class} object. The {@code name} parameter is a * {@code String} that specifies the simple name of the desired * method, and the {@code parameterTypes} parameter is an array of * {@code Class} objects that identify the method's formal parameter * types, in declared order. If more than one method with the same * parameter types is declared in a class, and one of these methods has a * return type that is more specific than any of the others, that method is * returned; otherwise one of the methods is chosen arbitrarily. If the * name is "<init>"or "<clinit>" a {@code NoSuchMethodException} * is raised. * * <p> If this {@code Class} object represents an array type, then this * method does not find the {@code clone()} method. * * @param clazz class to get declared method from * @param name the name of the method * @param parameterTypes the parameter array * @return the {@code ReflectionMethod} object for the method of this class * matching the specified name and parameters * @throws NoSuchMethodException if a matching method is not found. * @throws NullPointerException if {@code name} is {@code null} * @throws SecurityException * If a security manager, <i>s</i>, is present and any of the * following conditions is met: * * <ul> * * <li> the caller's class loader is not the same as the * class loader of this class and invocation of * {@link SecurityManager#checkPermission * s.checkPermission} method with * {@code RuntimePermission("accessDeclaredMembers")} * denies access to the declared method * * <li> the caller's class loader is not the same as or an * ancestor of the class loader for the current class and * invocation of {@link SecurityManager#checkPackageAccess * s.checkPackageAccess()} denies access to the package * of this class * * </ul> * * @since JDK1.1 */ ReflectionMethod getDeclaredMethod(Class<?> clazz, String name, Class<?>... parameterTypes) throws Exception; /** * Returns an array containing {@code ReflectionMethod} objects reflecting all the * public methods of the class or interface represented by this {@code * Class} object, including those declared by the class or interface and * those inherited from superclasses and superinterfaces. * * <p> If this {@code Class} object represents a type that has multiple * public methods with the same name and parameter types, but different * return types, then the returned array has a {@code ReflectionMethod} object for * each such method. * * <p> If this {@code Class} object represents a type with a class * initialization method {@code <clinit>}, then the returned array does * <em>not</em> have a corresponding {@code ReflectionMethod} object. * * <p> If this {@code Class} object represents an array type, then the * returned array has a {@code ReflectionMethod} object for each of the public * methods inherited by the array type from {@code Object}. It does not * contain a {@code ReflectionMethod} object for {@code clone()}. * * <p> If this {@code Class} object represents an interface then the * returned array does not contain any implicitly declared methods from * {@code Object}. Therefore, if no methods are explicitly declared in * this interface or any of its superinterfaces then the returned array * has length 0. (Note that a {@code Class} object which represents a class * always has public methods, inherited from {@code Object}.) * * <p> If this {@code Class} object represents a primitive type or void, * then the returned array has length 0. * * <p> Static methods declared in superinterfaces of the class or interface * represented by this {@code Class} object are not considered members of * the class or interface. * * <p> The elements in the returned array are not sorted and are not in any * particular order. * * @param clazz Class to get methods from * @return the array of {@code ReflectionMethod} objects representing the * public methods of this class * @throws SecurityException * If a security manager, <i>s</i>, is present and * the caller's class loader is not the same as or an * ancestor of the class loader for the current class and * invocation of {@link SecurityManager#checkPackageAccess * s.checkPackageAccess()} denies access to the package * of this class. * * @since JDK1.1 */ ReflectionMethod[] getMethods(Class<?> clazz) throws SecurityException; /** * Returns an array of {@code Field} objects reflecting all the fields * declared by the class or interface represented by this * {@code Class} object. This includes public, protected, default * (package) access, and private fields, but excludes inherited fields. * * <p> If this {@code Class} object represents a class or interface with no * declared fields, then this method returns an array of length 0. * * <p> If this {@code Class} object represents an array type, a primitive * type, or void, then this method returns an array of length 0. * * <p> The elements in the returned array are not sorted and are not in any * particular order. * * @param clazz Class to get fields from * @return the array of {@code Field} objects representing all the * declared fields of this class * @throws SecurityException * If a security manager, <i>s</i>, is present and any of the * following conditions is met: * * <ul> * * <li> the caller's class loader is not the same as the * class loader of this class and invocation of * {@link SecurityManager#checkPermission * s.checkPermission} method with * {@code RuntimePermission("accessDeclaredMembers")} * denies access to the declared fields within this class * * <li> the caller's class loader is not the same as or an * ancestor of the class loader for the current class and * invocation of {@link SecurityManager#checkPackageAccess * s.checkPackageAccess()} denies access to the package * of this class * * </ul> * * @since JDK1.1 */ ReflectionField[] getDeclaredFields(Class<?> clazz) throws SecurityException; /** * Returns this element's annotation for the specified type if * such an annotation is <em>present</em>, else null. * * @param <T> the type of the annotation to query for and return if present * @param clazz Class to get annotation from * @param annotationClass the Class object corresponding to the * annotation type * @return this element's annotation for the specified annotation type if * present on this element, else null * @throws NullPointerException if the given annotation class is null * @since 1.5 */ <T extends Annotation> T getAnnotation(Class<?> clazz, Class<T> annotationClass); /** * Creates a new instance of the class represented by this {@code Class} * object. The class is instantiated as if by a {@code new} * expression with an empty argument list. The class is initialized if it * has not already been initialized. * * <p>Note that this method propagates any exception thrown by the * nullary constructor, including a checked exception. Use of * this method effectively bypasses the compile-time exception * checking that would otherwise be performed by the compiler. * The {@link * java.lang.reflect.Constructor#newInstance(java.lang.Object...) * Constructor.newInstance} method avoids this problem by wrapping * any exception thrown by the constructor in a (checked) {@link * java.lang.reflect.InvocationTargetException}. * * @param <T> class type * @param clazz Class to instantiate * @return a newly allocated instance of the class represented by this * object. * @throws IllegalAccessException if the class or its nullary * constructor is not accessible. * @throws InstantiationException * if this {@code Class} represents an abstract class, * an interface, an array class, a primitive type, or void; * or if the class has no nullary constructor; * or if the instantiation fails for some other reason. * @throws ExceptionInInitializerError if the initialization * provoked by this method fails. * @throws SecurityException * If a security manager, <i>s</i>, is present and * the caller's class loader is not the same as or an * ancestor of the class loader for the current class and * invocation of {@link SecurityManager#checkPackageAccess * s.checkPackageAccess()} denies access to the package * of this class. */ <T> T newInstance(Class<T> clazz) throws Exception; /** * Returns an array of {@code Class} objects reflecting all the * classes and interfaces declared as members of the class represented by * this {@code Class} object. This includes public, protected, default * (package) access, and private classes and interfaces declared by the * class, but excludes inherited classes and interfaces. This method * returns an array of length 0 if the class declares no classes or * interfaces as members, or if this {@code Class} object represents a * primitive type, an array class, or void. * * @param clazz Class to reflect inner classes from. * @return the array of {@code Class} objects representing all the * declared members of this class * @throws Exception * If a security manager, <i>s</i>, is present and any of the * following conditions is met: * * <ul> * * <li> the caller's class loader is not the same as the * class loader of this class and invocation of * {@link SecurityManager#checkPermission * s.checkPermission} method with * {@code RuntimePermission("accessDeclaredMembers")} * denies access to the declared classes within this class * * <li> the caller's class loader is not the same as or an * ancestor of the class loader for the current class and * invocation of {@link SecurityManager#checkPackageAccess * s.checkPackageAccess()} denies access to the package * of this class * * </ul> * * @since JDK1.1 */ Class<?>[] getDeclaredClasses(Class<?> clazz) throws Exception; /** * Returns a {@code Constructor} object that reflects the specified * constructor of the class or interface represented by this * {@code Class} object. The {@code parameterTypes} parameter is * an array of {@code Class} objects that identify the constructor's * formal parameter types, in declared order. * * If this {@code Class} object represents an inner class * declared in a non-static context, the formal parameter types * include the explicit enclosing instance as the first parameter. * * @param clazz Class to reflect constructor from. * @param parameterTypes the parameter array * @param <T> Class type * @return The {@code Constructor} object for the constructor with the * specified parameter list * @throws NoSuchMethodException if a matching method is not found. * @throws Exception * If a security manager, <i>s</i>, is present and any of the * following conditions is met: * * <ul> * * <li> the caller's class loader is not the same as the * class loader of this class and invocation of * {@link SecurityManager#checkPermission * s.checkPermission} method with * {@code RuntimePermission("accessDeclaredMembers")} * denies access to the declared constructor * * <li> the caller's class loader is not the same as or an * ancestor of the class loader for the current class and * invocation of {@link SecurityManager#checkPackageAccess * s.checkPackageAccess()} denies access to the package * of this class * * </ul> * * @since JDK1.1 */ <T> ReflectionConstructor<T> getDeclaredConstructor(Class<T> clazz, Class<?>... parameterTypes) throws Exception; /** * Determines if the class or interface represented by this * {@code Class} object is either the same as, or is a superclass or * superinterface of, the class or interface represented by the specified * {@code Class} parameter. It returns {@code true} if so; * otherwise it returns {@code false}. If this {@code Class} * object represents a primitive type, this method returns * {@code true} if the specified {@code Class} parameter is * exactly this {@code Class} object; otherwise it returns * {@code false}. * * <p> Specifically, this method tests whether the type represented by the * specified {@code Class} parameter can be converted to the type * represented by this {@code Class} object via an identity conversion * or via a widening reference conversion. See <em>The Java Language * Specification</em>, sections 5.1.1 and 5.1.4 , for details. * * @param c1 the {@code Class} object to be checked * @param c2 the {@code Class} object to be checked * @return the {@code boolean} value indicating whether objects of the * type {@code cls} can be assigned to objects of this class * @since JDK1.1 */ boolean isAssignableFrom(Class<?> c1, Class<?> c2); }