Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Feb 5, 2024
1 parent ce114e7 commit 02de577
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.codehaus.groovy.ast.Parameter;
import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.runtime.Adapters;
import org.eclipse.core.runtime.IAdaptable;

public class GroovyPropertyTester extends PropertyTester {

Expand All @@ -50,17 +49,15 @@ private static boolean oneStringArray(Parameter[] p) {

@Override
public boolean test(Object receiver, String property, Object[] arguments, Object expectedValue) {
if (receiver instanceof IAdaptable) {
ModuleNode node = Adapters.adapt(receiver, ModuleNode.class);
if (node != null && !Boolean.TRUE.equals(node.getNodeMetaData("ParseError"))) {
switch (property) {
case "hasMain":
return node.getClasses().stream().flatMap(cn -> cn.getDeclaredMethods("main").stream()).anyMatch(JAVA_MAIN);
case "isScript":
return !node.getStatementBlock().isEmpty() || (!node.getClasses().isEmpty() &&
node.getClasses().get(0).getNameEnd() < 1 /* un-named */ && getGroovyVersion().getMajor() >= 5 &&
node.getClasses().get(0).getDeclaredMethods("main").stream().anyMatch(JEP_445_MAIN.and(JAVA_MAIN.negate())));
}
ModuleNode node = Adapters.adapt(receiver, ModuleNode.class);
if (node != null && !node.encounteredUnrecoverableError()) {
switch (property) {
case "hasMain":
return node.getClasses().stream().flatMap(cn -> cn.getDeclaredMethods("main").stream()).anyMatch(JAVA_MAIN);
case "isScript":
return !node.getStatementBlock().isEmpty() || (!node.getClasses().isEmpty() &&
node.getClasses().get(0).getNameEnd() < 1 /* un-named */ && getGroovyVersion().getMajor() >= 5 &&
node.getClasses().get(0).getDeclaredMethods("main").stream().anyMatch(JEP_445_MAIN.and(JAVA_MAIN.negate())));
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.Variable;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.TernaryExpression;
Expand Down Expand Up @@ -427,7 +428,7 @@ public static Expression getTraitFieldExpression(MethodCallExpression call) {
} else if (objType.equals(ClassHelper.CLASS_Type) && asBoolean(objType.getGenericsTypes())) {
objType = objType.getGenericsTypes()[0].getType(); // look for $static$self.T__name$get()
}
if (Traits.isTrait(objType)) {
if (Traits.isTrait(objType) && call.getMethod() instanceof ConstantExpression) {
Matcher m = Pattern.compile(".+__(\\p{javaJavaIdentifierPart}+)\\$[gs]et").matcher(call.getMethodAsString());
if (m.matches()) {
String fieldName = m.group(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2071,14 +2071,14 @@ private boolean handleSimpleExpression(final Expression node) {
primaryType = null; // implicit-this calls are handled like free variables
isStatic = scope.isStatic();
} else {
isStatic = mce.getObjectExpression() instanceof ClassExpression || primaryType.equals(VariableScope.CLASS_CLASS_NODE);
isStatic = mce.getObjectExpression() instanceof ClassExpression || VariableScope.CLASS_CLASS_NODE.equals(primaryType);
}
} else if (enclosingNode instanceof PropertyExpression) {
PropertyExpression pe = (PropertyExpression) enclosingNode;
isStatic = pe.getObjectExpression() instanceof ClassExpression || primaryType.equals(VariableScope.CLASS_CLASS_NODE);
isStatic = pe.getObjectExpression() instanceof ClassExpression || VariableScope.CLASS_CLASS_NODE.equals(primaryType);
} else if (enclosingNode instanceof MethodPointerExpression) {
MethodPointerExpression mpe = (MethodPointerExpression) enclosingNode;
isStatic = mpe.getExpression() instanceof ClassExpression || primaryType.equals(VariableScope.CLASS_CLASS_NODE);
isStatic = mpe.getExpression() instanceof ClassExpression || VariableScope.CLASS_CLASS_NODE.equals(primaryType);
} else /*if (enclosingNode instanceof ImportNode)*/ {
isStatic = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import org.junit.Test

final class IsScriptTesterTests extends GroovyEclipseTestSuite {

private void doTest(String name = 'Main', String text, boolean expected) {
boolean isScript = new GroovyPropertyTester().test(addGroovySource(text, name), 'isScript', null, null)
private void doTest(String text, boolean expected) {
boolean isScript = new GroovyPropertyTester().test(addGroovySource(text, nextUnitName()), 'isScript', null, null)
assert (isScript == expected) : "Should have ${expected ? '' : '*not*'} found a script class in:\n$text"
}

Expand Down

0 comments on commit 02de577

Please sign in to comment.