Skip to content

Commit

Permalink
Merge pull request #2 from marcphilipp/better-markers
Browse files Browse the repository at this point in the history
Markers on method names
  • Loading branch information
davidburkhart committed Mar 18, 2012
2 parents 43f3572 + b32e63e commit 22019a2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
1 change: 1 addition & 0 deletions unused.methods/pdetestsrc/test/Actor.java_
Expand Up @@ -10,6 +10,7 @@ public class Actor {
public void lonelyMethod() {
}

@Deprecated
public static void lonelyStaticMethod() {
}

Expand Down
26 changes: 7 additions & 19 deletions unused.methods/src/unused/methods/core/UnusedMethodsMarker.java
@@ -1,6 +1,7 @@
package unused.methods.core;

import static org.eclipse.core.resources.IMarker.LINE_NUMBER;
import static org.eclipse.core.resources.IMarker.CHAR_END;
import static org.eclipse.core.resources.IMarker.CHAR_START;
import static org.eclipse.core.resources.IMarker.MESSAGE;
import static org.eclipse.core.resources.IMarker.SEVERITY;
import static org.eclipse.core.resources.IMarker.SEVERITY_WARNING;
Expand All @@ -10,8 +11,8 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

public class UnusedMethodsMarker {

Expand Down Expand Up @@ -39,24 +40,11 @@ private static void addMarker(IMethod method) throws CoreException {
JavaCore.getJavaCore().configureJavaElementMarker(marker, method);
marker.setAttribute(MESSAGE, "Method " + method.getElementName() + " is not used.");
marker.setAttribute(SEVERITY, SEVERITY_WARNING);
marker.setAttribute(LINE_NUMBER, findLine(method));
}
}

private static int findLine(IMethod method) throws JavaModelException {
String sourceOfCompilationUnit = method.getCompilationUnit().getSource();
int offsetInCharacters = method.getSourceRange().getOffset();
String linesBeforeMethod = sourceOfCompilationUnit.substring(0, offsetInCharacters);
return countLines(linesBeforeMethod);
}

private static int countLines(String text) {
int countLines = 1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '\n') {
countLines++;
ISourceRange nameRange = method.getNameRange();
if (nameRange != null) {
marker.setAttribute(CHAR_START, nameRange.getOffset());
marker.setAttribute(CHAR_END, nameRange.getOffset() + nameRange.getLength());
}
}
return countLines;
}
}
3 changes: 2 additions & 1 deletion unused.methods/test/unused/methods/core/AllPdeTests.java
Expand Up @@ -5,7 +5,8 @@
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ FindUnusedMethodsInJavaProjectPdeTest.class, FindUnusedContructorsPdeTest.class })
@SuiteClasses({ FindUnusedMethodsInJavaProjectPdeTest.class, FindUnusedContructorsPdeTest.class,
LineNumbersPdeTest.class })
// TODO test Markers
public class AllPdeTests {

Expand Down
41 changes: 41 additions & 0 deletions unused.methods/test/unused/methods/core/LineNumbersPdeTest.java
@@ -0,0 +1,41 @@
package unused.methods.core;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.SourceRange;
import org.junit.Test;

public class LineNumbersPdeTest extends PdeTestCaseWithTestProject {

@Test
public void findUnusedMethodsInProject() throws CoreException, IOException {
List<IMethod> unusedMethods = calculateUnusedMethods();

IMethod lonelyMethod = findMethodByName(unusedMethods, "lonelyMethod");
assertThat(lonelyMethod.getNameRange(), is((ISourceRange) new SourceRange(128, 12)));

IMethod lonelyStaticMethod = findMethodByName(unusedMethods, "lonelyStaticMethod");
assertThat(lonelyStaticMethod.getNameRange(), is((ISourceRange) new SourceRange(182, 18)));
}

private IMethod findMethodByName(List<IMethod> unusedMethods, String name) {
for (IMethod method : unusedMethods) {
if (name.equals(method.getElementName())) {
return method;
}
}
return null;
}

@Override
protected String[] getTestFiles() {
return new String[] { "/test/Movie.java", "/test/Actor.java" };
}
}
Expand Up @@ -13,8 +13,6 @@
import org.junit.Before;
import org.junit.Rule;

import unused.methods.core.FindUnusedMethodsInJavaProjects;

/**
* Do not use classes from jdt in example code as no jre or classpath is set up
* in test project!!!
Expand Down

0 comments on commit 22019a2

Please sign in to comment.