Search in sources :

Example 1 with DiagnosticsReceiver

use of lombok.core.DiagnosticsReceiver in project lombok by rzwitserloot.

the class LombokProcessor method placePostCompileAndDontMakeForceRoundDummiesHook.

private void placePostCompileAndDontMakeForceRoundDummiesHook() {
    stopJavacProcessingEnvironmentFromClosingOurClassloader();
    forceMultipleRoundsInNetBeansEditor();
    Context context = processingEnv.getContext();
    disablePartialReparseInNetBeansEditor(context);
    try {
        Method keyMethod = Context.class.getDeclaredMethod("key", Class.class);
        keyMethod.setAccessible(true);
        Object key = keyMethod.invoke(context, JavaFileManager.class);
        Field htField = Context.class.getDeclaredField("ht");
        htField.setAccessible(true);
        @SuppressWarnings("unchecked") Map<Object, Object> ht = (Map<Object, Object>) htField.get(context);
        final JavaFileManager originalFiler = (JavaFileManager) ht.get(key);
        if (!(originalFiler instanceof InterceptingJavaFileManager)) {
            final Messager messager = processingEnv.getMessager();
            DiagnosticsReceiver receiver = new MessagerDiagnosticsReceiver(messager);
            JavaFileManager newFiler = new InterceptingJavaFileManager(originalFiler, receiver);
            ht.put(key, newFiler);
            Field filerFileManagerField = JavacFiler.class.getDeclaredField("fileManager");
            filerFileManagerField.setAccessible(true);
            filerFileManagerField.set(processingEnv.getFiler(), newFiler);
        }
    } catch (Exception e) {
        throw Lombok.sneakyThrow(e);
    }
}
Also used : Context(com.sun.tools.javac.util.Context) Messager(javax.annotation.processing.Messager) JavaFileManager(javax.tools.JavaFileManager) Method(java.lang.reflect.Method) IOException(java.io.IOException) Field(java.lang.reflect.Field) DiagnosticsReceiver(lombok.core.DiagnosticsReceiver) JavaFileObject(javax.tools.JavaFileObject) Map(java.util.Map) IdentityHashMap(java.util.IdentityHashMap)

Example 2 with DiagnosticsReceiver

use of lombok.core.DiagnosticsReceiver in project lombok by rzwitserloot.

the class TestPostCompiler method testPostCompiler.

@Test
public void testPostCompiler() throws IOException {
    byte[] compiled = TestClassFileMetaData.compile(new File("test/bytecode/resource/PostCompileSneaky.java"));
    DiagnosticsReceiver receiver = new DiagnosticsReceiver() {

        @Override
        public void addWarning(String message) {
            fail("Warning during post compilation processing of a sneakyThrow call: " + message);
        }

        @Override
        public void addError(String message) {
            fail("Error during post compilation processing of a sneakyThrow call: " + message);
        }
    };
    assertTrue("Before post compilation, expected lombok.Lombok.sneakyThrow() call in compiled code, but it's not there", new ClassFileMetaData(compiled).usesMethod("lombok/Lombok", "sneakyThrow"));
    byte[] transformed = PostCompiler.applyTransformations(compiled, "PostCompileSneaky.java", receiver);
    assertNotSame("Post-compiler did not do anything; we expected it to remove a Lombok.sneakyThrow() call.", compiled, transformed);
    assertTrue("After removing a sneakyThrow the classfile got... bigger (or stayed equal in size). Huh?", transformed.length < compiled.length);
}
Also used : DiagnosticsReceiver(lombok.core.DiagnosticsReceiver) File(java.io.File) Test(org.junit.Test)

Aggregations

DiagnosticsReceiver (lombok.core.DiagnosticsReceiver)2 Context (com.sun.tools.javac.util.Context)1 File (java.io.File)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 IdentityHashMap (java.util.IdentityHashMap)1 Map (java.util.Map)1 Messager (javax.annotation.processing.Messager)1 JavaFileManager (javax.tools.JavaFileManager)1 JavaFileObject (javax.tools.JavaFileObject)1 Test (org.junit.Test)1