Skip to content

Commit

Permalink
Fix hasAnnotationOnAnyOverriddenMethod to handle missing annotation t…
Browse files Browse the repository at this point in the history
…ypes

MOE_MIGRATED_REVID=126229029
  • Loading branch information
cushon committed Jul 8, 2016
1 parent 633c5a5 commit f4ab2da
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 12 deletions.
15 changes: 6 additions & 9 deletions core/src/main/java/com/google/errorprone/matchers/Matchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -815,20 +815,17 @@ public static Matcher<MethodTree> hasAnnotationOnAnyOverriddenMethod(
@Override
public boolean matches(MethodTree tree, VisitorState state) {
MethodSymbol methodSym = ASTHelpers.getSymbol(tree);
Symbol annotationSym = state.getSymbolFromString(annotationType);
if ((methodSym == null) || (annotationSym == null)) {
if (methodSym == null) {
return false;
}

Set<MethodSymbol> allMethods = ASTHelpers.findSuperMethods(methodSym, state.getTypes());
allMethods.add(methodSym);

for (MethodSymbol method : allMethods) {
if (method.attribute(annotationSym) != null) {
if (ASTHelpers.hasAnnotation(methodSym, annotationType, state)) {
return true;
}
for (MethodSymbol method : ASTHelpers.findSuperMethods(methodSym, state.getTypes())) {
if (ASTHelpers.hasAnnotation(method, annotationType, state)) {
return true;
}
}

return false;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,32 @@

package com.google.errorprone.bugpatterns;

import com.google.common.io.ByteStreams;
import com.google.errorprone.CompilationTestHelper;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.JUnit4;
import org.junit.runners.ParentRunner;

/**
* @author glorioso@google.com (Nick Glorioso)
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

/** @author glorioso@google.com (Nick Glorioso) */
@RunWith(JUnit4.class)
public class JUnit4SetUpNotRunTest {

@Rule public final TemporaryFolder tempFolder = new TemporaryFolder();

private CompilationTestHelper compilationHelper;

@Before
Expand All @@ -51,4 +64,42 @@ public void testNegativeCases() throws Exception {
compilationHelper.addSourceFile("JUnit4SetUpNotRunNegativeCases.java").doTest();
}

public abstract static class SuperTest {
@Before
public void setUp() {}
}

@Test
public void noBeforeOnClasspath() throws Exception {
File libJar = tempFolder.newFile("lib.jar");
try (FileOutputStream fis = new FileOutputStream(libJar);
JarOutputStream jos = new JarOutputStream(fis)) {
addClassToJar(jos, RunWith.class);
addClassToJar(jos, JUnit4.class);
addClassToJar(jos, BlockJUnit4ClassRunner.class);
addClassToJar(jos, ParentRunner.class);
addClassToJar(jos, SuperTest.class);
addClassToJar(jos, SuperTest.class.getEnclosingClass());
}
compilationHelper
.addSourceLines(
"Test.java",
"import org.junit.runner.RunWith;",
"import org.junit.runners.JUnit4;",
"import " + SuperTest.class.getCanonicalName() + ";",
"@RunWith(JUnit4.class)",
"class Test extends SuperTest {",
" @Override public void setUp() {}",
"}")
.setArgs(Arrays.asList("-cp", libJar.toString()))
.doTest();
}

static void addClassToJar(JarOutputStream jos, Class<?> clazz) throws IOException {
String entryPath = clazz.getName().replace('.', '/') + ".class";
try (InputStream is = clazz.getClassLoader().getResourceAsStream(entryPath)) {
jos.putNextEntry(new JarEntry(entryPath));
ByteStreams.copy(is, jos);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.sample;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
import java.util.Set;

/** A no-op annotation processor. */
@SupportedAnnotationTypes({})
public class NullAnnotationProcessor extends AbstractProcessor {
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latest();
}

@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
return false;
}
}

0 comments on commit f4ab2da

Please sign in to comment.