diff --git a/infer/annotations/com/facebook/infer/annotprocess/AnnotationProcessor.java b/infer/annotations/com/facebook/infer/annotprocess/CollectSuppressWarnings.java similarity index 72% rename from infer/annotations/com/facebook/infer/annotprocess/AnnotationProcessor.java rename to infer/annotations/com/facebook/infer/annotprocess/CollectSuppressWarnings.java index acde0c68c..2c2dd9446 100644 --- a/infer/annotations/com/facebook/infer/annotprocess/AnnotationProcessor.java +++ b/infer/annotations/com/facebook/infer/annotprocess/CollectSuppressWarnings.java @@ -29,7 +29,7 @@ import javax.lang.model.element.TypeElement; import javax.lang.model.util.Elements; @SupportedAnnotationTypes({ "java.lang.SuppressWarnings" }) -public class AnnotationProcessor extends AbstractProcessor { +public class CollectSuppressWarnings extends AbstractProcessor { private static final String ANNOTATION_ENV_VAR = "INFER_ANNOTATIONS_OUT"; @@ -42,43 +42,6 @@ public class AnnotationProcessor extends AbstractProcessor { // total number of classes/methods with a SuppressWarnings annotation private int mNumToSuppress = 0; - // print a comma between all objects except for the last one - private void outputCommaIfNotLast(PrintWriter out, int elemCount) { - if (elemCount == mNumToSuppress) { - out.println(""); - } else { - out.println(","); - } - } - - // output a method to suppress in JSON format - // clearly, we should be using an existing JSON output library like Jackson here. however, we - // can't do this because we do not want to add Jackson (or another JSON library) to the classpath - // of the Java program we are building (along with the JAR for this processor). the reason is that - // if there is a different version of the JSON parser library somewhere in the classpath for the - // project, it could cause *very* strange problems. instead, we rolled our own library to avoid - // introducing additional dependencies - private void outputMethod(PrintWriter out, String clazz, String method, int elemCount) { - String TAB1 = " "; - String TAB2 = TAB1 + TAB1; - out.println(TAB1 + "{"); - out.println(TAB2 +"\"language\": \"Java\","); - out.print(TAB2 + "\"class\": \"" + clazz + "\""); - if (method != null) { - out.println(","); - out.println(TAB2 + "\"method\": \"" + method + "\""); - } else { - out.println(); - } - out.print(TAB1 + "}"); - outputCommaIfNotLast(out, elemCount); - } - - // output a class to suppress in JSON format - private void outputClass(PrintWriter out, String clazz, int elemCount) { - outputMethod(out, clazz, null, elemCount); - } - // write the methods/classes to suppress to .inferconfig-style JSON private void exportSuppressMap() throws FileNotFoundException, IOException { Map env = System.getenv(); @@ -96,10 +59,10 @@ public class AnnotationProcessor extends AbstractProcessor { String clazz = entry.getKey(); Set methods = entry.getValue(); if (methods.isEmpty()) { // empty set of methods means annotation is on class - outputClass(out, clazz, ++elemCount); + JSONOutputUtils.outputClass(out, clazz, ++elemCount, mNumToSuppress); } else { for (String method : methods) { - outputMethod(out, clazz, method, ++elemCount); + JSONOutputUtils.outputMethod(out, clazz, method, ++elemCount, mNumToSuppress); } } } diff --git a/infer/annotations/com/facebook/infer/annotprocess/JSONOutputUtils.java b/infer/annotations/com/facebook/infer/annotprocess/JSONOutputUtils.java new file mode 100644 index 000000000..839ac5184 --- /dev/null +++ b/infer/annotations/com/facebook/infer/annotprocess/JSONOutputUtils.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2016 - 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 java.io.PrintWriter; + +/** Clearly, we should be using an existing JSON output library like Jackson here. However, we can't + * do this because we do not want to add Jackson (or another JSON library) to the classpath of the + * Java program we are building (along with the JAR for this processor). The reason is that if + * there is a different version of the JSON parser library somewhere in the classpath for the + * project, it could cause *very* strange problems. Instead, we rol our own library to avoid + * introducing depencies on code that the projects we analyze might be using. + */ +public class JSONOutputUtils { + + private JSONOutputUtils() {} + + // print a comma between all JSON objects except for the last one + public static void outputCommaIfNotLast(PrintWriter out, int elemCount, int elemMax) { + if (elemCount == elemMax) { + out.println(""); + } else { + out.println(","); + } + } + + public static void outputMethod(PrintWriter out, String clazz, String method, + int elemCount, int elemMax) { + String TAB1 = " "; + String TAB2 = TAB1 + TAB1; + out.println(TAB1 + "{"); + out.println(TAB2 +"\"language\": \"Java\","); + out.print(TAB2 + "\"class\": \"" + clazz + "\""); + if (method != null) { + out.println(","); + out.println(TAB2 + "\"method\": \"" + method + "\""); + } else { + out.println(); + } + out.print(TAB1 + "}"); + outputCommaIfNotLast(out, elemCount, elemMax); + } + + public static void outputClass(PrintWriter out, String clazz, int elemCount, int elemMax) { + outputMethod(out, clazz, null, elemCount, elemMax); + } + + public static void outputClassSourcePair(PrintWriter out, String clazz, String source, + int elemCount, int elemMax) { + String TAB1 = " "; + String TAB2 = TAB1 + TAB1; + out.println(TAB1 + "{"); + out.print(TAB2 + "\"class\": \"" + clazz + "\""); + out.println(","); + out.println(TAB2 + "\"source\": \"" + source + "\""); + + out.print(TAB1 + "}"); + outputCommaIfNotLast(out, elemCount, elemMax); + } + +} diff --git a/infer/annotations/resources/META-INF/services/javax.annotation.processing.Processor b/infer/annotations/resources/META-INF/services/javax.annotation.processing.Processor index 72d885fc8..a4c881ef2 100644 --- a/infer/annotations/resources/META-INF/services/javax.annotation.processing.Processor +++ b/infer/annotations/resources/META-INF/services/javax.annotation.processing.Processor @@ -1 +1 @@ -com.facebook.infer.annotprocess.AnnotationProcessor +com.facebook.infer.annotprocess.CollectSuppressWarnings