Summary: Setting up a basic annotation processor. Right now, the processor just saves a map of class -> methods that should be suppressed. Next, this map needs to be turned into a .inferconfig file.master
parent
37a4709a0f
commit
2f25d6344c
@ -1,7 +1,15 @@
|
||||
prebuilt_jar(
|
||||
name = 'annotations',
|
||||
binary_jar = 'annotations.jar',
|
||||
visibility = [
|
||||
'PUBLIC',
|
||||
]
|
||||
name = 'annotations',
|
||||
binary_jar = 'annotations.jar',
|
||||
visibility = [
|
||||
'PUBLIC',
|
||||
]
|
||||
)
|
||||
|
||||
prebuilt_jar(
|
||||
name = 'processor',
|
||||
binary_jar = 'processor.jar',
|
||||
visibility = [
|
||||
'PUBLIC',
|
||||
]
|
||||
)
|
||||
|
@ -1,13 +1,24 @@
|
||||
DEPENDENCIES = ../../dependencies
|
||||
JSR_JAR = $(DEPENDENCIES)/java/jsr-305/jsr305.jar
|
||||
CLASSES_OUT = classes
|
||||
SOURCES = `find . -name "*.java"`
|
||||
ANNOT_SOURCES = `find com/facebook/infer/annotation -name "*.java"`
|
||||
PROCESSOR_SOURCES = `find com/facebook/infer/annotprocess -name "*.java"`
|
||||
ANNOT_CLASSES = 'annot_classes'
|
||||
PROCESSOR_CLASSES = 'processor_classes'
|
||||
|
||||
all:
|
||||
mkdir -p classes
|
||||
javac -cp $(JSR_JAR) $(SOURCES)
|
||||
jar cvf annotations.jar $(SOURCES) `find . -name "*.class"`
|
||||
all: annotations processor
|
||||
|
||||
annotations:
|
||||
mkdir -p $(ANNOT_CLASSES)
|
||||
javac -cp $(JSR_JAR) $(ANNOT_SOURCES) -d $(ANNOT_CLASSES)
|
||||
cd $(ANNOT_CLASSES) && jar cvf annotations.jar com && cp annotations.jar ../
|
||||
|
||||
processor:
|
||||
mkdir -p $(PROCESSOR_CLASSES)
|
||||
javac $(PROCESSOR_SOURCES) -d $(PROCESSOR_CLASSES)
|
||||
cd $(PROCESSOR_CLASSES) && cp -r ../resources/META-INF . && jar cvMf processor.jar com META-INF && cp processor.jar ../
|
||||
|
||||
clean:
|
||||
rm -rvf `find . -name "*.class"`
|
||||
rm -rvf annotations.jar
|
||||
rm -r $(ANNOT_CLASSES)
|
||||
rm annotations.jar
|
||||
rm -r $(PROCESSOR_CLASSES)
|
||||
rm processor.jar
|
||||
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - present Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.infer.annotprocess;
|
||||
|
||||
import javax.annotation.processing.*;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.tools.*;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import java.util.*;
|
||||
|
||||
@SupportedAnnotationTypes({"java.lang.SuppressWarnings"})
|
||||
public class AnnotationProcessor extends AbstractProcessor {
|
||||
|
||||
// map of (classes -> methods in class). an empty set means suppress all warnings on class
|
||||
public Map<String,Set<String>> suppressMap = new LinkedHashMap<String,Set<String>>();
|
||||
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
|
||||
for (TypeElement te : annotations) {
|
||||
for (Element e : env.getElementsAnnotatedWith(te)) {
|
||||
if (e instanceof TypeElement) { // class
|
||||
String className = ((TypeElement) e).getQualifiedName().toString();
|
||||
suppressMap.put(className, Collections.EMPTY_SET);
|
||||
} else if (e instanceof ExecutableElement) { // method
|
||||
String clazz = e.getEnclosingElement().toString();
|
||||
Set<String> suppressMethods = suppressMap.get(clazz);
|
||||
if (suppressMethods == null) {
|
||||
suppressMethods = new LinkedHashSet();
|
||||
suppressMap.put(clazz, suppressMethods);
|
||||
} else if (suppressMethods.isEmpty()) {
|
||||
// empty set means suppress warnings on all methods in class; do nothing
|
||||
continue;
|
||||
}
|
||||
suppressMethods.add(clazz);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (env.processingOver()) {
|
||||
// TODO: write suppressMap to a .inferconfig file on disk
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
return SourceVersion.latestSupported();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
com.facebook.infer.annotprocess.AnnotationProcessor
|
Loading…
Reference in new issue