Skip to content

Commit

Permalink
Fix for #1190: BaseScript: custom script method visitation
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 6, 2020
1 parent d874d9b commit 7612b3e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2009-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.jdt.core.groovy.tests.search;

import org.junit.Test;

public final class Groovy22InferencingTests extends InferencingTestSuite {

@Test
public void testBaseScript1() {
//@formatter:off
String contents =
"abstract class Foo extends Script {\n" +
" Number bar\n" +
" Number baz() {}\n" +
"}\n" +
"@groovy.transform.BaseScript Foo foo\n" +
"def x = bar\n" +
"def y = baz()\n";
//@formatter:on
assertType(contents, "x", "java.lang.Number");
assertType(contents, "y", "java.lang.Number");
}

@Test
public void testBaseScript2() {
//@formatter:off
String contents =
"@BaseScript(Foo)\n" +
"import groovy.transform.BaseScript\n" +
"abstract class Foo extends Script {\n" +
" Number bar\n" +
" Number baz() {}\n" +
"}\n" +
"def x = bar\n" +
"def y = baz()\n";
//@formatter:on
assertType(contents, "x", "java.lang.Number");
assertType(contents, "y", "java.lang.Number");
}

@Test
public void testBaseScript3() {
//@formatter:off
String contents =
"@BaseScript(Foo)\n" +
"import groovy.transform.BaseScript\n" +
"abstract class Foo extends Script {\n" +
" Number bar\n" +
" Number baz() {}\n" +
" abstract body()\n" +
" def run() {\n" +
" body()\n" +
" }\n" +
"}\n" +
"def x = bar\n" +
"def y = baz()\n";
//@formatter:on
assertType(contents, "x", "java.lang.Number");
assertType(contents, "y", "java.lang.Number");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public void visitJDT(final IType type, final ITypeRequestor requestor) {

// visit relocated @Memoized method bodies
for (MethodNode method : node.getMethods()) {
if (method.getName().startsWith("memoizedMethodPriv$")) {
if (method.getName().startsWith("memoizedMethodPriv$") || isCustomScriptBody(method)) {
scopes.add(new VariableScope(scopes.getLast(), method, method.isStatic()));
enclosingDeclarationNode = method;
try {
Expand Down Expand Up @@ -3100,6 +3100,11 @@ private static boolean isArithmeticOperationOnListOrNumberOrString(final String
}
}

private static boolean isCustomScriptBody(final MethodNode node) {
return node.getDeclaringClass().isScript() && !node.isScriptBody() &&
Boolean.TRUE.equals(node.getNodeMetaData(MethodNode.SCRIPT_BODY_METHOD_KEY));
}

private static boolean isEnumInit(final MethodCallExpression node) {
return (node.getType().isEnum() && node.getMethodAsString().equals("$INIT"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import org.junit.runners.Suite
org.eclipse.jdt.core.groovy.tests.search.GenericsMappingTests,
org.eclipse.jdt.core.groovy.tests.search.Groovy20InferencingTests,
org.eclipse.jdt.core.groovy.tests.search.Groovy21InferencingTests,
org.eclipse.jdt.core.groovy.tests.search.Groovy22InferencingTests,
org.eclipse.jdt.core.groovy.tests.search.Groovy25InferencingTests,
org.eclipse.jdt.core.groovy.tests.search.InferencingTests,
org.eclipse.jdt.core.groovy.tests.search.JDTPropertyNodeInferencingTests,
Expand Down

0 comments on commit 7612b3e

Please sign in to comment.