rt/emul/mini/src/main/java/java/lang/reflect/Constructor.java
branchjavac
changeset 1321 7a78a84ab583
parent 772 d382dacfd73f
child 1377 5e8f58b48380
     1.1 --- a/rt/emul/mini/src/main/java/java/lang/reflect/Constructor.java	Tue Feb 26 16:54:16 2013 +0100
     1.2 +++ b/rt/emul/mini/src/main/java/java/lang/reflect/Constructor.java	Sat Sep 28 12:03:59 2013 +0200
     1.3 @@ -26,6 +26,10 @@
     1.4  package java.lang.reflect;
     1.5  
     1.6  import java.lang.annotation.Annotation;
     1.7 +import static java.lang.reflect.Method.fromPrimitive;
     1.8 +import static java.lang.reflect.Method.getAccess;
     1.9 +import static java.lang.reflect.Method.getParameterTypes;
    1.10 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.11  import org.apidesign.bck2brwsr.emul.reflect.TypeProvider;
    1.12  
    1.13  /**
    1.14 @@ -53,44 +57,20 @@
    1.15                                                      GenericDeclaration,
    1.16                                                      Member {
    1.17  
    1.18 -    private Class<T>            clazz;
    1.19 -    private int                 slot;
    1.20 -    private Class<?>[]          parameterTypes;
    1.21 -    private Class<?>[]          exceptionTypes;
    1.22 -    private int                 modifiers;
    1.23 -    // Generics and annotations support
    1.24 -    private transient String    signature;
    1.25 -    private byte[]              annotations;
    1.26 -    private byte[]              parameterAnnotations;
    1.27 -
    1.28 -
    1.29 -    // For sharing of ConstructorAccessors. This branching structure
    1.30 -    // is currently only two levels deep (i.e., one root Constructor
    1.31 -    // and potentially many Constructor objects pointing to it.)
    1.32 -    private Constructor<T>      root;
    1.33 +    private final Class<T> clazz;
    1.34 +    private final Object data;
    1.35 +    private final String sig;
    1.36  
    1.37      /**
    1.38       * Package-private constructor used by ReflectAccess to enable
    1.39       * instantiation of these objects in Java code from the java.lang
    1.40       * package via sun.reflect.LangReflectAccess.
    1.41       */
    1.42 -    Constructor(Class<T> declaringClass,
    1.43 -                Class<?>[] parameterTypes,
    1.44 -                Class<?>[] checkedExceptions,
    1.45 -                int modifiers,
    1.46 -                int slot,
    1.47 -                String signature,
    1.48 -                byte[] annotations,
    1.49 -                byte[] parameterAnnotations)
    1.50 +    Constructor(Class<T> declaringClass, Object data, String sig)
    1.51      {
    1.52          this.clazz = declaringClass;
    1.53 -        this.parameterTypes = parameterTypes;
    1.54 -        this.exceptionTypes = checkedExceptions;
    1.55 -        this.modifiers = modifiers;
    1.56 -        this.slot = slot;
    1.57 -        this.signature = signature;
    1.58 -        this.annotations = annotations;
    1.59 -        this.parameterAnnotations = parameterAnnotations;
    1.60 +        this.data = data;
    1.61 +        this.sig = sig;
    1.62      }
    1.63  
    1.64      /**
    1.65 @@ -126,7 +106,7 @@
    1.66       * @see Modifier
    1.67       */
    1.68      public int getModifiers() {
    1.69 -        return modifiers;
    1.70 +        return getAccess(data);
    1.71      }
    1.72  
    1.73      /**
    1.74 @@ -159,7 +139,7 @@
    1.75       * represents
    1.76       */
    1.77      public Class<?>[] getParameterTypes() {
    1.78 -        return (Class<?>[]) parameterTypes.clone();
    1.79 +        return Method.getParameterTypes(sig);
    1.80      }
    1.81  
    1.82  
    1.83 @@ -205,7 +185,7 @@
    1.84       * constructor this object represents
    1.85       */
    1.86      public Class<?>[] getExceptionTypes() {
    1.87 -        return (Class<?>[])exceptionTypes.clone();
    1.88 +        throw new UnsupportedOperationException();
    1.89      }
    1.90  
    1.91  
    1.92 @@ -242,20 +222,9 @@
    1.93       * same formal parameter types.
    1.94       */
    1.95      public boolean equals(Object obj) {
    1.96 -        if (obj != null && obj instanceof Constructor) {
    1.97 -            Constructor<?> other = (Constructor<?>)obj;
    1.98 -            if (getDeclaringClass() == other.getDeclaringClass()) {
    1.99 -                /* Avoid unnecessary cloning */
   1.100 -                Class<?>[] params1 = parameterTypes;
   1.101 -                Class<?>[] params2 = other.parameterTypes;
   1.102 -                if (params1.length == params2.length) {
   1.103 -                    for (int i = 0; i < params1.length; i++) {
   1.104 -                        if (params1[i] != params2[i])
   1.105 -                            return false;
   1.106 -                    }
   1.107 -                    return true;
   1.108 -                }
   1.109 -            }
   1.110 +        if (obj instanceof Constructor) {
   1.111 +            Constructor other = (Constructor)obj;
   1.112 +            return data == other.data;
   1.113          }
   1.114          return false;
   1.115      }
   1.116 @@ -293,13 +262,14 @@
   1.117              }
   1.118              sb.append(Field.getTypeName(getDeclaringClass()));
   1.119              sb.append("(");
   1.120 -            Class<?>[] params = parameterTypes; // avoid clone
   1.121 +            Class<?>[] params = getParameterTypes(); // avoid clone
   1.122              for (int j = 0; j < params.length; j++) {
   1.123                  sb.append(Field.getTypeName(params[j]));
   1.124                  if (j < (params.length - 1))
   1.125                      sb.append(",");
   1.126              }
   1.127              sb.append(")");
   1.128 +            /*
   1.129              Class<?>[] exceptions = exceptionTypes; // avoid clone
   1.130              if (exceptions.length > 0) {
   1.131                  sb.append(" throws ");
   1.132 @@ -309,6 +279,7 @@
   1.133                          sb.append(",");
   1.134                  }
   1.135              }
   1.136 +            */
   1.137              return sb.toString();
   1.138          } catch (Exception e) {
   1.139              return "<" + e + ">";
   1.140 @@ -452,9 +423,29 @@
   1.141          throws InstantiationException, IllegalAccessException,
   1.142                 IllegalArgumentException, InvocationTargetException
   1.143      {
   1.144 -        throw new SecurityException();
   1.145 +        Class[] types = getParameterTypes();
   1.146 +        if (types.length != initargs.length) {
   1.147 +            throw new IllegalArgumentException("Types len " + types.length + " args: " + initargs.length);
   1.148 +        } else {
   1.149 +            initargs = initargs.clone();
   1.150 +            for (int i = 0; i < types.length; i++) {
   1.151 +                Class c = types[i];
   1.152 +                if (c.isPrimitive() && initargs[i] != null) {
   1.153 +                    initargs[i] = Method.toPrimitive(initargs[i]);
   1.154 +                }
   1.155 +            }
   1.156 +        }
   1.157 +        return (T) newInstance0(this.getDeclaringClass(), "cons__" + sig, initargs);
   1.158      }
   1.159  
   1.160 +    @JavaScriptBody(args = { "self", "sig", "args" }, body =
   1.161 +          "\nvar c = self.cnstr;"
   1.162 +        + "\nvar inst = c();"
   1.163 +        + "\nc[sig].apply(inst, args);"
   1.164 +        + "\nreturn inst;"
   1.165 +    )
   1.166 +    private static native Object newInstance0(Class<?> self, String sig, Object[] args);
   1.167 +    
   1.168      /**
   1.169       * Returns {@code true} if this constructor was declared to take
   1.170       * a variable number of arguments; returns {@code false}
   1.171 @@ -481,22 +472,6 @@
   1.172          return Modifier.isSynthetic(getModifiers());
   1.173      }
   1.174  
   1.175 -    int getSlot() {
   1.176 -        return slot;
   1.177 -    }
   1.178 -
   1.179 -   String getSignature() {
   1.180 -            return signature;
   1.181 -   }
   1.182 -
   1.183 -    byte[] getRawAnnotations() {
   1.184 -        return annotations;
   1.185 -    }
   1.186 -
   1.187 -    byte[] getRawParameterAnnotations() {
   1.188 -        return parameterAnnotations;
   1.189 -    }
   1.190 -
   1.191      /**
   1.192       * @throws NullPointerException {@inheritDoc}
   1.193       * @since 1.5
   1.194 @@ -532,11 +507,11 @@
   1.195       * @since 1.5
   1.196       */
   1.197      public Annotation[][] getParameterAnnotations() {
   1.198 -        int numParameters = parameterTypes.length;
   1.199 -        if (parameterAnnotations == null)
   1.200 -            return new Annotation[numParameters][0];
   1.201 +//        int numParameters = parameterTypes.length;
   1.202 +//        if (parameterAnnotations == null)
   1.203 +//            return new Annotation[numParameters][0];
   1.204          
   1.205 -        return new Annotation[numParameters][0]; // XXX
   1.206 +        return new Annotation[0][0]; // XXX
   1.207  /*
   1.208          Annotation[][] result = AnnotationParser.parseParameterAnnotations(
   1.209              parameterAnnotations,