Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7007539

java.reflect: Provide classes for formal parameters of Constructor and Method

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 7
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      A DESCRIPTION OF THE REQUEST :
      java.reflect: Please provide classes which would represent single formal parameters of Constructor and Method.

      I propose three classes for this. One abstract class which would generalize parameters of
      constructors and methods, and the concrete classes for method parameter and for constructor parameter.

      Please comment on the idea.

      Best regards,
      Ivan G Shevchenko (assembling DOT signals AT yandex DOT ru)

      = = = = = = = = = =
      MemberParameter
      = = = = = = = = = =

      import java.util.*;
      import java.lang.annotation.*;
      import java.lang.reflect.*;

      /**
       * Generalizes {@see MethodParameter} and {@see ConstructorParameter}.
       */
      public abstract class MemberParameter
      implements AnnotatedElement {

      protected final Member methodOrCtor;
      protected final int number;
      protected final Class<?> type;
      protected final Type genericType;
      protected final List<Annotation> annotations;

      MemberParameter(Member methodOrCtor, int number, Class<?> type,
      Type genericType, List<Annotation> annotations) {
      this.methodOrCtor = methodOrCtor;
      this.number = number;
      this.type = type;
      this.genericType = genericType;
      this.annotations = annotations;
      }

      public abstract Member getDeclarator();

      public Class<?> getType() {
      return type;
      }

      public Type getGenericType() {
      return genericType;
      }

      @Override
      public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
      for (Annotation a : annotations) {
      if (a.annotationType() == annotationClass) {
      return true;
      }
      }

      return false;
      }

      @Override
      @SuppressWarnings("unchecked")
      public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
      for (Annotation a : annotations) {
      if (a.annotationType() == annotationClass) {
      return (T) a;
      }
      }

      return null;
      }

      @Override
      public Annotation[] getAnnotations() {
      return annotations.toArray(new Annotation[]{});
      }

      /**
      * @return same as {@see #getAnnotations}
      */
      @Override
      public Annotation[] getDeclaredAnnotations() {
      return getAnnotations();
      }

      public List<Annotation> getAnnotationsAsList() {
      return annotations; // unmodifiable
      }

      // don't compare type, genericType and annotations: they're implied by method and param number
      @Override
      public final boolean equals(Object obj) {
      // trivial
      }

      // don't hash type, genericType and annotations: they're implied by method and param number
      @Override
      public final int hashCode() {
      // trivial
      }

      }

      = = = = = = = = = =
      MethodParameter
      = = = = = = = = = =

      import java.util.*;
      import java.lang.reflect.*;

      public final class MethodParameter
      extends MemberParameter {

      public static MethodParameter create(Method method, int number) {
      if (number >= method.getParameterTypes().length) {
      throw new IllegalArgumentException("method has "
      + method.getParameterTypes().length
      + " parameters, but given number is " + number);
      }

      return new MethodParameter(method, number);
      }

      public static List<MethodParameter> createList(Method method) {
      Class<?>[] params = method.getParameterTypes();
      int number = params.length;

      List<MethodParameter> ret = new ArrayList<>(number);
      for (int i = 0; i < number; ++i) {
      ret.add(new MethodParameter(method, number));
      }

      return Collections.unmodifiableList(ret);
      }

      private MethodParameter(Method method, int number) {
      super(method, number, method.getParameterTypes()[number],
      method.getGenericParameterTypes()[number],
      Collections.unmodifiableList(Arrays.asList(
      method.getParameterAnnotations()[number])));
      }

      @Override
      public Method getDeclarator() {
      return (Method) methodOrCtor;
      }

      }

      = = = = = = = = = =
      ConstructorParameter (basicaly, copy+paste of MethodParameter)
      = = = = = = = = = =

      import java.util.*;
      import java.lang.reflect.*;

      public final class ConstructorParameter
      extends MemberParameter {

      public static ConstructorParameter create(Constructor<?> ctor, int number) {
      if (number >= ctor.getParameterTypes().length) {
      throw new IllegalArgumentException("method has "
      + ctor.getParameterTypes().length
      + " parameters, but given number is " + number);
      }

      return new ConstructorParameter(ctor, number);
      }

      public static List<ConstructorParameter> createList(Constructor<?> ctor) {
      Class<?>[] params = ctor.getParameterTypes();
      int number = params.length;

      List<ConstructorParameter> ret = new ArrayList<>(number);
      for (int i = 0; i < number; ++i) {
      ret.add(new ConstructorParameter(ctor, number));
      }

      return Collections.unmodifiableList(ret);
      }

      private ConstructorParameter(Constructor<?> ctor, int number) {
      super(ctor, number, ctor.getParameterTypes()[number],
      ctor.getGenericParameterTypes()[number],
      Collections.unmodifiableList(Arrays.asList(
      ctor.getParameterAnnotations()[number])));
      }

      @Override
      public Constructor<?> getDeclarator() {
      return (Constructor<?>) methodOrCtor;
      }

      }

      JUSTIFICATION :
      Currently it is impossible to get OOP access of parameters, defined by Constructor and Method.
      OOP access of parameters would provide a more convinient way to store and equality-compare parameters.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                darcy Joe Darcy
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: