< prev index next >

src/java.base/share/classes/java/lang/reflect/Executable.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import java.lang.annotation.*;
  29 import java.util.Map;
  30 import java.util.Objects;

  31 
  32 import jdk.internal.misc.SharedSecrets;
  33 import sun.reflect.annotation.AnnotationParser;
  34 import sun.reflect.annotation.AnnotationSupport;
  35 import sun.reflect.annotation.TypeAnnotationParser;
  36 import sun.reflect.annotation.TypeAnnotation;
  37 import sun.reflect.generics.repository.ConstructorRepository;
  38 
  39 /**
  40  * A shared superclass for the common functionality of {@link Method}
  41  * and {@link Constructor}.
  42  *
  43  * @since 1.8
  44  */
  45 public abstract class Executable extends AccessibleObject
  46     implements Member, GenericDeclaration {
  47     /*
  48      * Only grant package-visibility to the constructor.
  49      */
  50     Executable() {}


  69     boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) {
  70         /* Avoid unnecessary cloning */
  71         if (params1.length == params2.length) {
  72             for (int i = 0; i < params1.length; i++) {
  73                 if (params1[i] != params2[i])
  74                     return false;
  75             }
  76             return true;
  77         }
  78         return false;
  79     }
  80 
  81     Annotation[][] parseParameterAnnotations(byte[] parameterAnnotations) {
  82         return AnnotationParser.parseParameterAnnotations(
  83                parameterAnnotations,
  84                SharedSecrets.getJavaLangAccess().
  85                getConstantPool(getDeclaringClass()),
  86                getDeclaringClass());
  87     }
  88 
  89     void separateWithCommas(Class<?>[] types, StringBuilder sb) {
  90         for (int j = 0; j < types.length; j++) {
  91             sb.append(types[j].getTypeName());
  92             if (j < (types.length - 1))
  93                 sb.append(",");
  94         }
  95 
  96     }
  97 
  98     void printModifiersIfNonzero(StringBuilder sb, int mask, boolean isDefault) {
  99         int mod = getModifiers() & mask;
 100 
 101         if (mod != 0 && !isDefault) {
 102             sb.append(Modifier.toString(mod)).append(' ');
 103         } else {
 104             int access_mod = mod & Modifier.ACCESS_MODIFIERS;
 105             if (access_mod != 0)
 106                 sb.append(Modifier.toString(access_mod)).append(' ');
 107             if (isDefault)
 108                 sb.append("default ");
 109             mod = (mod & ~Modifier.ACCESS_MODIFIERS);
 110             if (mod != 0)
 111                 sb.append(Modifier.toString(mod)).append(' ');
 112         }
 113     }
 114 
 115     String sharedToString(int modifierMask,
 116                           boolean isDefault,
 117                           Class<?>[] parameterTypes,
 118                           Class<?>[] exceptionTypes) {
 119         try {
 120             StringBuilder sb = new StringBuilder();
 121 
 122             printModifiersIfNonzero(sb, modifierMask, isDefault);
 123             specificToStringHeader(sb);
 124 
 125             sb.append('(');
 126             separateWithCommas(parameterTypes, sb);




 127             sb.append(')');

 128             if (exceptionTypes.length > 0) {
 129                 sb.append(" throws ");
 130                 separateWithCommas(exceptionTypes, sb);



 131             }
 132             return sb.toString();
 133         } catch (Exception e) {
 134             return "<" + e + ">";
 135         }
 136     }
 137 
 138     /**
 139      * Generate toString header information specific to a method or
 140      * constructor.
 141      */
 142     abstract void specificToStringHeader(StringBuilder sb);
 143 
 144     String sharedToGenericString(int modifierMask, boolean isDefault) {
 145         try {
 146             StringBuilder sb = new StringBuilder();
 147 
 148             printModifiersIfNonzero(sb, modifierMask, isDefault);
 149 
 150             TypeVariable<?>[] typeparms = getTypeParameters();
 151             if (typeparms.length > 0) {
 152                 boolean first = true;
 153                 sb.append('<');
 154                 for(TypeVariable<?> typeparm: typeparms) {
 155                     if (!first)
 156                         sb.append(',');
 157                     // Class objects can't occur here; no need to test
 158                     // and call Class.getName().
 159                     sb.append(typeparm.toString());
 160                     first = false;
 161                 }
 162                 sb.append("> ");
 163             }
 164 
 165             specificToGenericStringHeader(sb);
 166 
 167             sb.append('(');

 168             Type[] params = getGenericParameterTypes();
 169             for (int j = 0; j < params.length; j++) {
 170                 String param = params[j].getTypeName();
 171                 if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
 172                     param = param.replaceFirst("\\[\\]$", "...");
 173                 sb.append(param);
 174                 if (j < (params.length - 1))
 175                     sb.append(',');
 176             }

 177             sb.append(')');
 178             Type[] exceptions = getGenericExceptionTypes();
 179             if (exceptions.length > 0) {
 180                 sb.append(" throws ");
 181                 for (int k = 0; k < exceptions.length; k++) {
 182                     sb.append((exceptions[k] instanceof Class)?
 183                               ((Class)exceptions[k]).getName():
 184                               exceptions[k].toString());
 185                     if (k < (exceptions.length - 1))
 186                         sb.append(',');
 187                 }

 188             }
 189             return sb.toString();
 190         } catch (Exception e) {
 191             return "<" + e + ">";
 192         }
 193     }
 194 
 195     /**
 196      * Generate toGenericString header information specific to a
 197      * method or constructor.
 198      */
 199     abstract void specificToGenericStringHeader(StringBuilder sb);
 200 
 201     /**
 202      * Returns the {@code Class} object representing the class or interface
 203      * that declares the executable represented by this object.
 204      */
 205     public abstract Class<?> getDeclaringClass();
 206 
 207     /**


   1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.reflect;
  27 
  28 import java.lang.annotation.*;
  29 import java.util.Map;
  30 import java.util.Objects;
  31 import java.util.StringJoiner;
  32 
  33 import jdk.internal.misc.SharedSecrets;
  34 import sun.reflect.annotation.AnnotationParser;
  35 import sun.reflect.annotation.AnnotationSupport;
  36 import sun.reflect.annotation.TypeAnnotationParser;
  37 import sun.reflect.annotation.TypeAnnotation;
  38 import sun.reflect.generics.repository.ConstructorRepository;
  39 
  40 /**
  41  * A shared superclass for the common functionality of {@link Method}
  42  * and {@link Constructor}.
  43  *
  44  * @since 1.8
  45  */
  46 public abstract class Executable extends AccessibleObject
  47     implements Member, GenericDeclaration {
  48     /*
  49      * Only grant package-visibility to the constructor.
  50      */
  51     Executable() {}


  70     boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) {
  71         /* Avoid unnecessary cloning */
  72         if (params1.length == params2.length) {
  73             for (int i = 0; i < params1.length; i++) {
  74                 if (params1[i] != params2[i])
  75                     return false;
  76             }
  77             return true;
  78         }
  79         return false;
  80     }
  81 
  82     Annotation[][] parseParameterAnnotations(byte[] parameterAnnotations) {
  83         return AnnotationParser.parseParameterAnnotations(
  84                parameterAnnotations,
  85                SharedSecrets.getJavaLangAccess().
  86                getConstantPool(getDeclaringClass()),
  87                getDeclaringClass());
  88     }
  89 









  90     void printModifiersIfNonzero(StringBuilder sb, int mask, boolean isDefault) {
  91         int mod = getModifiers() & mask;
  92 
  93         if (mod != 0 && !isDefault) {
  94             sb.append(Modifier.toString(mod)).append(' ');
  95         } else {
  96             int access_mod = mod & Modifier.ACCESS_MODIFIERS;
  97             if (access_mod != 0)
  98                 sb.append(Modifier.toString(access_mod)).append(' ');
  99             if (isDefault)
 100                 sb.append("default ");
 101             mod = (mod & ~Modifier.ACCESS_MODIFIERS);
 102             if (mod != 0)
 103                 sb.append(Modifier.toString(mod)).append(' ');
 104         }
 105     }
 106 
 107     String sharedToString(int modifierMask,
 108                           boolean isDefault,
 109                           Class<?>[] parameterTypes,
 110                           Class<?>[] exceptionTypes) {
 111         try {
 112             StringBuilder sb = new StringBuilder();
 113 
 114             printModifiersIfNonzero(sb, modifierMask, isDefault);
 115             specificToStringHeader(sb);

 116             sb.append('(');
 117             StringJoiner sj = new StringJoiner(",");
 118             for (Class<?> parameterType : parameterTypes) {
 119                 sj.add(parameterType.getTypeName());
 120             }
 121             sb.append(sj.toString());
 122             sb.append(')');
 123 
 124             if (exceptionTypes.length > 0) {
 125                 StringJoiner joiner = new StringJoiner(",", "throws ", "");
 126                 for (Class<?> exceptionType : exceptionTypes) {
 127                     joiner.add(exceptionType.getTypeName());
 128                 }
 129                 sb.append(joiner.toString());
 130             }
 131             return sb.toString();
 132         } catch (Exception e) {
 133             return "<" + e + ">";
 134         }
 135     }
 136 
 137     /**
 138      * Generate toString header information specific to a method or
 139      * constructor.
 140      */
 141     abstract void specificToStringHeader(StringBuilder sb);
 142 
 143     String sharedToGenericString(int modifierMask, boolean isDefault) {
 144         try {
 145             StringBuilder sb = new StringBuilder();
 146 
 147             printModifiersIfNonzero(sb, modifierMask, isDefault);
 148 
 149             TypeVariable<?>[] typeparms = getTypeParameters();
 150             if (typeparms.length > 0) {
 151                 StringJoiner sj = new StringJoiner(",", "<", "> ");

 152                 for(TypeVariable<?> typeparm: typeparms) { 
 153                     sj.add(typeparm.getTypeName());





 154                 }
 155                 sb.append(sj.toString());
 156             }
 157 
 158             specificToGenericStringHeader(sb);
 159 
 160             sb.append('(');
 161             StringJoiner sj = new StringJoiner(",");
 162             Type[] params = getGenericParameterTypes();
 163             for (int j = 0; j < params.length; j++) {
 164                 String param = params[j].getTypeName();
 165                 if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
 166                     param = param.replaceFirst("\\[\\]$", "...");
 167                 sj.add(param);


 168             }
 169             sb.append(sj.toString());
 170             sb.append(')');
 171 
 172             Type[] exceptionTypes = getGenericExceptionTypes();
 173             if (exceptionTypes.length > 0) {
 174                 StringJoiner joiner = new StringJoiner(",", " throws ", "");
 175                 for (Type exceptionType : exceptionTypes) {
 176                     joiner.add(exceptionType.getTypeName());



 177                 }
 178                 sb.append(joiner.toString());
 179             }
 180             return sb.toString();
 181         } catch (Exception e) {
 182             return "<" + e + ">";
 183         }
 184     }
 185 
 186     /**
 187      * Generate toGenericString header information specific to a
 188      * method or constructor.
 189      */
 190     abstract void specificToGenericStringHeader(StringBuilder sb);
 191 
 192     /**
 193      * Returns the {@code Class} object representing the class or interface
 194      * that declares the executable represented by this object.
 195      */
 196     public abstract Class<?> getDeclaringClass();
 197 
 198     /**


< prev index next >