< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 2012, 2017, 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() {}


  92             if (access_mod != 0)
  93                 sb.append(Modifier.toString(access_mod)).append(' ');
  94             if (isDefault)
  95                 sb.append("default ");
  96             mod = (mod & ~Modifier.ACCESS_MODIFIERS);
  97             if (mod != 0)
  98                 sb.append(Modifier.toString(mod)).append(' ');
  99         }
 100     }
 101 
 102     String sharedToString(int modifierMask,
 103                           boolean isDefault,
 104                           Class<?>[] parameterTypes,
 105                           Class<?>[] exceptionTypes) {
 106         try {
 107             StringBuilder sb = new StringBuilder();
 108 
 109             printModifiersIfNonzero(sb, modifierMask, isDefault);
 110             specificToStringHeader(sb);
 111             sb.append('(');
 112             StringJoiner sj = new StringJoiner(",");
 113             for (Class<?> parameterType : parameterTypes) {
 114                 sj.add(parameterType.getTypeName());
 115             }
 116             sb.append(sj.toString());
 117             sb.append(')');
 118 
 119             if (exceptionTypes.length > 0) {
 120                 StringJoiner joiner = new StringJoiner(",", " throws ", "");
 121                 for (Class<?> exceptionType : exceptionTypes) {
 122                     joiner.add(exceptionType.getTypeName());
 123                 }
 124                 sb.append(joiner.toString());
 125             }
 126             return sb.toString();
 127         } catch (Exception e) {
 128             return "<" + e + ">";
 129         }
 130     }
 131 
 132     /**
 133      * Generate toString header information specific to a method or
 134      * constructor.
 135      */
 136     abstract void specificToStringHeader(StringBuilder sb);
 137 











 138     String sharedToGenericString(int modifierMask, boolean isDefault) {
 139         try {
 140             StringBuilder sb = new StringBuilder();
 141 
 142             printModifiersIfNonzero(sb, modifierMask, isDefault);
 143 
 144             TypeVariable<?>[] typeparms = getTypeParameters();
 145             if (typeparms.length > 0) {
 146                 StringJoiner sj = new StringJoiner(",", "<", "> ");
 147                 for(TypeVariable<?> typeparm: typeparms) {
 148                     sj.add(typeparm.getTypeName());
 149                 }
 150                 sb.append(sj.toString());
 151             }
 152 
 153             specificToGenericStringHeader(sb);
 154 
 155             sb.append('(');
 156             StringJoiner sj = new StringJoiner(",");
 157             Type[] params = getGenericParameterTypes();
 158             for (int j = 0; j < params.length; j++) {
 159                 String param = params[j].getTypeName();
 160                 if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
 161                     param = param.replaceFirst("\\[\\]$", "...");
 162                 sj.add(param);
 163             }
 164             sb.append(sj.toString());
 165             sb.append(')');
 166 
 167             Type[] exceptionTypes = getGenericExceptionTypes();
 168             if (exceptionTypes.length > 0) {
 169                 StringJoiner joiner = new StringJoiner(",", " throws ", "");
 170                 for (Type exceptionType : exceptionTypes) {


   1 /*
   2  * Copyright (c) 2012, 2018, 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.Arrays;
  30 import java.util.Map;
  31 import java.util.Objects;
  32 import java.util.StringJoiner;
  33 import java.util.stream.Stream;
  34 import java.util.stream.Collectors;
  35 
  36 import jdk.internal.misc.SharedSecrets;
  37 import sun.reflect.annotation.AnnotationParser;
  38 import sun.reflect.annotation.AnnotationSupport;
  39 import sun.reflect.annotation.TypeAnnotationParser;
  40 import sun.reflect.annotation.TypeAnnotation;
  41 import sun.reflect.generics.repository.ConstructorRepository;
  42 
  43 /**
  44  * A shared superclass for the common functionality of {@link Method}
  45  * and {@link Constructor}.
  46  *
  47  * @since 1.8
  48  */
  49 public abstract class Executable extends AccessibleObject
  50     implements Member, GenericDeclaration {
  51     /*
  52      * Only grant package-visibility to the constructor.
  53      */
  54     Executable() {}


  95             if (access_mod != 0)
  96                 sb.append(Modifier.toString(access_mod)).append(' ');
  97             if (isDefault)
  98                 sb.append("default ");
  99             mod = (mod & ~Modifier.ACCESS_MODIFIERS);
 100             if (mod != 0)
 101                 sb.append(Modifier.toString(mod)).append(' ');
 102         }
 103     }
 104 
 105     String sharedToString(int modifierMask,
 106                           boolean isDefault,
 107                           Class<?>[] parameterTypes,
 108                           Class<?>[] exceptionTypes) {
 109         try {
 110             StringBuilder sb = new StringBuilder();
 111 
 112             printModifiersIfNonzero(sb, modifierMask, isDefault);
 113             specificToStringHeader(sb);
 114             sb.append('(');
 115 
 116             sb.append(Stream.of(parameterTypes).map(p -> p.getTypeName()).
 117                       collect(Collectors.joining(",")));
 118 

 119             sb.append(')');
 120 
 121             if (exceptionTypes.length > 0) {
 122                 sb.append(Stream.of(exceptionTypes).map(e -> e.getTypeName()).
 123                           collect(Collectors.joining(",", " throws ", "")));



 124             }
 125             return sb.toString();
 126         } catch (Exception e) {
 127             return "<" + e + ">";
 128         }
 129     }
 130 
 131     /**
 132      * Generate toString header information specific to a method or
 133      * constructor.
 134      */
 135     abstract void specificToStringHeader(StringBuilder sb);
 136 
 137     String typeVarBounds(TypeVariable<?> typeVar) {
 138         Type[] bounds = typeVar.getBounds();
 139         if (bounds.length == 1 && bounds[0].equals(Object.class)) {
 140             return typeVar.getName();
 141         } else {
 142             return typeVar.getName() + " extends " +
 143                 Stream.of(bounds).map(e -> e.getTypeName()).
 144                 collect(Collectors.joining(" & "));
 145         }
 146     }
 147 
 148     String sharedToGenericString(int modifierMask, boolean isDefault) {
 149         try {
 150             StringBuilder sb = new StringBuilder();
 151 
 152             printModifiersIfNonzero(sb, modifierMask, isDefault);
 153 
 154             TypeVariable<?>[] typeparms = getTypeParameters();
 155             if (typeparms.length > 0) {
 156                 sb.append(Stream.of(typeparms).map(t -> typeVarBounds(t)).
 157                           collect(Collectors.joining(",", "<", "> ")));



 158             }
 159 
 160             specificToGenericStringHeader(sb);
 161 
 162             sb.append('(');
 163             StringJoiner sj = new StringJoiner(",");
 164             Type[] params = getGenericParameterTypes();
 165             for (int j = 0; j < params.length; j++) {
 166                 String param = params[j].getTypeName();
 167                 if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
 168                     param = param.replaceFirst("\\[\\]$", "...");
 169                 sj.add(param);
 170             }
 171             sb.append(sj.toString());
 172             sb.append(')');
 173 
 174             Type[] exceptionTypes = getGenericExceptionTypes();
 175             if (exceptionTypes.length > 0) {
 176                 StringJoiner joiner = new StringJoiner(",", " throws ", "");
 177                 for (Type exceptionType : exceptionTypes) {


< prev index next >