Skip to content

Commit

Permalink
[FIXED JENKINS-34741] Allow new Class(foo: bar) to work.
Browse files Browse the repository at this point in the history
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 284340d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Expand Up @@ -328,7 +328,6 @@ public static final class Special {
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");
Expand Down

0 comments on commit 284340d

Please sign in to comment.