Package com.artemis.utils.reflect

Source Code of com.artemis.utils.reflect.Method

/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* 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.artemis.utils.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Arrays;

import com.artemis.gwtref.client.Parameter;
import com.artemis.utils.reflect.ReflectionException;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/** Provides information about, and access to, a single method on a class or interface.
* @author nexsoftware */
public final class Method {

  private final com.artemis.gwtref.client.Method method;

  Method (com.artemis.gwtref.client.Method method) {
    this.method = method;
  }

  /** Returns the name of the method. */
  public String getName () {
    return method.getName();
  }

  /** Returns a Class object that represents the formal return type of the method. */
  public Class getReturnType () {
    return method.getReturnType();
  }

  /** Returns an array of Class objects that represent the formal parameter types, in declaration order, of the method. */
  public Class[] getParameterTypes () {
    Parameter[] parameters = method.getParameters();
    Class[] parameterTypes = new Class[parameters.length];
    for (int i = 0, j = parameters.length; i < j; i++) {
      parameterTypes[i] = parameters[i].getType();
    }
    return parameterTypes;
  }

  /** Returns the Class object representing the class or interface that declares the method. */
  public Class getDeclaringClass () {
    return method.getEnclosingType();
  }

  public boolean isAccessible () {
    return method.isPublic();
  }

  public void setAccessible (boolean accessible) {
    // NOOP in GWT
  }

  /** Return true if the method includes the {@code abstract} modifier. */
  public boolean isAbstract () {
    return method.isAbstract();
  }

  /** Return true if the method does not include any of the {@code private}, {@code protected}, or {@code public} modifiers. */
  public boolean isDefaultAccess () {
    return !isPrivate() && !isProtected() && !isPublic();
  }

  /** Return true if the method includes the {@code final} modifier. */
  public boolean isFinal () {
    return method.isFinal();
  }

  /** Return true if the method includes the {@code private} modifier. */
  public boolean isPrivate () {
    return method.isPrivate();
  }

  /** Return true if the method includes the {@code protected} modifier. */
  public boolean isProtected () {
    return method.isProtected();
  }

  /** Return true if the method includes the {@code public} modifier. */
  public boolean isPublic () {
    return method.isPublic();
  }

  /** Return true if the method includes the {@code native} modifier. */
  public boolean isNative () {
    return method.isNative();
  }

  /** Return true if the method includes the {@code static} modifier. */
  public boolean isStatic () {
    return method.isStatic();
  }

  /** Return true if the method takes a variable number of arguments. */
  public boolean isVarArgs () {
    return method.isVarArgs();
  }

  /** Invokes the underlying method on the supplied object with the supplied parameters. */
  public Object invoke (Object obj, Object... args) throws ReflectionException {
    try {
      return method.invoke(obj, args);
    } catch (IllegalArgumentException e) {
      throw new ReflectionException("Illegal argument(s) supplied to method: " + getName(), e);
    }
  }

  @SuppressWarnings("rawtypes")
  @Deprecated
  public boolean hasAnnotation(Class annotationClass) {
    return isAnnotationPresent(annotationClass);
  }

  /** Returns this element's annotation for the specified type if such an annotation is present, else null. */
  @Deprecated
  public <T extends java.lang.annotation.Annotation>T getAnnotation(Class<T> annotationClass) {
    final Annotation declaredAnnotation = getDeclaredAnnotation(annotationClass);
    return declaredAnnotation != null ? declaredAnnotation.getAnnotation(annotationClass) : null;
  }

  /** Returns true if the field includes an annotation of the provided class type. */
  public boolean isAnnotationPresent (Class<? extends java.lang.annotation.Annotation> annotationType) {
    java.lang.annotation.Annotation[] annotations = method.getDeclaredAnnotations();
    for (java.lang.annotation.Annotation annotation : annotations) {
      if (annotation.annotationType().equals(annotationType)) {
        return true;
      }
    }
    return false;
  }

  /** Returns an array of {@link Annotation} objects reflecting all annotations declared by this field,
   * or an empty array if there are none. Does not include inherited annotations. */
  public Annotation[] getDeclaredAnnotations () {
    java.lang.annotation.Annotation[] annotations = method.getDeclaredAnnotations();
    Annotation[] result = new Annotation[annotations.length];
    for (int i = 0; i < annotations.length; i++) {
      result[i] = new Annotation(annotations[i]);
    }
    return result;
  }

  /** Returns an {@link Annotation} object reflecting the annotation provided, or null of this field doesn't
   * have such an annotation. This is a convenience function if the caller knows already which annotation
   * type he's looking for. */
  public Annotation getDeclaredAnnotation (Class<? extends java.lang.annotation.Annotation> annotationType) {
    java.lang.annotation.Annotation[] annotations = method.getDeclaredAnnotations();
    for (java.lang.annotation.Annotation annotation : annotations) {
      if (annotation.annotationType().equals(annotationType)) {
        return new Annotation(annotation);
      }
    }
    return null;
  }
}
TOP

Related Classes of com.artemis.utils.reflect.Method

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.