Skip to content

Commit

Permalink
fix: throw an exception if user uses unsupoprted Errorprone
Browse files Browse the repository at this point in the history
refs #153
  • Loading branch information
KengoTODA committed Feb 11, 2022
1 parent bb79cd7 commit 71a7d1d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Expand Up @@ -4,6 +4,7 @@

import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.ErrorProneVersion;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
Expand Down Expand Up @@ -97,7 +98,7 @@ private static final class LoggerInitializerVisitor
extends TreeScanner<TypeSymbol, VisitorState> {
@Override
public TypeSymbol visitMethodInvocation(MethodInvocationTree node, VisitorState state) {
if (!isGetLogger.matches(node, state)) {
if (!MatherHolder.isGetLogger.matches(node, state)) {
return null;
}

Expand All @@ -106,8 +107,31 @@ public TypeSymbol visitMethodInvocation(MethodInvocationTree node, VisitorState
Type typeParameter = type.getTypeArguments().get(0);
return typeParameter.asElement();
}
}

static final class MatherHolder {
static {
boolean supported =
ErrorProneVersion.loadVersionFromPom()
.transform(MatherHolder::checkSupportedVersion)
.or(true);
if (!supported) {
throw new IllegalStateException("Run this rule with Errorprone 2.11.0 or later.");
}
}

static boolean checkSupportedVersion(String version) {
String[] split = version.split("\\.", 3);
int major = Integer.parseInt(split[0], 10);
if (major > 2) {
// assuming this version uses new API definition
return true;
}
int minor = Integer.parseInt(split[1], 10);
return minor >= 11;
}

private final Matcher<ExpressionTree> isGetLogger =
static Matcher<ExpressionTree> isGetLogger =
MethodMatchers.staticMethod()
.onClass("org.slf4j.LoggerFactory")
.named("getLogger")
Expand Down
@@ -1,5 +1,8 @@
package jp.skypencil.errorprone.slf4j;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
Expand All @@ -9,6 +12,13 @@
import org.junit.Test;

public class IllegalPassedClassTest {
@Test
public void testSupportedVersion() {
assertTrue(IllegalPassedClass.MatherHolder.checkSupportedVersion("3.0.0"));
assertTrue(IllegalPassedClass.MatherHolder.checkSupportedVersion("2.11.0"));
assertFalse(IllegalPassedClass.MatherHolder.checkSupportedVersion("2.10.0"));
}

@Test
public void testRefactoringInstanceField() throws IOException {
BugChecker checker = new IllegalPassedClass();
Expand Down

0 comments on commit 71a7d1d

Please sign in to comment.