< prev index next >

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

Print this page

        

@@ -25,13 +25,11 @@
 
 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*/

@@ -109,20 +107,29 @@
 
     private Member reflectUnchecked() throws ReflectiveOperationException {
         byte refKind = (byte) getReferenceKind();
         Class<?> defc = getDeclaringClass();
         boolean isPublic = Modifier.isPublic(getModifiers());
-        if (MethodHandleNatives.refKindIsMethod(refKind)) {
+        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.asPrimaryType()) {
+                // static init factory
+                throw new IllegalArgumentException("static constructor must be of " + getDeclaringClass().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.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());
< prev index next >