< prev index next >

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

Print this page




  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     /**




  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             StringJoiner sj = new StringJoiner(",", "(", ")");
 117             sj.setEmptyValue("()");
 118             for (Class<?> parameterType : parameterTypes) {
 119                 sj.add(parameterType.getTypeName());
 120             }
 121             sb.append(sj.toString());
 122 



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

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





 153                 }
 154                 sb.append(sj.toString());
 155             }
 156 
 157             specificToGenericStringHeader(sb);
 158 
 159             StringJoiner sj = new StringJoiner(",", "(", ")");
 160             sj.setEmptyValue("()");
 161 
 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 
 171             Type[] exceptionTypes = getGenericExceptionTypes();
 172             if (exceptionTypes.length > 0) {
 173                 StringJoiner joiner = new StringJoiner(",", " throws ", "");
 174                 for (Type exceptionType : exceptionTypes) {
 175                     joiner.add(exceptionType.getTypeName());





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


< prev index next >