Skip to content

Commit

Permalink
#29 avoid binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiming committed Jan 7, 2019
1 parent d6827ab commit fc44de0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class LogAnalyzer extends ASTVisitor {

private static boolean useLogCategory = false;

private int test;
private boolean test;

public LogAnalyzer(int isTest) {
public LogAnalyzer(boolean isTest) {
this.test = isTest;
}

Expand Down Expand Up @@ -220,7 +220,7 @@ public Set<LogInvocation> getLogInvocationSet() {
return this.logInvocationSet;
}

public void setTest(int test) {
public void setTest(boolean test) {
this.test = test;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
import org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring;
Expand Down Expand Up @@ -52,17 +51,6 @@ static Set<ITypeBinding> getAllClasses(ITypeBinding type) {
return ret;
}

// if isTest == 1, then it is junit test
public static Level isLogExpression(MethodInvocation node, int isTest) {
if (isTest != 1) {
IMethodBinding methodBinding = node.resolveMethodBinding();

if (methodBinding == null
|| !methodBinding.getDeclaringClass().getQualifiedName().equals("java.util.logging.Logger"))
return null;
}
return isLogExpression(node);
}

/**
* We only focus on the logging level, which is set by the developer. Hence, we
Expand All @@ -73,7 +61,15 @@ public static Level isLogExpression(MethodInvocation node, int isTest) {
* @param node
* @return logging level
*/
public static Level isLogExpression(MethodInvocation node) {
// if isTest == 1, then it is junit test
public static Level isLogExpression(MethodInvocation node, boolean isTest) {
if (!isTest) {
IMethodBinding methodBinding = node.resolveMethodBinding();

if (methodBinding == null
|| !methodBinding.getDeclaringClass().getQualifiedName().equals("java.util.logging.Logger"))
return null;
}

String methodName = node.getName().toString();

Expand Down Expand Up @@ -101,7 +97,7 @@ public static Level isLogExpression(MethodInvocation node) {
// TODO: may need wala?
// They should not be null
if (methodName.equals("log")) {
Level loggingLevel = getLogLevel(firstArgument);
Level loggingLevel = getLogLevel(firstArgument, isTest);
if (loggingLevel == null) {
throw new IllegalStateException("The log level cannot be detected.");
}
Expand All @@ -111,7 +107,7 @@ public static Level isLogExpression(MethodInvocation node) {
}

if (methodName.equals("logp") || methodName.equals("logrb")) {
Level loggingLevel = getLogLevel(firstArgument);
Level loggingLevel = getLogLevel(firstArgument, isTest);
if (loggingLevel == Level.ALL || loggingLevel == Level.OFF)
return null;
return loggingLevel;
Expand All @@ -126,9 +122,9 @@ public static Level isLogExpression(MethodInvocation node) {
* @param argument
* @return logging level
*/
public static Level getLogLevel(Expression firstArg) {
public static Level getLogLevel(Expression firstArg, boolean isTest) {
ITypeBinding typeBinding = firstArg.resolveTypeBinding();
if (typeBinding == null || !typeBinding.getQualifiedName().equals("java.util.logging.Level"))
if ((!isTest) && (typeBinding == null || !typeBinding.getQualifiedName().equals("java.util.logging.Level")))
return null;
String argument = firstArg.toString();
if (argument.contains("Level.SEVERE"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public static Test setUpTest(Test test) {
protected ICompilationUnit createCUfromTestFile(IPackageFragment pack, String cuName) throws Exception {

ICompilationUnit unit = super.createCUfromTestFile(pack, cuName);

if (!unit.isStructureKnown())
throw new IllegalArgumentException(cuName + " has structural errors.");

Path directory = Paths.get(unit.getParent().getParent().getParent().getResource().getLocation().toString());

Expand All @@ -73,15 +76,15 @@ protected ICompilationUnit createCUfromTestFile(IPackageFragment pack, String cu
private void helper(LogInvocationExpectedResult... expectedResults) throws Exception {

// compute the actual results.
ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
ICompilationUnit cu = this.createCUfromTestFile(this.getPackageP(), "A");

ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setResolveBindings(true);
parser.setSource(cu);

parser.setKind(ASTParser.K_COMPILATION_UNIT);
ASTNode ast = parser.createAST(new NullProgressMonitor());

LogAnalyzer logAnalyzer = new LogAnalyzer(1);
LogAnalyzer logAnalyzer = new LogAnalyzer(true);
ast.accept(logAnalyzer);

logAnalyzer.analyze();
Expand Down

0 comments on commit fc44de0

Please sign in to comment.