summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorsalsolatragus2012-11-18 13:16:00 (EST)
committer salsolatragus2012-11-18 13:16:00 (EST)
commit087114b8f794ab1049430cf052a7b6af5cad97d3 (patch)
tree057993f897c27cc65d56017a8af8288cc2e240f2
parent709840764490776d9ede277a41c705357ad72e17 (diff)
downloadorg.eclipse.recommenders-087114b8f794ab1049430cf052a7b6af5cad97d3.zip
org.eclipse.recommenders-087114b8f794ab1049430cf052a7b6af5cad97d3.tar.gz
org.eclipse.recommenders-087114b8f794ab1049430cf052a7b6af5cad97d3.tar.bz2
[tests] tests and fix for method/constructor binding resolutionrefs/changes/49/8749/3
-rw-r--r--plugins/org.eclipse.recommenders.utils.rcp/src/org/eclipse/recommenders/utils/rcp/CompilerBindings.java7
-rw-r--r--tests/org.eclipse.recommenders.tests.completion.rcp/src/org/eclipse/recommenders/tests/completion/rcp/CompilerBindingsTest.java65
2 files changed, 66 insertions, 6 deletions
diff --git a/plugins/org.eclipse.recommenders.utils.rcp/src/org/eclipse/recommenders/utils/rcp/CompilerBindings.java b/plugins/org.eclipse.recommenders.utils.rcp/src/org/eclipse/recommenders/utils/rcp/CompilerBindings.java
index fbbdadc..7a93a7e 100644
--- a/plugins/org.eclipse.recommenders.utils.rcp/src/org/eclipse/recommenders/utils/rcp/CompilerBindings.java
+++ b/plugins/org.eclipse.recommenders.utils.rcp/src/org/eclipse/recommenders/utils/rcp/CompilerBindings.java
@@ -27,6 +27,7 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
+import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
import org.eclipse.jdt.internal.corext.template.java.SignatureUtil;
@@ -114,7 +115,9 @@ public class CompilerBindings {
}
try {
final String uniqueKey = String.valueOf(binding.computeUniqueKey());
- final String qualifiedMethodName = StringUtils.substringBefore(uniqueKey, "(").replace(";.", ".");
+ String qualifiedMethodName = StringUtils.substringBefore(uniqueKey, "(").replace(";.", ".");
+ if (qualifiedMethodName.endsWith("."))
+ qualifiedMethodName += new String(TypeConstants.INIT);
final String[] parameterTypes = Signature.getParameterTypes(uniqueKey);
final String returnType = Signature.getReturnType(uniqueKey);
final StringBuilder sb = new StringBuilder();
@@ -126,7 +129,7 @@ public class CompilerBindings {
final IMethodName res = VmMethodName.get(sb.toString());
return of(res);
} catch (final RuntimeException e) {
- // if the signature could not be parsed by JDT (because it it incomplete!):
+ // if the signature could not be parsed by JDT (because it is incomplete!):
return absent();
}
}
diff --git a/tests/org.eclipse.recommenders.tests.completion.rcp/src/org/eclipse/recommenders/tests/completion/rcp/CompilerBindingsTest.java b/tests/org.eclipse.recommenders.tests.completion.rcp/src/org/eclipse/recommenders/tests/completion/rcp/CompilerBindingsTest.java
index fbf3c27..be5a7b5 100644
--- a/tests/org.eclipse.recommenders.tests.completion.rcp/src/org/eclipse/recommenders/tests/completion/rcp/CompilerBindingsTest.java
+++ b/tests/org.eclipse.recommenders.tests.completion.rcp/src/org/eclipse/recommenders/tests/completion/rcp/CompilerBindingsTest.java
@@ -15,8 +15,11 @@ import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
+import org.eclipse.recommenders.utils.names.IMethodName;
import org.eclipse.recommenders.utils.names.ITypeName;
+import org.eclipse.recommenders.utils.names.VmMethodName;
import org.eclipse.recommenders.utils.names.VmTypeName;
import org.eclipse.recommenders.utils.rcp.CompilerBindings;
import org.junit.Test;
@@ -27,7 +30,7 @@ public class CompilerBindingsTest {
@Test
public void testCanParseSimpleType() {
- final ReferenceBinding mock = createSimpleBinding("Ltest/Class;");
+ final ReferenceBinding mock = createTypeBinding("Ltest/Class;");
final Optional<ITypeName> actual = CompilerBindings.toTypeName(mock);
assertTrue(actual.isPresent());
@@ -35,15 +38,69 @@ public class CompilerBindingsTest {
@Test
public void testCanParsePrimitiveType() {
- final ReferenceBinding mock = createSimpleBinding("J");
+ final ReferenceBinding mock = createTypeBinding("J");
final Optional<ITypeName> actual = CompilerBindings.toTypeName(mock);
assertEquals(VmTypeName.LONG, actual.get());
}
- private ReferenceBinding createSimpleBinding(final String type) {
+ @Test
+ public void testCanParseMethodWithOneArgAndPrimitiveReturnType() {
+ final MethodBinding mock = createMethodBinding("Ljava/lang/Object;.equals(Ljava/lang/Object;)Z");
+
+ final Optional<IMethodName> actual = CompilerBindings.toMethodName(mock);
+ assertEquals(VmMethodName.get("Ljava/lang/Object.equals(Ljava/lang/Object;)Z"), actual.get());
+ }
+
+ @Test
+ public void testCanParseMethodWithoutArgsAndReturnType() {
+ final MethodBinding mock = createMethodBinding("Lmy/package/Obj;.method()V");
+
+ final Optional<IMethodName> actual = CompilerBindings.toMethodName(mock);
+ assertEquals(VmMethodName.get("Lmy/package/Obj.method()V"), actual.get());
+ }
+
+ @Test
+ public void testCanParseMethodWithPrimitiveArgsAndSimpleReturnType() {
+ final MethodBinding mock = createMethodBinding("Lsome/Impl;.callee(IJZ)Lmy/arg/Type;");
+
+ final Optional<IMethodName> actual = CompilerBindings.toMethodName(mock);
+ assertEquals(VmMethodName.get("Lsome/Impl.callee(IJZ)Lmy/arg/Type;"), actual.get());
+ }
+
+ @Test
+ public void testCanParseMethodWithPrimitiveAndSimpleTypeArgs() {
+ final MethodBinding mock = createMethodBinding("Land/a/Last;.one(SLArg;C)V");
+
+ final Optional<IMethodName> actual = CompilerBindings.toMethodName(mock);
+ assertEquals(VmMethodName.get("Land/a/Last.one(SLArg;C)V"), actual.get());
+ }
+
+ @Test
+ public void testCanParseConstructorWithoutArgs() {
+ final MethodBinding mock = createMethodBinding("Ljava/lang/Object;.()V");
+
+ final Optional<IMethodName> actual = CompilerBindings.toMethodName(mock);
+ assertEquals(VmMethodName.get("Ljava/lang/Object.<init>()V"), actual.get());
+ }
+
+ @Test
+ public void testCanParseConstructorWithArgs() {
+ final MethodBinding mock = createMethodBinding("Lanother/Impl;.(ILsome/Instance;)V");
+
+ final Optional<IMethodName> actual = CompilerBindings.toMethodName(mock);
+ assertEquals(VmMethodName.get("Lanother/Impl.<init>(ILsome/Instance;)V"), actual.get());
+ }
+
+ private ReferenceBinding createTypeBinding(final String type) {
final ReferenceBinding mock = mock(ReferenceBinding.class);
- when(mock.computeUniqueKey()).thenReturn("J".toCharArray());
+ when(mock.computeUniqueKey()).thenReturn(type.toCharArray());
+ return mock;
+ }
+
+ private MethodBinding createMethodBinding(final String method) {
+ final MethodBinding mock = mock(MethodBinding.class);
+ when(mock.computeUniqueKey()).thenReturn(method.toCharArray());
return mock;
}
}