]> git.uio.no Git - ifi-stolz-refaktor.git/commitdiff
RefaktorHandleUtils: fixing bug with methods declaring exceptions
authorErlend Kristiansen <erlenkr@ifi.uio.no>
Wed, 12 Feb 2014 19:41:58 +0000 (20:41 +0100)
committerErlend Kristiansen <erlenkr@ifi.uio.no>
Wed, 12 Feb 2014 19:41:58 +0000 (20:41 +0100)
software/no.uio.ifi.refaktor.examples/examples/src/div/Main.java [new file with mode: 0644]
software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/FindMethodHandleTest.java [new file with mode: 0644]
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/NullMethodHandle.java [new file with mode: 0644]
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/RefaktorHandleUtils.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/SignatureUtils.java [new file with mode: 0644]

diff --git a/software/no.uio.ifi.refaktor.examples/examples/src/div/Main.java b/software/no.uio.ifi.refaktor.examples/examples/src/div/Main.java
new file mode 100644 (file)
index 0000000..499aae2
--- /dev/null
@@ -0,0 +1,6 @@
+package div;
+
+public class Main {
+       private void method(String s) throws Exception {
+       }
+}
\ No newline at end of file
diff --git a/software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/FindMethodHandleTest.java b/software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/FindMethodHandleTest.java
new file mode 100644 (file)
index 0000000..6febd02
--- /dev/null
@@ -0,0 +1,33 @@
+package no.uio.ifi.refaktor.tests;
+
+import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat;
+import no.uio.ifi.refaktor.examples.manager.ExampleCodeManager;
+import no.uio.ifi.refaktor.utils.RefaktorHandleUtils;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.jdt.core.IMethod;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FindMethodHandleTest {
+
+       @Before
+       public void setUp() throws Exception {
+               ExampleCodeManager.INSTANCE.loadExampleCode();
+       }
+
+       @Test
+       public void testFindMethodHandle() throws Exception {
+               IMethod method = getTestDataMethod("method", "QString;");
+               assertThat(method.exists());
+               
+               String simpleSignature = "private void method(String s) throws Exception";
+               IProject project = ExampleCodeManager.INSTANCE.getProject();
+               IMethod methodSimpleSignature = RefaktorHandleUtils.findMethodHandle(project, "div", "Main", simpleSignature);
+               assertThat("Method exists", methodSimpleSignature.exists());
+       }
+
+       private IMethod getTestDataMethod(String methodName, String... parameters) {
+               return RefaktorHandleUtils.findMethodHandleChecked(ExampleCodeManager.INSTANCE.getProject(), "div", "Main", methodName, parameters);
+       }
+}
diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/NullMethodHandle.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/NullMethodHandle.java
new file mode 100644 (file)
index 0000000..1c3b2d9
--- /dev/null
@@ -0,0 +1,316 @@
+package no.uio.ifi.refaktor.utils;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.jobs.ISchedulingRule;
+import org.eclipse.jdt.core.IAnnotation;
+import org.eclipse.jdt.core.IClassFile;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaModel;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.ILocalVariable;
+import org.eclipse.jdt.core.IMemberValuePair;
+import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.IOpenable;
+import org.eclipse.jdt.core.ISourceRange;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.ITypeParameter;
+import org.eclipse.jdt.core.ITypeRoot;
+import org.eclipse.jdt.core.JavaModelException;
+
+public class NullMethodHandle implements IMethod {
+
+       @Override
+       public String[] getCategories() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public IClassFile getClassFile() {
+               return null;
+       }
+
+       @Override
+       public ICompilationUnit getCompilationUnit() {
+               return null;
+       }
+
+       @Override
+       public IType getDeclaringType() {
+               return null;
+       }
+
+       @Override
+       public int getFlags() throws JavaModelException {
+               return 0;
+       }
+
+       @Override
+       public ISourceRange getJavadocRange() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public int getOccurrenceCount() {
+               return 0;
+       }
+
+       @Override
+       public ITypeRoot getTypeRoot() {
+               return null;
+       }
+
+       @Override
+       public IType getType(String name, int occurrenceCount) {
+               return null;
+       }
+
+       @Override
+       public boolean isBinary() {
+               return false;
+       }
+
+       @Override
+       public boolean exists() {
+               return false;
+       }
+
+       @Override
+       public IJavaElement getAncestor(int ancestorType) {
+               return null;
+       }
+
+       @Override
+       public String getAttachedJavadoc(IProgressMonitor monitor)
+                       throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public IResource getCorrespondingResource() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public int getElementType() {
+               return 0;
+       }
+
+       @Override
+       public String getHandleIdentifier() {
+               return null;
+       }
+
+       @Override
+       public IJavaModel getJavaModel() {
+               return null;
+       }
+
+       @Override
+       public IJavaProject getJavaProject() {
+               return null;
+       }
+
+       @Override
+       public IOpenable getOpenable() {
+               return null;
+       }
+
+       @Override
+       public IJavaElement getParent() {
+               return null;
+       }
+
+       @Override
+       public IPath getPath() {
+               return null;
+       }
+
+       @Override
+       public IJavaElement getPrimaryElement() {
+               return null;
+       }
+
+       @Override
+       public IResource getResource() {
+               return null;
+       }
+
+       @Override
+       public ISchedulingRule getSchedulingRule() {
+               return null;
+       }
+
+       @Override
+       public IResource getUnderlyingResource() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public boolean isReadOnly() {
+               return false;
+       }
+
+       @Override
+       public boolean isStructureKnown() throws JavaModelException {
+               return false;
+       }
+
+       @Override
+       public Object getAdapter(Class adapter) {
+               return null;
+       }
+
+       @Override
+       public String getSource() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public ISourceRange getSourceRange() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public ISourceRange getNameRange() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public void copy(IJavaElement container, IJavaElement sibling,
+                       String rename, boolean replace, IProgressMonitor monitor)
+                       throws JavaModelException {
+
+       }
+
+       @Override
+       public void delete(boolean force, IProgressMonitor monitor)
+                       throws JavaModelException {
+
+       }
+
+       @Override
+       public void move(IJavaElement container, IJavaElement sibling,
+                       String rename, boolean replace, IProgressMonitor monitor)
+                       throws JavaModelException {
+
+       }
+
+       @Override
+       public void rename(String name, boolean replace, IProgressMonitor monitor)
+                       throws JavaModelException {
+
+       }
+
+       @Override
+       public IJavaElement[] getChildren() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public boolean hasChildren() throws JavaModelException {
+               return false;
+       }
+
+       @Override
+       public IAnnotation getAnnotation(String name) {
+               return null;
+       }
+
+       @Override
+       public IAnnotation[] getAnnotations() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public IMemberValuePair getDefaultValue() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public String getElementName() {
+               return null;
+       }
+
+       @Override
+       public String[] getExceptionTypes() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public String[] getTypeParameterSignatures() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public ITypeParameter[] getTypeParameters() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public int getNumberOfParameters() {
+               return 0;
+       }
+
+       @Override
+       public ILocalVariable[] getParameters() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public String getKey() {
+               return null;
+       }
+
+       @Override
+       public String[] getParameterNames() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public String[] getParameterTypes() {
+               return null;
+       }
+
+       @Override
+       public String[] getRawParameterNames() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public String getReturnType() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public String getSignature() throws JavaModelException {
+               return null;
+       }
+
+       @Override
+       public ITypeParameter getTypeParameter(String name) {
+               return null;
+       }
+
+       @Override
+       public boolean isConstructor() throws JavaModelException {
+               return false;
+       }
+
+       @Override
+       public boolean isMainMethod() throws JavaModelException {
+               return false;
+       }
+
+       @Override
+       public boolean isResolved() {
+               return false;
+       }
+
+       @Override
+       public boolean isSimilar(IMethod method) {
+               return false;
+       }
+}
index 8f9569a218cb784e45ed803e907a0cecefb4b3df..0cb2b982a0184ae0fb9a574a3be1fe3f09462c80 100644 (file)
@@ -73,17 +73,16 @@ public class RefaktorHandleUtils {
        private static IMethod findMethodHandleThatMatchesSimpleSignature(IMethod[] methods, String simpleSignature)
                        throws JavaModelException {
                for (IMethod method: methods) {
-                       String methodSource = method.getSource();
-                       if (methodSource == null)
-                               continue;
-                       if (makeSimpleSignature(methodSource).equals(simpleSignature)) 
+                       String normalizedMethodSimpleSignature = SignatureUtils.createNormalizedSimpleSignature(method);
+                       String normalizedSimpleSignature = SignatureUtils.createNormalizedSimpleSignature(simpleSignature);
+                       if (normalizeSimpleSignature(normalizedMethodSimpleSignature).equals(normalizedSimpleSignature)) 
                                return method;
                }
-               return null;
+               return new NullMethodHandle();
        }
 
-       private static String makeSimpleSignature(String methodSource) {
-               return methodSource.substring(0, methodSource.indexOf(')') + 1).trim();
+       private static String normalizeSimpleSignature(String simpleSignature) {
+               return simpleSignature.substring(0, simpleSignature.indexOf(')') + 1).trim();
        }
 
        public static IJavaProject createJavaProjectFrom(IProject project) {
diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/SignatureUtils.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/SignatureUtils.java
new file mode 100644 (file)
index 0000000..5011ed1
--- /dev/null
@@ -0,0 +1,29 @@
+package no.uio.ifi.refaktor.utils;
+
+import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.JavaModelException;
+
+public class SignatureUtils {
+
+       public static String createNormalizedSimpleSignature(IMethod method) throws JavaModelException {
+               String source = method.getSource();
+               return createNormalizedSimpleSignature(flattenSource(source == null ? "" : source));
+       }
+
+       private static String flattenSource(String methodSource) {
+               return methodSource.replaceAll("\\s+", " ");
+       }
+
+       public static String createNormalizedSimpleSignature(String source) {
+               return normalizeSimpleSignature(findSimpleSignature(source));
+       }
+
+       private static String findSimpleSignature(String flatSource) {
+               return flatSource.substring(0, flatSource.indexOf(')') + 1).trim();
+       }
+       
+       private static String normalizeSimpleSignature(String simpleSignature) {
+               return simpleSignature.replaceAll("\\s+", " ").replaceAll("\\s*\\(\\s*", "(").replaceAll("\\s+\\)", ")").replaceAll("\\s+,", ",");
+       }
+
+}