diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/AbstractInferencingTest.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/AbstractInferencingTest.java index 4ff4c51142..c5efe35ac3 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/AbstractInferencingTest.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/AbstractInferencingTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2016 the original author or authors. + * Copyright 2009-2017 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. @@ -15,11 +15,10 @@ */ package org.eclipse.jdt.core.groovy.tests.search; -import groovyjarjarasm.asm.Opcodes; - import java.util.ArrayList; import java.util.List; +import groovyjarjarasm.asm.Opcodes; import org.codehaus.groovy.ast.ASTNode; import org.codehaus.groovy.ast.AnnotatedNode; import org.codehaus.groovy.ast.ClassHelper; @@ -38,10 +37,6 @@ import org.eclipse.jdt.groovy.search.TypeLookupResult.TypeConfidence; import org.eclipse.jdt.groovy.search.VariableScope; -/** - * @author Andrew Eisenberg - * @created Nov 13, 2009 - */ public abstract class AbstractInferencingTest extends AbstractGroovySearchTest { public AbstractInferencingTest(String name) { @@ -52,13 +47,11 @@ protected void assertType(String contents, String expectedType) { assertType(contents, 0, contents.length(), expectedType, false); } - protected void assertType(String contents, int exprStart, int exprEnd, - String expectedType) { + protected void assertType(String contents, int exprStart, int exprEnd, String expectedType) { assertType(contents, exprStart, exprEnd, expectedType, false); } - protected void assertTypeOneOf(String contents, int start, int end, - String... expectedTypes) throws Throwable { + protected void assertTypeOneOf(String contents, int start, int end, String... expectedTypes) throws Throwable { boolean ok = false; Throwable error = null; for (int i = 0; !ok && i < expectedTypes.length; i++) { @@ -89,14 +82,12 @@ protected void assertType(String contents, String expectedType, boolean forceWor protected void assertType(String contents, int exprStart, int exprEnd, String expectedType, boolean forceWorkingCopy) { assertType(contents, exprStart, exprEnd, expectedType, null, forceWorkingCopy); } - public static void assertType(GroovyCompilationUnit contents, int exprStart, - int exprEnd, String expectedType, boolean forceWorkingCopy) { + + public static void assertType(GroovyCompilationUnit contents, int exprStart, int exprEnd, String expectedType, boolean forceWorkingCopy) { assertType(contents, exprStart, exprEnd, expectedType, null, forceWorkingCopy); } - public static void assertType(GroovyCompilationUnit unit, - int exprStart, int exprEnd, String expectedType, String extraDocSnippet, - boolean forceWorkingCopy) { + public static void assertType(GroovyCompilationUnit unit, int exprStart, int exprEnd, String expectedType, String extraDocSnippet, boolean forceWorkingCopy) { SearchRequestor requestor = doVisit(exprStart, exprEnd, unit, forceWorkingCopy); assertNotNull("Did not find expected ASTNode", requestor.node); @@ -131,8 +122,7 @@ public static void assertType(GroovyCompilationUnit unit, * @param assumeNoUnknowns * @return null if all is OK, or else returns an error message specifying the problem */ - public static String checkType(GroovyCompilationUnit unit, int exprStart, - int exprEnd, String expectedType, String expectedDeclaringType, boolean assumeNoUnknowns, boolean forceWorkingCopy) { + public static String checkType(GroovyCompilationUnit unit, int exprStart, int exprEnd, String expectedType, String expectedDeclaringType, boolean assumeNoUnknowns, boolean forceWorkingCopy) { SearchRequestor requestor = doVisit(exprStart, exprEnd, unit, forceWorkingCopy); if (requestor.node == null) { return "Did not find expected ASTNode. (Start:" + exprStart + ", End:" + exprEnd + ")\n" + @@ -178,8 +168,7 @@ public static String checkType(GroovyCompilationUnit unit, int exprStart, return null; } - protected void assertType(String contents, int exprStart, int exprEnd, - String expectedType, String extraDocSnippet, boolean forceWorkingCopy) { + protected void assertType(String contents, int exprStart, int exprEnd, String expectedType, String extraDocSnippet, boolean forceWorkingCopy) { GroovyCompilationUnit unit = createUnit("Search", contents); assertType(unit, exprStart, exprEnd, expectedType, extraDocSnippet, forceWorkingCopy); } @@ -236,50 +225,41 @@ public static SearchRequestor doVisit(int exprStart, int exprEnd, GroovyCompilat } } - protected void assertDeclaringType(String contents, int exprStart, int exprEnd, - String expectedDeclaringType) { + protected void assertDeclaringType(String contents, int exprStart, int exprEnd, String expectedDeclaringType) { assertDeclaringType(contents, exprStart, exprEnd, expectedDeclaringType, false); } protected enum DeclarationKind { FIELD, METHOD, PROPERTY, CLASS } - protected void assertDeclaration(String contents, int exprStart, int exprEnd, - String expectedDeclaringType, String declarationName, DeclarationKind kind) { + protected void assertDeclaration(String contents, int exprStart, int exprEnd, String expectedDeclaringType, String declarationName, DeclarationKind kind) { assertDeclaringType(contents, exprStart, exprEnd, expectedDeclaringType, false, false); GroovyCompilationUnit unit = createUnit("Search", contents); SearchRequestor requestor = doVisit(exprStart, exprEnd, unit, false); switch (kind) { case FIELD: - assertTrue("Expecting field, but was " + requestor.result.declaration, - requestor.result.declaration instanceof FieldNode); + assertTrue("Expecting field, but was " + requestor.result.declaration, requestor.result.declaration instanceof FieldNode); assertEquals("Wrong field name", declarationName, ((FieldNode) requestor.result.declaration).getName()); break; case METHOD: - assertTrue("Expecting method, but was " + requestor.result.declaration, - requestor.result.declaration instanceof MethodNode); + assertTrue("Expecting method, but was " + requestor.result.declaration, requestor.result.declaration instanceof MethodNode); assertEquals("Wrong method name", declarationName, ((MethodNode) requestor.result.declaration).getName()); break; case PROPERTY: - assertTrue("Expecting property, but was " + requestor.result.declaration, - requestor.result.declaration instanceof PropertyNode); + assertTrue("Expecting property, but was " + requestor.result.declaration, requestor.result.declaration instanceof PropertyNode); assertEquals("Wrong property name", declarationName, ((PropertyNode) requestor.result.declaration).getName()); break; case CLASS: - assertTrue("Expecting class, but was " + requestor.result.declaration, - requestor.result.declaration instanceof ClassNode); + assertTrue("Expecting class, but was " + requestor.result.declaration, requestor.result.declaration instanceof ClassNode); assertEquals("Wrong class name", declarationName, ((ClassNode) requestor.result.declaration).getName()); } } - protected void assertDeclaringType(String contents, int exprStart, int exprEnd, - String expectedDeclaringType, boolean forceWorkingCopy) { - + protected void assertDeclaringType(String contents, int exprStart, int exprEnd, String expectedDeclaringType, boolean forceWorkingCopy) { assertDeclaringType(contents, exprStart, exprEnd, expectedDeclaringType, forceWorkingCopy, false); } - protected void assertDeclaringType(String contents, int exprStart, int exprEnd, - String expectedDeclaringType, boolean forceWorkingCopy, boolean expectingUnknown) { + protected void assertDeclaringType(String contents, int exprStart, int exprEnd, String expectedDeclaringType, boolean forceWorkingCopy, boolean expectingUnknown) { GroovyCompilationUnit unit = createUnit("Search", contents); SearchRequestor requestor = doVisit(exprStart, exprEnd, unit, forceWorkingCopy); @@ -316,8 +296,7 @@ protected void assertDeclaringType(String contents, int exprStart, int exprEnd, } } - protected void assertUnknownConfidence(String contents, int exprStart, int exprEnd, - String expectedDeclaringType, boolean forceWorkingCopy) { + protected void assertUnknownConfidence(String contents, int exprStart, int exprEnd, String expectedDeclaringType, boolean forceWorkingCopy) { GroovyCompilationUnit unit = createUnit("Search", contents); SearchRequestor requestor = doVisit(exprStart, exprEnd, unit, forceWorkingCopy); @@ -363,7 +342,7 @@ public static String printGenerics(ClassNode type) { return sb.toString(); } - public class UnknownTypeRequestor implements ITypeRequestor { + public class UnknownTypeRequestor implements ITypeRequestor { private List unknownNodes = new ArrayList(); public List getUnknownNodes() { @@ -390,7 +369,6 @@ public static class SearchRequestor implements ITypeRequestor { public final List unknowns = new ArrayList(); public SearchRequestor(int start, int end) { - super(); this.start = start; this.end = end; } diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java index 5d7dca675b..c622fc8d9c 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/InferencingTests.java @@ -48,167 +48,167 @@ private static boolean isAccessorPreferredForSTCProperty() { return (version.compareTo(new Version(2, 4, 6)) >= 0); } - public void testLocalVar1() throws Exception { + public void testLocalVar1() { String contents ="def x\nthis.x"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertUnknownConfidence(contents, start, end, "Search", false); } - public void testLocalVar2() throws Exception { + public void testLocalVar2() { String contents ="def x\ndef y = { this.x }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertUnknownConfidence(contents, start, end, "Search", false); } - public void testLocalVar2a() throws Exception { + public void testLocalVar2a() { String contents ="def x\ndef y = { this.x() }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertUnknownConfidence(contents, start, end, "Search", false); } - public void testLocalVar3() throws Exception { + public void testLocalVar3() { String contents ="int x\ndef y = { x }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testLocalVar4() throws Exception { + public void testLocalVar4() { String contents ="int x\ndef y = { x() }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testLocalMethod1() throws Exception { + public void testLocalMethod1() { String contents ="int x() { }\ndef y = { x() }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testLocalMethod2() throws Exception { + public void testLocalMethod2() { String contents ="int x() { }\ndef y = { x }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testLocalMethod3() throws Exception { + public void testLocalMethod3() { String contents ="int x() { }\ndef y = { def z = { x } }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testLocalMethod4() throws Exception { + public void testLocalMethod4() { String contents ="int x() { }\ndef y = { def z = { x() } }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testLocalMethod5() throws Exception { + public void testLocalMethod5() { String contents ="int x() { }\ndef y = { def z = { this.x() } }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testLocalMethod6() throws Exception { + public void testLocalMethod6() { String contents ="def x\ndef y = { delegate.x() }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertUnknownConfidence(contents, start, end, "Search", false); } - public void testLocalMethod7() throws Exception { + public void testLocalMethod7() { String contents ="def x\ndef y = { delegate.x }"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertUnknownConfidence(contents, start, end, "Search", false); } - public void testNumber1() throws Exception { + public void testNumber1() { assertType("10", "java.lang.Integer"); } - public void testNumber1a() throws Exception { + public void testNumber1a() { // same as above, but test that whitespace is not included assertType("10 ", 0, 2, "java.lang.Integer"); } - public void testNumber2() throws Exception { + public void testNumber2() { String contents ="def x = 1+2\nx"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testNumber3() throws Exception { + public void testNumber3() { assertType("10L", "java.lang.Long"); } - public void testNumber4() throws Exception { + public void testNumber4() { assertType("10++", "java.lang.Integer"); } - public void testNumber5() throws Exception { + public void testNumber5() { assertType("++10", "java.lang.Integer"); } - public void testNumber6() throws Exception { + public void testNumber6() { String contents = "(x <=> y).intValue()"; int start = contents.indexOf("intValue"); int end = start + "intValue".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testString1() throws Exception { + public void testString1() { assertType("\"10\"", "java.lang.String"); } - public void testString2() throws Exception { + public void testString2() { assertType("'10'", "java.lang.String"); } - public void testString3() throws Exception { + public void testString3() { String contents = "def x = '10'"; assertType(contents, contents.indexOf('\''), contents.lastIndexOf('\'')+1, "java.lang.String"); } - public void testString4() throws Exception { + public void testString4() { String contents = "def x = false ? '' : ''\nx"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.String"); } - public void testMatcher1() throws Exception { + public void testMatcher1() { String contents = "def x = \"\" =~ /pattern/\nx"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.util.regex.Matcher"); } - public void testMatcher2() throws Exception { + public void testMatcher2() { String contents = "(\"\" =~ /pattern/).hasGroup()"; int start = contents.indexOf("hasGroup"); int end = start + "hasGroup".length(); assertType(contents, start, end, "java.lang.Boolean"); } - public void testPattern() throws Exception { + public void testPattern() { String contents ="def x = \"\" ==~ /pattern/\nx"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Boolean"); } - public void testList1() throws Exception { + public void testList1() { String contents ="def x = []\nx"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); @@ -223,32 +223,32 @@ public void testList2() throws Throwable { "java.util.Collection", "java.util.List"); } - public void testClosure1() throws Exception { + public void testClosure1() { String contents = "def fn = { a, b -> a + b }"; assertType(contents, 4, 6, "groovy.lang.Closure"); } - public void testClosure2() throws Exception { + public void testClosure2() { String contents = "def fn = { int a, int b -> a + b }"; assertType(contents, 4, 6, "groovy.lang.Closure"); // } - public void _testClosure3() throws Exception { + public void _testClosure3() { String contents = "def fn = x.&y"; assertType(contents, 4, 6, "groovy.lang.Closure"); } - public void _testClosure4() throws Exception { + public void _testClosure4() { String contents = "def fn = 'abc'.&length"; assertType(contents, 4, 6, "groovy.lang.Closure"); // } - public void _testClosure5() throws Exception { + public void _testClosure5() { String contents = "def fn = Collections.&emptyList"; assertType(contents, 4, 6, "groovy.lang.Closure"); // } - public void testClosure6() throws Exception { + public void testClosure6() { String contents = "class Baz {\n" + " URL other\n" + @@ -261,7 +261,7 @@ public void testClosure6() throws Exception { assertType(contents, start, end, "java.net.URL"); } - public void testClosure7() throws Exception { + public void testClosure7() { String contents = "class Baz {\n" + " URL other\n" + @@ -288,7 +288,7 @@ public void testClosure7() throws Exception { assertType(contents, start, end, "Baz"); } - public void testClosure8() throws Exception { + public void testClosure8() { String contents = "def x = {\n" + "maximumNumberOfParameters\n" + @@ -310,7 +310,7 @@ public void testClosure8() throws Exception { assertType(contents, start, end, "java.lang.Object"); } - public void testClosure9() throws Exception { + public void testClosure9() { String contents = "def x = { def y = {\n" + "maximumNumberOfParameters\n" + @@ -332,81 +332,81 @@ public void testClosure9() throws Exception { assertType(contents, start, end, "java.lang.Object"); } - public void testSpread1() throws Exception { + public void testSpread1() { String contents = "def z = [1,2]*.value"; int start = contents.lastIndexOf("value"); assertType(contents, start, start + "value".length(), "java.lang.Integer"); } - public void testSpread2() throws Exception { + public void testSpread2() { String contents = "[1,2,3]*.intValue()"; int start = contents.lastIndexOf("intValue"); assertType(contents, start, start + "intValue".length(), "java.lang.Integer"); } - public void testSpread3() throws Exception { + public void testSpread3() { String contents = "[1,2,3]*.intValue()[0].value"; int start = contents.lastIndexOf("value"); assertType(contents, start, start + "value".length(), "java.lang.Integer"); } - public void testSpread4() throws Exception { + public void testSpread4() { String contents = "[x:1,y:2,z:3]*.getKey()"; int start = contents.lastIndexOf("getKey"); assertType(contents, start, start + "getKey".length(), "java.lang.String"); } - public void testSpread5() throws Exception { + public void testSpread5() { String contents = "[x:1,y:2,z:3]*.getValue()"; int start = contents.lastIndexOf("getValue"); assertType(contents, start, start + "getValue".length(), "java.lang.Integer"); } - public void testSpread6() throws Exception { + public void testSpread6() { String contents = "[x:1,y:2,z:3]*.key"; int start = contents.lastIndexOf("key"); assertType(contents, start, start + "key".length(), "java.lang.String"); } - public void testSpread7() throws Exception { + public void testSpread7() { String contents = "[x:1,y:2,z:3]*.value"; int start = contents.lastIndexOf("value"); assertType(contents, start, start + "value".length(), "java.lang.Integer"); } - public void testSpread8() throws Exception { + public void testSpread8() { String contents = "[x:1,y:2,z:3]*.key[0].toLowerCase()"; int start = contents.lastIndexOf("toLowerCase"); assertType(contents, start, start + "toLowerCase".length(), "java.lang.String"); } - public void testSpread9() throws Exception { + public void testSpread9() { String contents = "[x:1,y:2,z:3]*.value[0].intValue()"; int start = contents.lastIndexOf("intValue"); assertType(contents, start, start + "intValue".length(), "java.lang.Integer"); } - public void testSpread10() throws Exception { + public void testSpread10() { String contents = "[1,2,3]*.value[0].value"; int start = contents.lastIndexOf("value"); assertType(contents, start, start + "value".length(), "java.lang.Integer"); } - public void testSpread11() throws Exception { + public void testSpread11() { String contents = "Set strings = ['1','2','3'] as Set\n" + "strings*.bytes"; int start = contents.lastIndexOf("bytes"); assertType(contents, start, start + "bytes".length(), "byte[]"); } - public void testSpread12() throws Exception { + public void testSpread12() { String contents = "Set strings = ['1','2','3'] as Set\n" + "strings*.length()"; int start = contents.lastIndexOf("length"); assertType(contents, start, start + "length".length(), "java.lang.Integer"); } - public void testSpread13() throws Exception { + public void testSpread13() { String contents = "@groovy.transform.TypeChecked class Foo {\n" + " static def meth() {\n" + " Set beans = []\n" + @@ -419,57 +419,57 @@ public void testSpread13() throws Exception { assertType(contents, start, start + "additionalBeanInfo".length(), "java.beans.BeanInfo[]"); } - public void testMap1() throws Exception { + public void testMap1() { assertType("[:]", "java.util.Map"); } - public void testBoolean1() throws Exception { + public void testBoolean1() { assertType("!x", "java.lang.Boolean"); } - public void testBoolean2() throws Exception { + public void testBoolean2() { String contents = "(x < y).booleanValue()"; int start = contents.indexOf("booleanValue"); int end = start + "booleanValue".length(); assertType(contents, start, end, "java.lang.Boolean"); } - public void testBoolean3() throws Exception { + public void testBoolean3() { String contents = "(x <= y).booleanValue()"; int start = contents.indexOf("booleanValue"); int end = start + "booleanValue".length(); assertType(contents, start, end, "java.lang.Boolean"); } - public void testBoolean4() throws Exception { + public void testBoolean4() { String contents = "(x >= y).booleanValue()"; int start = contents.indexOf("booleanValue"); int end = start + "booleanValue".length(); assertType(contents, start, end, "java.lang.Boolean"); } - public void testBoolean5() throws Exception { + public void testBoolean5() { String contents = "(x != y).booleanValue()"; int start = contents.indexOf("booleanValue"); int end = start + "booleanValue".length(); assertType(contents, start, end, "java.lang.Boolean"); } - public void testBoolean6() throws Exception { + public void testBoolean6() { String contents = "(x == y).booleanValue()"; int start = contents.indexOf("booleanValue"); int end = start + "booleanValue".length(); assertType(contents, start, end, "java.lang.Boolean"); } - public void testBoolean7() throws Exception { + public void testBoolean7() { String contents = "(x in y).booleanValue()"; int start = contents.indexOf("booleanValue"); int end = start + "booleanValue".length(); assertType(contents, start, end, "java.lang.Boolean"); } - public void testConstructor1() throws Exception { + public void testConstructor1() { // GRECLIPSE-1229 constructors with map parameters String contents = "class O {\n" + @@ -489,7 +489,7 @@ public void testConstructor1() throws Exception { assertDeclaration(contents, start, end, "O", "bb", DeclarationKind.PROPERTY); } - public void testConstructor2() throws Exception { + public void testConstructor2() { String contents = "class O {\n" + " boolean aa\n" + @@ -508,7 +508,7 @@ public void testConstructor2() throws Exception { assertDeclaration(contents, start, end, "O", "bb", DeclarationKind.PROPERTY); } - public void testConstructor3() throws Exception { + public void testConstructor3() { String contents = "class O {\n" + " boolean aa\n" + @@ -522,7 +522,7 @@ public void testConstructor3() throws Exception { assertDeclaration(contents, start, end, "O", "bb", DeclarationKind.PROPERTY); } - public void testConstructor4() throws Exception { + public void testConstructor4() { String contents = "class O {\n" + " boolean aa\n" + @@ -541,7 +541,7 @@ public void testConstructor4() throws Exception { assertDeclaringType(contents, start, end, "null"); } - public void testConstructor5() throws Exception { + public void testConstructor5() { String contents = "class O {\n" + " boolean aa\n" + @@ -560,7 +560,7 @@ public void testConstructor5() throws Exception { assertDeclaringType(contents, start, end, "null"); } - public void testConstructor6() throws Exception { + public void testConstructor6() { String contents = "class O {\n" + " boolean aa\n" + @@ -579,32 +579,60 @@ public void testConstructor6() throws Exception { assertDeclaringType(contents, start, end, "null"); } - public void testStaticMethodCall() throws Exception { + public void testSpecialConstructor1() { + String contents = + "class C {\n" + + " C() {\n" + + " this()\n" + + " }\n" + + "}"; + + int start = contents.indexOf("this()"); + int end = start + "this(".length(); + assertType(contents, start, end, "java.lang.Object"); + assertDeclaringType(contents, start, end, "C"); + } + + public void testSpecialConstructor2() { + String contents = + "class C extends HashMap {\n" + + " C() {\n" + + " super()\n" + + " }\n" + + "}"; + + int start = contents.indexOf("super()"); + int end = start + "super(".length(); + assertType(contents, start, end, "java.lang.Object"); + assertDeclaringType(contents, start, end, "java.util.HashMap"); + } + + public void testStaticMethodCall() { String contents = "Two.x()\n class Two {\n static String x() {\n \"\" } } "; String expr = "x"; assertType(contents, contents.indexOf(expr), contents.indexOf(expr)+expr.length(), "java.lang.String"); } - public void testStaticMethodCall2() throws Exception { + public void testStaticMethodCall2() { String contents = "Two.x\n class Two {\n static String x() {\n \"\" } } "; String expr = "x"; assertType(contents, contents.indexOf(expr), contents.indexOf(expr)+expr.length(), "java.lang.String"); } - public void testStaticMethodCall3() throws Exception { + public void testStaticMethodCall3() { String contents = "class Two {\n def other() { \n x() } \n static String x() {\n \"\" } } "; String expr = "x() "; // extra space b/c static method call expression end offset is wrong assertType(contents, contents.indexOf(expr), contents.indexOf(expr)+expr.length(), "java.lang.String"); } - public void testStaticMethodCall4() throws Exception { + public void testStaticMethodCall4() { String contents = "class Two {\n def other() { \n x } \n static String x() {\n \"\" } } "; String expr = "x"; assertType(contents, contents.indexOf(expr), contents.indexOf(expr)+expr.length(), "java.lang.String"); } // GRECLISPE-1244 - public void testStaticMethodCall5() throws Exception { + public void testStaticMethodCall5() { String contents = "class Parent {\n" + " static p() {}\n" + @@ -619,7 +647,7 @@ public void testStaticMethodCall5() throws Exception { } // GRECLISPE-1244 - public void testStaticMethodCall6() throws Exception { + public void testStaticMethodCall6() { createUnit("Parent", "class Parent {\n" + " static p() {}\n" + @@ -634,20 +662,20 @@ public void testStaticMethodCall6() throws Exception { assertDeclaringType(contents, contents.lastIndexOf(expr), contents.lastIndexOf(expr)+expr.length(), "Parent"); } - public void testSuperFieldReference() throws Exception { + public void testSuperFieldReference() { String contents = "class B extends A {\n def other() { \n myOther } } \n class A { String myOther } "; String expr = "myOther"; assertType(contents, contents.indexOf(expr), contents.indexOf(expr)+expr.length(), "java.lang.String"); } - public void testFieldWithInitializer1() throws Exception { + public void testFieldWithInitializer1() { String contents = "class A {\ndef x = 9\n}\n new A().x"; int start = contents.lastIndexOf('x'); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testFieldWithInitializer2() throws Exception { + public void testFieldWithInitializer2() { createUnit("A", "class A {\ndef x = 9\n} "); String contents = "new A().x"; int start = contents.lastIndexOf('x'); @@ -655,105 +683,105 @@ public void testFieldWithInitializer2() throws Exception { assertType(contents, start, end, "java.lang.Integer"); } - public void testTernaryExpression() throws Exception { + public void testTernaryExpression() { String contents = "def x = true ? 2 : 1\nx"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testElvisOperator() throws Exception { + public void testElvisOperator() { String contents = "def x = 2 ?: 1\nx"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testRangeExpression1() throws Exception { + public void testRangeExpression1() { String contents = "def x = 0 .. 5\nx"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "groovy.lang.Range"); } - public void testRangeExpression2() throws Exception { + public void testRangeExpression2() { String contents = "def x = 0 ..< 5\nx"; int start = contents.lastIndexOf("x"); int end = start + "x".length(); assertType(contents, start, end, "groovy.lang.Range"); } - public void testRangeExpression3() throws Exception { + public void testRangeExpression3() { String contents = "(1..10).getFrom()"; int start = contents.lastIndexOf("getFrom"); int end = start + "getFrom".length(); assertType(contents, start, end, "java.lang.Comparable"); } - public void testRangeExpression4() throws Exception { + public void testRangeExpression4() { String contents = "(1..10).getTo()"; int start = contents.lastIndexOf("getTo"); int end = start + "getTo".length(); assertType(contents, start, end, "java.lang.Comparable"); } - public void testRangeExpression5() throws Exception { + public void testRangeExpression5() { String contents = "(1..10).step(0)"; int start = contents.lastIndexOf("step"); int end = start + "step".length(); assertType(contents, start, end, "java.util.List"); } - public void testRangeExpression6() throws Exception { + public void testRangeExpression6() { String contents = "(1..10).step(0, { })"; int start = contents.lastIndexOf("step"); int end = start + "step".length(); assertType(contents, start, end, "java.lang.Void"); } - public void testInnerClass1() throws Exception { + public void testInnerClass1() { String contents = "class Outer { class Inner { } \nInner x }\nnew Outer().x "; int start = contents.lastIndexOf("x"); int end = start + 1; assertType(contents, start, end, "Outer$Inner"); } - public void testInnerClass2() throws Exception { + public void testInnerClass2() { String contents = "class Outer { class Inner { class InnerInner{ } }\n Outer.Inner.InnerInner x }\nnew Outer().x "; int start = contents.lastIndexOf("x"); int end = start + 1; assertType(contents, start, end, "Outer$Inner$InnerInner"); } - public void testInnerClass3() throws Exception { + public void testInnerClass3() { String contents = "class Outer { class Inner { def z() { \nnew Outer().x \n } } \nInner x }"; int start = contents.indexOf("x"); int end = start + 1; assertType(contents, start, end, "Outer$Inner"); } - public void testInnerClass4() throws Exception { + public void testInnerClass4() { String contents = "class Outer { class Inner { class InnerInner { def z() { \nnew Outer().x \n } } } \nInner x }"; int start = contents.indexOf("x"); int end = start + 1; assertType(contents, start, end, "Outer$Inner"); } - public void testInnerClass5() throws Exception { + public void testInnerClass5() { String contents = "class Outer { class Inner extends Outer { } }"; int start = contents.lastIndexOf("Outer"); int end = start + "Outer".length(); assertType(contents, start, end, "Outer"); } - public void testInnerClass6() throws Exception { + public void testInnerClass6() { String contents = "class Outer extends RuntimeException { class Inner { def foo() throws Outer { } } }"; int start = contents.lastIndexOf("Outer"); int end = start + "Outer".length(); assertType(contents, start, end, "Outer"); } - public void testConstantFromSuper() throws Exception { + public void testConstantFromSuper() { String contents = "public interface Constants {\n" + "int FIRST = 9;\n" + "}\n" + @@ -767,14 +795,14 @@ public void testConstantFromSuper() throws Exception { assertType(contents, start, end, "java.lang.Integer"); } - public void testAssignementInInnerBlock() throws Exception { + public void testAssignementInInnerBlock() { String contents = "def xxx\n if (true) { xxx = \"\" \n xxx} "; int start = contents.lastIndexOf("xxx"); int end = start + "xxx".length(); assertType(contents, start, end, "java.lang.String"); } - public void testAssignementInInnerBlock2() throws Exception { + public void testAssignementInInnerBlock2() { String contents = "def xxx\n if (true) { xxx = \"\" \n }\n xxx"; int start = contents.lastIndexOf("xxx"); int end = start + "xxx".length(); @@ -782,7 +810,7 @@ public void testAssignementInInnerBlock2() throws Exception { } // GRECLIPSE-743 - public void testOverrideCategory1() throws Exception { + public void testOverrideCategory1() { String contents = "class A { }\n new A().getAt() "; int start = contents.lastIndexOf("getAt"); int end = start + "getAt".length(); @@ -790,7 +818,7 @@ public void testOverrideCategory1() throws Exception { assertDeclaringType(contents, start, end, "org.codehaus.groovy.runtime.DefaultGroovyMethods"); } - public void testOverrideCategory2() throws Exception { + public void testOverrideCategory2() { String contents = "class A {\n A getAt(prop) { \n new A() \n } }\n new A().getAt()"; int start = contents.lastIndexOf("getAt"); int end = start + "getAt".length(); @@ -798,7 +826,7 @@ public void testOverrideCategory2() throws Exception { assertDeclaringType(contents, start, end, "A"); } - public void testOverrideCategory3() throws Exception { + public void testOverrideCategory3() { String contents = "class A {\n A getAt(prop) { \n new A() \n } }\n class B extends A { }\n new B().getAt()"; int start = contents.lastIndexOf("getAt"); int end = start + "getAt".length(); @@ -806,35 +834,35 @@ public void testOverrideCategory3() throws Exception { assertDeclaringType(contents, start, end, "A"); } - public void testGRECLIPSE731a() throws Exception { + public void testGRECLIPSE731a() { String contents = "def foo() { } \nString xxx = foo()\nxxx"; int start = contents.lastIndexOf("xxx"); int end = start + "xxx".length(); assertType(contents, start, end, "java.lang.String"); } - public void testGRECLIPSE731b() throws Exception { + public void testGRECLIPSE731b() { String contents = "def foo() { } \ndef xxx = foo()\nxxx"; int start = contents.lastIndexOf("xxx"); int end = start + "xxx".length(); assertType(contents, start, end, "java.lang.Object"); } - public void testGRECLIPSE731c() throws Exception { + public void testGRECLIPSE731c() { String contents = "String foo() { } \ndef xxx = foo()\nxxx"; int start = contents.lastIndexOf("xxx"); int end = start + "xxx".length(); assertType(contents, start, end, "java.lang.String"); } - public void testGRECLIPSE731d() throws Exception { + public void testGRECLIPSE731d() { String contents = "int foo() { } \ndef xxx = foo()\nxxx"; int start = contents.lastIndexOf("xxx"); int end = start + "xxx".length(); assertType(contents, start, end, "java.lang.Integer"); } - public void testGRECLIPSE731e() throws Exception { + public void testGRECLIPSE731e() { // ignore assignments to object expressions String contents = "def foo() { } \nString xxx\nxxx = foo()\nxxx"; int start = contents.lastIndexOf("xxx"); @@ -842,7 +870,7 @@ public void testGRECLIPSE731e() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testGRECLIPSE731f() throws Exception { + public void testGRECLIPSE731f() { // ignore assignments to object expressions String contents = "class X { String xxx\ndef foo() { }\ndef meth() { xxx = foo()\nxxx } }"; int start = contents.lastIndexOf("xxx"); @@ -850,7 +878,7 @@ public void testGRECLIPSE731f() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testGRECLIPSE1720() throws Exception { + public void testGRECLIPSE1720() { String contents = "import groovy.transform.CompileStatic\n" + "@CompileStatic\n" + "public class Bug {\n" + @@ -865,77 +893,77 @@ public void testGRECLIPSE1720() throws Exception { assertType(contents, start, end, "java.lang.Boolean"); } - public void testCatchBlock1() throws Exception { + public void testCatchBlock1() { String catchString = "try { } catch (NullPointerException e) { e }"; int start = catchString.lastIndexOf("NullPointerException"); int end = start + "NullPointerException".length(); assertType(catchString, start, end, "java.lang.NullPointerException"); } - public void testCatchBlock2() throws Exception { + public void testCatchBlock2() { String catchString = "try { } catch (NullPointerException e) { e }"; int start = catchString.lastIndexOf("e"); int end = start + 1; assertType(catchString, start, end, "java.lang.NullPointerException"); } - public void testCatchBlock3() throws Exception { + public void testCatchBlock3() { String catchString = "try { } catch (NullPointerException e) { e }"; int start = catchString.indexOf("NullPointerException e"); int end = start + ("NullPointerException e").length(); assertType(catchString, start, end, "java.lang.NullPointerException"); } - public void testCatchBlock4() throws Exception { + public void testCatchBlock4() { String catchString2 = "try { } catch (e) { e }"; int start = catchString2.indexOf("e"); int end = start + 1; assertType(catchString2, start, end, "java.lang.Exception"); } - public void testCatchBlock5() throws Exception { + public void testCatchBlock5() { String catchString2 = "try { } catch (e) { e }"; int start = catchString2.lastIndexOf("e"); int end = start + 1; assertType(catchString2, start, end, "java.lang.Exception"); } - public void testAssignment1() throws Exception { + public void testAssignment1() { String contents = "String x = 7\nx"; int start = contents.lastIndexOf("x"); int end = start + 1; assertType(contents, start, end, "java.lang.String"); } - public void testAssignment2() throws Exception { + public void testAssignment2() { String contents = "String x\nx"; int start = contents.lastIndexOf("x"); int end = start + 1; assertType(contents, start, end, "java.lang.String"); } - public void testAssignment3() throws Exception { + public void testAssignment3() { String contents = "String x\nx = 7\nx"; // will be a GroovyCastException at runtime int start = contents.lastIndexOf("x"); int end = start + 1; assertType(contents, start, end, "java.lang.String"); } - public void testAssignment4() throws Exception { + public void testAssignment4() { String contents = "String x() { \ndef x = 9\n x}"; int start = contents.lastIndexOf("x"); int end = start + 1; assertType(contents, start, end, "java.lang.Integer"); } - public void testAssignment5() throws Exception { + public void testAssignment5() { String contents = "String x() { \ndef x\nx = 9\n x}"; int start = contents.lastIndexOf("x"); int end = start + 1; assertType(contents, start, end, "java.lang.Integer"); } - public void testInClosure1() throws Exception { + public void testInClosure1() { String contents = "class Bar {\n" + " def meth() { }\n" + @@ -946,7 +974,7 @@ public void testInClosure1() throws Exception { assertType(contents, start, end, "Bar"); } - public void testInClosure2() throws Exception { + public void testInClosure2() { String contents = "class Bar {\n" + " def meth(x) { }\n" + @@ -957,7 +985,7 @@ public void testInClosure2() throws Exception { assertType(contents, start, end, "Search"); } - public void testInClosure2a() throws Exception { + public void testInClosure2a() { String contents = "class Bar {\n" + " def meth(x) { }\n" + @@ -968,7 +996,7 @@ public void testInClosure2a() throws Exception { assertType(contents, start, end, "Bar"); } - public void testInClosure2b() throws Exception { + public void testInClosure2b() { String contents = "class Bar {\n" + " def meth(x) { }\n" + @@ -979,7 +1007,7 @@ public void testInClosure2b() throws Exception { assertType(contents, start, end, "Search"); } - public void testInClosure2c() throws Exception { + public void testInClosure2c() { // closure in a closure and owner is outer closure String contents = "first {\n second {\n owner } }"; int start = contents.lastIndexOf("owner"); @@ -991,7 +1019,7 @@ public void testInClosure2c() throws Exception { } } - public void testInClosure3() throws Exception { + public void testInClosure3() { String contents = "class Baz { }\n" + "class Bar {\n" + " def meth(x) { }\n" + @@ -1004,7 +1032,7 @@ public void testInClosure3() throws Exception { assertType(contents, start, end, "Baz"); } - public void testInClosure4() throws Exception { + public void testInClosure4() { String contents = "''.foo {\n" + " substring" + @@ -1014,7 +1042,7 @@ public void testInClosure4() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testInClosure5() throws Exception { + public void testInClosure5() { String contents = "''.foo {\n" + " delegate.substring()" + @@ -1024,7 +1052,7 @@ public void testInClosure5() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testInClosure6() throws Exception { + public void testInClosure6() { String contents = "''.foo {\n" + " this.substring" + @@ -1034,7 +1062,7 @@ public void testInClosure6() throws Exception { assertUnknownConfidence(contents, start, end, "Search", false); } - public void testInClosure7() throws Exception { + public void testInClosure7() { String contents = "''.foo {\n" + " this\n" + @@ -1044,7 +1072,7 @@ public void testInClosure7() throws Exception { assertType(contents, start, end, "Search", false); } - public void testInClosure8() throws Exception { + public void testInClosure8() { String contents = "new Date().with {\n" + " def t = time\n" + @@ -1054,7 +1082,7 @@ public void testInClosure8() throws Exception { assertType(contents, start, end, "java.lang.Long", false); } - public void testInClosure9() throws Exception { + public void testInClosure9() { String contents = "new Date().with {\n" + " time = 0L\n" + @@ -1066,7 +1094,7 @@ public void testInClosure9() throws Exception { // the declaring type of things inside of a closure should be the declaring // type of the method that calls the closure - public void testInClosureDeclaringType1() throws Exception { + public void testInClosureDeclaringType1() { String contents = "class Baz {\n" + " def method() { }\n" + @@ -1085,7 +1113,7 @@ public void testInClosureDeclaringType1() throws Exception { } // Unknown references should have the declaring type of the closure - public void testInClosureDeclaringType2() throws Exception { + public void testInClosureDeclaringType2() { String contents = "class Baz {\n" + " def method() { }\n" + @@ -1101,7 +1129,7 @@ public void testInClosureDeclaringType2() throws Exception { } // Unknown references should have the delegate type of the closure - public void testInClosureDeclaringType3() throws Exception { + public void testInClosureDeclaringType3() { String contents = "class Baz {\n" + " def method() { }\n" + @@ -1119,7 +1147,7 @@ public void testInClosureDeclaringType3() throws Exception { } // 'this' is always the enclosing type - public void testInClosureDeclaringType4() throws Exception { + public void testInClosureDeclaringType4() { String contents = "class Bar {\n" + " def method() { }\n" + @@ -1133,7 +1161,7 @@ public void testInClosureDeclaringType4() throws Exception { } // 'delegate' always has declaring type of closure - public void testInClosureDeclaringType5() throws Exception { + public void testInClosureDeclaringType5() { // failing on 1.7 if (GroovyUtils.GROOVY_LEVEL > 17) { String contents = @@ -1150,7 +1178,7 @@ public void testInClosureDeclaringType5() throws Exception { } // Unknown references should have the declaring type of the enclosing method - public void testInClassDeclaringType1() throws Exception { + public void testInClassDeclaringType1() { String contents = "class Baz {\n" + " def method() {\n" + @@ -1163,7 +1191,7 @@ public void testInClassDeclaringType1() throws Exception { } // Unknown references should have the declaring type of the enclosing closure - public void testInClassDeclaringType2() throws Exception { + public void testInClassDeclaringType2() { String contents = "class Baz {\n" + " def method = {\n" + @@ -1175,7 +1203,7 @@ public void testInClassDeclaringType2() throws Exception { assertDeclaringType(contents, start, end, "Baz", false, true); } - public void testDoubleClosure1() throws Exception { + public void testDoubleClosure1() { String contents = "''.foo {\n" + " 1.foo {\n" + @@ -1188,7 +1216,7 @@ public void testDoubleClosure1() throws Exception { assertType(contents, start, end, "java.lang.Integer"); } - public void testDoubleClosure2() throws Exception { + public void testDoubleClosure2() { String contents = "''.foo {\n" + " 1.foo {\n" + @@ -1201,7 +1229,7 @@ public void testDoubleClosure2() throws Exception { assertType(contents, start, end, "java.lang.Integer"); } - public void testDoubleClosure2a() throws Exception { + public void testDoubleClosure2a() { String contents = "''.foo {\n" + " 1.foo {\n" + @@ -1215,7 +1243,7 @@ public void testDoubleClosure2a() throws Exception { } // test DGM - public void testDoubleClosure3() throws Exception { + public void testDoubleClosure3() { String contents = "''.foo {\n" + " 1.foo {\n" + @@ -1228,7 +1256,7 @@ public void testDoubleClosure3() throws Exception { assertType(contents, start, end, "java.lang.Integer"); } - public void testDoubleClosure4() throws Exception { + public void testDoubleClosure4() { String contents = "''.foo {\n" + " 1.foo {\n" + @@ -1241,7 +1269,7 @@ public void testDoubleClosure4() throws Exception { assertType(contents, start, end, "java.lang.Integer"); } - public void testDoubleClosure5() throws Exception { + public void testDoubleClosure5() { String contents = "''.foo {\n" + " 1.foo {\n" + @@ -1254,7 +1282,7 @@ public void testDoubleClosure5() throws Exception { assertType(contents, start, end, "java.lang.Integer"); } - public void testDoubleClosure6() throws Exception { + public void testDoubleClosure6() { String contents = "''.foo {\n" + " 1.foo {\n" + @@ -1267,7 +1295,7 @@ public void testDoubleClosure6() throws Exception { assertUnknownConfidence(contents, start, end, "Search", false); } - public void testDoubleClosure7() throws Exception { + public void testDoubleClosure7() { String contents = "''.foo {\n" + " 1.foo {\n" + @@ -1282,7 +1310,7 @@ public void testDoubleClosure7() throws Exception { // GRECLIPSE-1748 // Closure type inference with @CompileStatic - public void testClosureTypeInference1() throws Exception { + public void testClosureTypeInference1() { if (GroovyUtils.GROOVY_LEVEL < 21) { return; } @@ -1309,7 +1337,7 @@ public void testClosureTypeInference1() throws Exception { } // Closure type inference without @CompileStatic - public void testClosureTypeInference2() throws Exception { + public void testClosureTypeInference2() { if (GroovyUtils.GROOVY_LEVEL < 21) { return; } @@ -1485,7 +1513,7 @@ public void testWithAndClosure4() throws Exception { } // Unknown references should have the declaring type of the enclosing closure - public void testInScriptDeclaringType() throws Exception { + public void testInScriptDeclaringType() { String contents = "other\n"; int start = contents.lastIndexOf("other"); @@ -1493,7 +1521,7 @@ public void testInScriptDeclaringType() throws Exception { assertDeclaringType(contents, start, end, "Search", false, true); } - public void testStaticImports1() throws Exception { + public void testStaticImports1() { String contents = "import static javax.swing.text.html.HTML.NULL_ATTRIBUTE_VALUE\n" + "NULL_ATTRIBUTE_VALUE"; int start = contents.lastIndexOf("NULL_ATTRIBUTE_VALUE"); @@ -1501,7 +1529,7 @@ public void testStaticImports1() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testStaticImports2() throws Exception { + public void testStaticImports2() { String contents = "import static javax.swing.text.html.HTML.getAttributeKey\n" + "getAttributeKey('')"; int start = contents.lastIndexOf("getAttributeKey"); @@ -1509,7 +1537,7 @@ public void testStaticImports2() throws Exception { assertType(contents, start, end, "javax.swing.text.html.HTML$Attribute"); } - public void testStaticImports3() throws Exception { + public void testStaticImports3() { String contents = "import static javax.swing.text.html.HTML.*\n" + "NULL_ATTRIBUTE_VALUE"; int start = contents.lastIndexOf("NULL_ATTRIBUTE_VALUE"); @@ -1517,7 +1545,7 @@ public void testStaticImports3() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testStaticImports4() throws Exception { + public void testStaticImports4() { String contents = "import static javax.swing.text.html.HTML.*\n" + "getAttributeKey('')"; int start = contents.lastIndexOf("getAttributeKey"); @@ -1525,7 +1553,7 @@ public void testStaticImports4() throws Exception { assertType(contents, start, end, "javax.swing.text.html.HTML$Attribute"); } - public void testDGM1() throws Exception { + public void testDGM1() { String contents = "\"$print\""; String lookFor = "print"; int start = contents.indexOf(lookFor); @@ -1533,7 +1561,7 @@ public void testDGM1() throws Exception { assertDeclaringType(contents, start, end, "groovy.lang.Script"); } - public void testDGM2() throws Exception { + public void testDGM2() { String contents = "\"${print}\""; String lookFor = "print"; int start = contents.indexOf(lookFor); @@ -1541,7 +1569,7 @@ public void testDGM2() throws Exception { assertDeclaringType(contents, start, end, "groovy.lang.Script"); } - public void testDGM3() throws Exception { + public void testDGM3() { String contents = "class Foo {\n def m() {\n \"${print()}\"\n } }"; String lookFor = "print"; int start = contents.indexOf(lookFor); @@ -1566,26 +1594,26 @@ public void testDGM3() throws Exception { "GetAt g\n" + "g[0].startsWith()"; - public void testGetAt1() throws Exception { + public void testGetAt1() { int start = CONTENTS_GETAT1.indexOf("startsWith"); int end = start + "startsWith".length(); assertDeclaringType(CONTENTS_GETAT1, start, end, "java.lang.String"); } - public void testGetAt2() throws Exception { + public void testGetAt2() { int start = CONTENTS_GETAT1.lastIndexOf("startsWith"); int end = start + "startsWith".length(); assertDeclaringType(CONTENTS_GETAT1, start, end, "java.lang.String"); } - public void testGetAt3() throws Exception { + public void testGetAt3() { int start = CONTENTS_GETAT2.indexOf("startsWith"); int end = start + "startsWith".length(); // expecting unknown confidence because getAt not explicitly defined assertDeclaringType(CONTENTS_GETAT2, start, end, "GetAt", false, true); } - public void testGetAt4() throws Exception { + public void testGetAt4() { int start = CONTENTS_GETAT2.lastIndexOf("startsWith"); int end = start + "startsWith".length(); // expecting unknown confidence because getAt not explicitly defined @@ -1593,7 +1621,7 @@ public void testGetAt4() throws Exception { } // GRECLIPSE-1013 - public void testCategoryMethodAsField() throws Exception { + public void testCategoryMethodAsField() { String contents = "''.toURL().text"; int textStart = contents.indexOf("text"); @@ -1605,40 +1633,40 @@ public void testCategoryMethodAsField() throws Exception { } } - public void testClassReference1() throws Exception { + public void testClassReference1() { String contents = "String"; assertDeclaringType(contents, 0, contents.length(), "java.lang.String"); } - public void testClassReference2() throws Exception { + public void testClassReference2() { String contents = "String.substring"; int textStart = contents.indexOf("substring"); int textEnd = textStart + "substring".length(); assertDeclaringType(contents, textStart, textEnd, "java.lang.String", false, true); } - public void testClassReference3() throws Exception { + public void testClassReference3() { String contents = "String.getPackage()"; int textStart = contents.indexOf("getPackage"); int textEnd = textStart + "getPackage".length(); assertType(contents, textStart, textEnd, "java.lang.Package"); } - public void testClassReference4() throws Exception { + public void testClassReference4() { String contents = "String.class.getPackage()"; int textStart = contents.indexOf("getPackage"); int textEnd = textStart + "getPackage".length(); assertType(contents, textStart, textEnd, "java.lang.Package"); } - public void testClassReference5() throws Exception { + public void testClassReference5() { String contents = "String.class.package"; int textStart = contents.indexOf("package"); int textEnd = textStart + "package".length(); assertType(contents, textStart, textEnd, "java.lang.Package"); } - public void testClassReference6() throws Exception { + public void testClassReference6() { String contents = "String.class"; // in the groovy AST, this is all one ast expression node (a class expression) int textStart = contents.indexOf("String.class"); @@ -1646,7 +1674,7 @@ public void testClassReference6() throws Exception { assertDeclaringType(contents, textStart, textEnd, "java.lang.Class>", false, false); } - public void testMultiDecl1() throws Exception { + public void testMultiDecl1() { String contents = "def (x, y) = []\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1654,7 +1682,7 @@ public void testMultiDecl1() throws Exception { assertType(contents, yStart, yStart + 1, "java.lang.Object"); } - public void testMultiDecl2() throws Exception { + public void testMultiDecl2() { String contents = "def (x, y) = [1]\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1662,7 +1690,7 @@ public void testMultiDecl2() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.Integer"); } - public void testMultiDecl3() throws Exception { + public void testMultiDecl3() { String contents = "def (x, y) = [1,1]\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1670,7 +1698,7 @@ public void testMultiDecl3() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.Integer"); } - public void testMultiDecl4() throws Exception { + public void testMultiDecl4() { String contents = "def (x, y) = [1,'']\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1678,7 +1706,7 @@ public void testMultiDecl4() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.String"); } - public void testMultiDecl6() throws Exception { + public void testMultiDecl6() { String contents = "def (x, y) = new ArrayList()\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1686,7 +1714,7 @@ public void testMultiDecl6() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.Object"); } - public void testMultiDecl7() throws Exception { + public void testMultiDecl7() { String contents = "def (x, y) = new ArrayList()\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1694,7 +1722,7 @@ public void testMultiDecl7() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.Double"); } - public void testMultiDecl8() throws Exception { + public void testMultiDecl8() { String contents = "Double[] meth() { }\ndef (x, y) = meth()\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1702,7 +1730,7 @@ public void testMultiDecl8() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.Double"); } - public void testMultiDecl9() throws Exception { + public void testMultiDecl9() { String contents = "List meth() { }\ndef (x, y) = meth()\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1710,7 +1738,7 @@ public void testMultiDecl9() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.Double"); } - public void testMultiDecl10() throws Exception { + public void testMultiDecl10() { String contents = "List field\ndef (x, y) = field\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1718,7 +1746,7 @@ public void testMultiDecl10() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.Double"); } - public void testMultiDecl11() throws Exception { + public void testMultiDecl11() { String contents = "List field\ndef x\ndef y\n (x, y)= field\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1726,7 +1754,7 @@ public void testMultiDecl11() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.Double"); } - public void testMultiDecl12() throws Exception { + public void testMultiDecl12() { String contents = "def (x, y) = 1d\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1734,7 +1762,7 @@ public void testMultiDecl12() throws Exception { assertType(contents, yStart, yStart+1, "java.lang.Double"); } - public void testMultiDecl13() throws Exception { + public void testMultiDecl13() { String contents = "def (int x, float y) = [1,2]\nx\ny"; int xStart = contents.lastIndexOf("x"); int yStart = contents.lastIndexOf("y"); @@ -1743,20 +1771,20 @@ public void testMultiDecl13() throws Exception { } // GRECLIPSE-1174 groovy casting - public void testAsExpression1() throws Exception { + public void testAsExpression1() { String contents = "(1 as int).intValue()"; int start = contents.lastIndexOf("intValue"); assertType(contents, start, start+"intValue".length(), "java.lang.Integer"); } // GRECLIPSE-1174 groovy casting - public void testAsExpression2() throws Exception { + public void testAsExpression2() { String contents = "class Flar { int x\n }\n(null as Flar).x"; int start = contents.lastIndexOf("x"); assertType(contents, start, start+"x".length(), "java.lang.Integer"); } // GRECLIPSE-1264 - public void testImplicitVar1() throws Exception { + public void testImplicitVar1() { String contents = "class SettingUndeclaredProperty {\n" + " public void mymethod() {\n" + " doesNotExist = \"abc\"\n" + @@ -1767,7 +1795,7 @@ public void testImplicitVar1() throws Exception { } // GRECLIPSE-1264 - public void testImplicitVar2() throws Exception { + public void testImplicitVar2() { String contents = "class SettingUndeclaredProperty {\n" + " def r = {\n" + " doesNotExist = 0\n" + @@ -1778,7 +1806,7 @@ public void testImplicitVar2() throws Exception { } // GRECLIPSE-1264 - public void testImplicitVar3() throws Exception { + public void testImplicitVar3() { String contents = "doesNotExist"; int start = contents.lastIndexOf("doesNotExist"); @@ -1786,14 +1814,14 @@ public void testImplicitVar3() throws Exception { } // GRECLIPSE-1264 - public void testImplicitVar4() throws Exception { + public void testImplicitVar4() { String contents = "doesNotExist = 9"; int start = contents.lastIndexOf("doesNotExist"); assertDeclaringType(contents, start, start+"doesNotExist".length(), "Search", false); } // GRECLIPSE-1264 - public void testImplicitVar5() throws Exception { + public void testImplicitVar5() { String contents = "doesNotExist = 9\n" + "def x = {doesNotExist }"; @@ -1802,7 +1830,7 @@ public void testImplicitVar5() throws Exception { } // GRECLIPSE-1264 - public void testImplicitVar6() throws Exception { + public void testImplicitVar6() { String contents = "" + "def x = {\n" + @@ -1813,7 +1841,7 @@ public void testImplicitVar6() throws Exception { } // GRECLIPSE-1264 - public void testImplicitVar7() throws Exception { + public void testImplicitVar7() { String contents = "def z() {\n" + " doesNotExist = 9\n" + @@ -1823,21 +1851,21 @@ public void testImplicitVar7() throws Exception { } // nested expressions of various forms - public void testNested1() throws Exception { + public void testNested1() { String contents = "(true ? 2 : 7) + 9"; assertType(contents, "java.lang.Integer"); } // nested expressions of various forms - public void testNested2() throws Exception { + public void testNested2() { String contents = "(true ? 2 : 7) + (true ? 2 : 7)"; assertType(contents, "java.lang.Integer"); } // nested expressions of various forms - public void testNested3() throws Exception { + public void testNested3() { String contents = "(8 ?: 7) + (8 ?: 7)"; assertType(contents, "java.lang.Integer"); @@ -1846,12 +1874,11 @@ public void testNested3() throws Exception { // nested expressions of various forms public void testNested4() throws Exception { createUnit("", "Foo", "class Foo { int prop }"); - String contents = - "(new Foo().@prop) + (8 ?: 7)"; + String contents = "(new Foo().@prop) + (8 ?: 7)"; assertType(contents, "java.lang.Integer"); } - public void testPostfix() throws Exception { + public void testPostfix() { // failing for groovy 1.7 if (GroovyUtils.GROOVY_LEVEL > 17) { String contents = @@ -1864,7 +1891,7 @@ public void testPostfix() throws Exception { } // GRECLIPSE-1302 - public void testNothingIsUnknown() throws Exception { + public void testNothingIsUnknown() { assertNoUnknowns("1 > 4\n" + "1 < 1\n" + "1 >= 1\n" + @@ -1874,7 +1901,7 @@ public void testNothingIsUnknown() throws Exception { "[1,9][0]"); } - public void testNothingIsUnknownWithCategories() throws Exception { + public void testNothingIsUnknownWithCategories() { assertNoUnknowns("class Me {\n" + " def meth() {\n" + " use (MeCat) {\n" + @@ -1898,11 +1925,11 @@ public void testNothingIsUnknownWithCategories() throws Exception { } // GRECLIPSE-1304 - public void testNoGString1() throws Exception { + public void testNoGString1() { assertNoUnknowns("'$'\n'${}\n'${a}'\n'$a'"); } - public void testClosureReferencesSuperClass() throws Exception { + public void testClosureReferencesSuperClass() { assertNoUnknowns("class MySuper {\n" + " public void insuper() { }\n" + "}\n" + @@ -1918,7 +1945,7 @@ public void testClosureReferencesSuperClass() throws Exception { } // GRECLIPSE-1341 - public void testDeclarationAtBeginningOfMethod() throws Exception { + public void testDeclarationAtBeginningOfMethod() { String contents = "class Problem2 {\n" + " String action() { }\n" + " def meth() {\n" + @@ -1931,7 +1958,7 @@ public void testDeclarationAtBeginningOfMethod() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testGRECLIPSE1348() throws Exception { + public void testGRECLIPSE1348() { String contents = "class A {\n" + " def myMethod(String owner) {\n" + " return { return owner }\n" + @@ -1942,7 +1969,7 @@ public void testGRECLIPSE1348() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testGRECLIPSE1348a() throws Exception { + public void testGRECLIPSE1348a() { String contents = "class A {\n" + " def myMethod(String notOwner) {\n" + " return { return owner }\n" + @@ -1953,28 +1980,28 @@ public void testGRECLIPSE1348a() throws Exception { assertType(contents, start, end, "A"); } - public void testAnonInner1() throws Exception { + public void testAnonInner1() { String contents = "def foo = new Runnable() { void run() {} }"; int start = contents.lastIndexOf("Runnable"); int end = start + "Runnable".length(); assertType(contents, start, end, "java.lang.Runnable"); } - public void testAnonInner2() throws Exception { + public void testAnonInner2() { String contents = "def foo = new Comparable() { int compareTo(String a, String b) {} }"; int start = contents.lastIndexOf("Comparable"); int end = start + "Comparable".length(); assertType(contents, start, end, "java.lang.Comparable"); } - public void testAnonInner3() throws Exception { + public void testAnonInner3() { String contents = "def foo = new Comparable() { int compareTo(String a, String b) { compareTo()} }"; int start = contents.lastIndexOf("compareTo"); int end = start + "compareTo".length(); assertDeclaringType(contents, start, end, "Search$1"); } - public void testAnonInner4() throws Exception { + public void testAnonInner4() { String contents = "def foo = new Comparable() { int compareTo(String a, String b) {} }\n" + "foo.compareTo"; int start = contents.lastIndexOf("compareTo"); @@ -1982,7 +2009,7 @@ public void testAnonInner4() throws Exception { assertDeclaringType(contents, start, end, "Search$1"); } - public void testAnonInner5() throws Exception { + public void testAnonInner5() { String contents = "def foo = new Comparable() { int compareTo(String a, String b) {} }\n" + "foo = new Comparable() { int compareTo(String a, String b) {} }\n" + "foo.compareTo"; @@ -1992,7 +2019,7 @@ public void testAnonInner5() throws Exception { } // GRECLIPSE-1638 - public void testInstanceOf1() throws Exception { + public void testInstanceOf1() { String contents = "def m(Object obj) {\n" + " def val = obj\n" + @@ -2010,7 +2037,7 @@ public void testInstanceOf1() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testInstanceOf2() throws Exception { + public void testInstanceOf2() { String contents = "@groovy.transform.TypeChecked\n" + "def m(Object obj) {\n" + @@ -2029,7 +2056,7 @@ public void testInstanceOf2() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testInstanceOf3() throws Exception { + public void testInstanceOf3() { String contents = "@groovy.transform.CompileStatic\n" + "def m(Object obj) {\n" + @@ -2048,7 +2075,7 @@ public void testInstanceOf3() throws Exception { assertType(contents, start, end, "java.lang.String"); } - public void testInstanceOf4() throws Exception { + public void testInstanceOf4() { String contents = "def m(Object obj) {\n" + " def val = obj\n" + @@ -2079,7 +2106,7 @@ public void testInstanceOf4() throws Exception { } // GRECLIPSE-554 - public void testMapEntries1() throws Exception { + public void testMapEntries1() { String contents = "def map = [:]\n" + "map.foo = 1\n" + diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/GroovyUtils.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/GroovyUtils.java index 72b7d83a80..39d379a8a8 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/GroovyUtils.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/core/util/GroovyUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2016 the original author or authors. + * Copyright 2009-2017 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. @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.Reader; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.codehaus.groovy.ast.ASTNode; @@ -30,6 +31,7 @@ import org.codehaus.groovy.ast.ClassNode; import org.codehaus.groovy.ast.GenericsType; import org.codehaus.groovy.ast.Parameter; +import org.codehaus.groovy.ast.expr.ArgumentListExpression; import org.codehaus.groovy.ast.expr.Expression; import org.codehaus.groovy.control.SourceUnit; import org.eclipse.jdt.internal.core.util.Util; @@ -144,6 +146,26 @@ public static String[] splitName(ClassNode node) { return new String[] {name.substring(0, Math.max(0, index)), name.substring(index + 1)}; } + public static List getArgumentTypes(ArgumentListExpression list) { + final int n = list.getExpressions().size(); + if (n == 0) return Collections.emptyList(); + List types = new ArrayList(n); + for (Expression expression : list.getExpressions()) { + types.add(expression.getType()); + } + return types; + } + + public static List getParameterTypes(Parameter... params) { + final int n = params.length; + if (n == 0) return Collections.emptyList(); + List types = new ArrayList(n); + for (Parameter param : params) { + types.add(param.getType()); + } + return types; + } + /** * Creates a type signature string for the specified class node, including * its generics if any are present. diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java index 8ce3f56e1e..604662d9cb 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java @@ -41,6 +41,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.ArgumentListExpression; import org.codehaus.groovy.ast.expr.BitwiseNegationExpression; import org.codehaus.groovy.ast.expr.BooleanExpression; import org.codehaus.groovy.ast.expr.ClassExpression; @@ -56,6 +57,7 @@ import org.codehaus.groovy.ast.stmt.BlockStatement; import org.codehaus.jdt.groovy.internal.compiler.ast.JDTMethodNode; import org.codehaus.jdt.groovy.model.GroovyCompilationUnit; +import org.eclipse.jdt.groovy.core.util.GroovyUtils; import org.eclipse.jdt.groovy.search.TypeLookupResult.TypeConfidence; import org.eclipse.jdt.groovy.search.VariableScope.VariableInfo; @@ -262,19 +264,42 @@ private TypeLookupResult findType(Expression node, ClassNode declaringType, Vari return new TypeLookupResult(nodeType, declaringType, nodeType, confidence, scope); } } else if (node instanceof StaticMethodCallExpression) { - StaticMethodCallExpression expr = (StaticMethodCallExpression) node; - List methods = expr.getOwnerType().getMethods(expr.getMethod()); - if (methods.size() > 0) { - MethodNode method = methods.get(0); + StaticMethodCallExpression staticMethodCall = (StaticMethodCallExpression) node; + MethodNode method; + +// method = staticMethodCall.getOwnerType().tryFindPossibleMethod(staticMethodCall.getMethod(), staticMethodCall.getArguments()); +// if (method != null && method.isStatic()) { +// return new TypeLookupResult(method.getReturnType(), method.getDeclaringClass(), method, confidence, scope); +// } + + List methods = staticMethodCall.getOwnerType().getMethods(staticMethodCall.getMethod()); + if (!methods.isEmpty()) { + method = methods.get(0); return new TypeLookupResult(method.getReturnType(), method.getDeclaringClass(), method, confidence, scope); } } else if (node instanceof ConstructorCallExpression) { + ConstructorCallExpression constructorCall = (ConstructorCallExpression) node; + if (constructorCall.isThisCall()) { + declaringType = scope.getEnclosingMethodDeclaration().getDeclaringClass(); + } else if (constructorCall.isSuperCall()) { + declaringType = scope.getEnclosingMethodDeclaration().getDeclaringClass().getUnresolvedSuperClass(); + } + + // try to find best match if there is more than one constructor to choose from List declaredConstructors = declaringType.getDeclaredConstructors(); - if (declaredConstructors != null && declaredConstructors.size() > 0) { - // FIXADE we can do better here and at least match on number of arguments - return new TypeLookupResult(nodeType, declaringType, declaredConstructors.get(0), confidence, scope); + if (constructorCall.getArguments() instanceof ArgumentListExpression && declaredConstructors.size() > 1) { + List callTypes = GroovyUtils.getArgumentTypes((ArgumentListExpression) constructorCall.getArguments()); + for (ConstructorNode ctor : declaredConstructors) { + List declTypes = GroovyUtils.getParameterTypes(ctor.getParameters()); + if (callTypes.equals(declTypes)) { + return new TypeLookupResult(nodeType, declaringType, ctor, TypeConfidence.EXACT, scope); + } + } + // TODO: Perform fuzzy match if no exact match exists. See ClassNode.tryFindPossibleMethod. } - return new TypeLookupResult(nodeType, declaringType, declaringType, confidence, scope); + + ASTNode declaration = !declaredConstructors.isEmpty() ? declaredConstructors.get(0) : declaringType; + return new TypeLookupResult(nodeType, declaringType, declaration, confidence, scope); } // if we get here, then we can't infer the type. Set to unknown if required. diff --git a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java index 8d3b83f10c..cc974215a3 100644 --- a/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java +++ b/base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/TypeInferencingVisitorWithRequestor.java @@ -16,7 +16,6 @@ package org.eclipse.jdt.groovy.search; import java.io.File; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -2072,18 +2071,11 @@ private List getMethodCallArgs() { if (peek instanceof MethodCallExpression) { MethodCallExpression call = (MethodCallExpression) peek; Expression arguments = call.getArguments(); + // TODO: Might be useful also to look into TupleExpression if (arguments instanceof ArgumentListExpression) { - ArgumentListExpression list = (ArgumentListExpression) arguments; - List expressions = list.getExpressions(); - List types = new ArrayList(); - for (Expression expression : expressions) { - types.add(expression.getType()); - } - return types; - } else { - // TODO Might be useful also to look into TupleExpressions like in ArgumentListExpressions - return new ArrayList(); + return GroovyUtils.getArgumentTypes((ArgumentListExpression) arguments); } + return Collections.emptyList(); } return null; }