Skip to content
Permalink
Browse files

[FIXED JENKINS-34741] Allow new Class(foo: bar) to work.

Adds logic derived from Groovy's MetaClassImpl#invokeConstructor
method to return the no-parameters constructor when no matching
declared constructor is found, there's one argument, and that one
argument is a Map.
  • Loading branch information...
abayer committed Apr 10, 2017
1 parent a8160d6 commit 284340db4d1f6821126170f7b6fe20819c29fbf4
@@ -31,6 +31,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
@@ -163,6 +164,18 @@ private static boolean isInstancePrimitive(@Nonnull Class<?> type, @Nonnull Obje
return c;
}
}

// Only check for the magic Map constructor if we haven't already found a real constructor.
// Also note that this logic is derived from how Groovy itself decides to use the magic Map constructor, at
// MetaClassImpl#invokeConstructor(Class, Object[]).
if (args.length == 1 && args[0] instanceof Map) {
for (Constructor<?> c : receiver.getDeclaredConstructors()) {
if (c.getParameterTypes().length == 0 && !c.isVarArgs()) {
return c;
}
}
}

return null;
}

@@ -328,7 +328,6 @@ public String rawProp5() {
assertRejected(new AnnotatedWhitelist(), "staticMethod " + clazz + " explode", "C.m(); class C {static void m() {" + clazz + ".explode();}}");
}

@Ignore("TODO RejectedAccessException: unclassified new C java.util.LinkedHashMap")
@Issue("JENKINS-34741")
@Test public void structConstructor() throws Exception {
assertEvaluate(new StaticWhitelist(), "ok", "class C {String f}; new C(f: 'ok').f");

0 comments on commit 284340d

Please sign in to comment.
You can’t perform that action at this time.