Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw an exception with a user-friendly message when Errorprone is too old #156

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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