< prev index next >

src/java.base/share/classes/java/lang/invoke/InfoFromMemberName.java

Print this page
*** 25,13 ***
  
  package java.lang.invoke;
  
  import java.security.*;
  import java.lang.reflect.*;
- import java.lang.invoke.MethodHandleNatives.Constants;
  import java.lang.invoke.MethodHandles.Lookup;
- import static java.lang.invoke.MethodHandleStatics.*;
  
  /*
   * Auxiliary to MethodHandleInfo, wants to nest in MethodHandleInfo but must be non-public.
   */
  /*non-public*/
--- 25,11 ---

*** 109,20 ***
  
      private Member reflectUnchecked() throws ReflectiveOperationException {
          byte refKind = (byte) getReferenceKind();
          Class<?> defc = getDeclaringClass();
          boolean isPublic = Modifier.isPublic(getModifiers());
!         if (MethodHandleNatives.refKindIsMethod(refKind)) {
              if (isPublic)
                  return defc.getMethod(getName(), getMethodType().parameterArray());
              else
                  return defc.getDeclaredMethod(getName(), getMethodType().parameterArray());
-         } else if (MethodHandleNatives.refKindIsConstructor(refKind)) {
-             if (isPublic)
-                 return defc.getConstructor(getMethodType().parameterArray());
-             else
-                 return defc.getDeclaredConstructor(getMethodType().parameterArray());
          } else if (MethodHandleNatives.refKindIsField(refKind)) {
              if (isPublic)
                  return defc.getField(getName());
              else
                  return defc.getDeclaredField(getName());
--- 107,30 ---
  
      private Member reflectUnchecked() throws ReflectiveOperationException {
          byte refKind = (byte) getReferenceKind();
          Class<?> defc = getDeclaringClass();
          boolean isPublic = Modifier.isPublic(getModifiers());
!         if (member.isObjectConstructorOrStaticInitMethod()) {
+             MethodType methodType = getMethodType();
+             if (MethodHandleNatives.refKindIsObjectConstructor(refKind) &&
+                 methodType.returnType() != void.class) {
+                 // object constructor
+                 throw new IllegalArgumentException("object constructor must be of void return type");
+             } else if (MethodHandleNatives.refKindIsMethod(refKind) &&
+                        methodType.returnType() != defc.asValueType()) {
+                 // TODO: allow to return Object or perhaps one of the supertypes of that class
+                 // static init factory
+                 throw new IllegalArgumentException("static constructor must be of " + defc.getName());
+             }
+ 
+             return isPublic ? defc.getConstructor(methodType.parameterArray())
+                             : defc.getDeclaredConstructor(methodType.parameterArray());
+         } else if (MethodHandleNatives.refKindIsMethod(refKind)) {
              if (isPublic)
                  return defc.getMethod(getName(), getMethodType().parameterArray());
              else
                  return defc.getDeclaredMethod(getName(), getMethodType().parameterArray());
          } else if (MethodHandleNatives.refKindIsField(refKind)) {
              if (isPublic)
                  return defc.getField(getName());
              else
                  return defc.getDeclaredField(getName());
< prev index next >