import javax.annotation.processing.*; import javax.lang.model.*; import javax.lang.model.element.*; import javax.lang.model.type.*; import javax.lang.model.util.*; import java.io.*; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; @SupportedAnnotationTypes({"Silent"}) public class SilentProcessor extends AbstractProcessor { @Override public SourceVersion getSupportedSourceVersion() { return SourceVersion.latest(); } @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { System.out.println("running SilentProcessor on " + annotations); return true; } public static void main(String[] args) throws Exception { boolean truncate = args.length != 0; FileOutputStream fos = new FileOutputStream("ap.jar"); JarOutputStream jos = new JarOutputStream(fos); add(jos, "Silent.class", truncate); add(jos, "SilentProcessor.class", truncate); jos.putNextEntry(new JarEntry("META-INF/services/javax.annotation.processing.Processor")); jos.write("SilentProcessor".getBytes()); jos.closeEntry(); jos.close(); fos.close(); } private static void add(JarOutputStream jos, String classFilePath, boolean truncate) throws Exception { jos.putNextEntry(new JarEntry(classFilePath)); byte[] data = readClassfile(classFilePath); if (truncate) { jos.write(data, 0, data.length - 100); } else { jos.write(readClassfile(classFilePath)); } jos.closeEntry(); } private static byte[] readClassfile(String classFilePath) throws Exception { InputStream stream = SilentProcessor.class.getResourceAsStream(classFilePath); int bytesToRead = stream.available(); byte[] classFile = new byte[bytesToRead]; new DataInputStream(stream).readFully(classFile); return classFile; } }