[fixes #3954][fixes #3947] Suppress messages during resolution

This commit is contained in:
Rawi01 committed 2025-09-25 18:38:12 +02:00
1 parent e8fb46179f
commit 93e9bebf0a
11 files changed
+67 -32

No files matched your search

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 The Project Lombok Authors.
* Copyright (C) 2011-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -24,7 +24,10 @@ package lombok.javac;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
@@ -36,7 +39,6 @@ import javax.tools.JavaFileObject;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Log;
import lombok.permit.Permit;
@@ -53,9 +55,14 @@ public final class CompilerMessageSuppressor {
private static final Field deferDiagnosticsField, deferredDiagnosticsField, diagnosticHandlerField;
private static final ConcurrentMap<Class<?>, Field> handlerDeferredFields = new ConcurrentHashMap<Class<?>, Field>();
private static final Field NULL_FIELD;
private static final Class<?> DIAGNOSTIC_HANDLER;
private static final Class<?> DISCARD_DIAGNOSTIC_HANDLER;
private static final Method POP_DIAGNOSTIC_HANDLER;
private static final Constructor<?> DISCARD_DIAGNOSTIC_HANDLER_CONSTRUCTOR;
private Boolean dumpOnError, promptOnError;
private DiagnosticListener<?> contextDiagnosticListener, logDiagnosticListener;
private final Context context;
private Object diagnosticHandler;
private static final ThreadLocal<Queue<?>> queueCache = new ThreadLocal<Queue<?>>();
@@ -87,6 +94,11 @@ public final class CompilerMessageSuppressor {
diagnosticHandlerField = getDeclaredField(Log.class, "diagnosticHandler");
NULL_FIELD = getDeclaredField(JavacResolution.class, "NULL_FIELD");
DIAGNOSTIC_HANDLER = getClass("com.sun.tools.javac.util.Log$DiagnosticHandler");
DISCARD_DIAGNOSTIC_HANDLER = getClass("com.sun.tools.javac.util.Log$DiscardDiagnosticHandler");
POP_DIAGNOSTIC_HANDLER = DIAGNOSTIC_HANDLER != null ? Permit.permissiveGetMethod(Log.class, "popDiagnosticHandler", DIAGNOSTIC_HANDLER) : null;
DISCARD_DIAGNOSTIC_HANDLER_CONSTRUCTOR = DISCARD_DIAGNOSTIC_HANDLER != null ? Permit.permissiveGetConstructor(DISCARD_DIAGNOSTIC_HANDLER, Log.class) : null;
}
static Field getDeclaredField(Class<?> c, String fieldName) {
@@ -97,6 +109,14 @@ public final class CompilerMessageSuppressor {
}
}
static Class<?> getClass(String name) {
try {
return Class.forName(name);
} catch (Throwable t) {
return null;
}
}
public CompilerMessageSuppressor(Context context) {
this.log = Log.instance(context);
this.context = context;
@@ -118,16 +138,6 @@ public final class CompilerMessageSuppressor {
}
} catch (Exception e) {}
if (diagnosticHandlerField != null) try {
Object handler = diagnosticHandlerField.get(log);
Field field = getDeferredField(handler);
if (field != null) {
queueCache.set((Queue<?>) field.get(handler));
Queue<?> empty = new LinkedList<Object>();
field.set(handler, empty);
}
} catch (Exception e) {}
if (dumpOnErrorField != null) try {
dumpOnError = (Boolean) dumpOnErrorField.get(log);
dumpOnErrorField.set(log, false);
@@ -145,6 +155,11 @@ public final class CompilerMessageSuppressor {
diagnosticListenerField.set(log, null);
} catch (Exception e) {
}
if (DISCARD_DIAGNOSTIC_HANDLER != null) try {
diagnosticHandler = Permit.newInstance(DISCARD_DIAGNOSTIC_HANDLER_CONSTRUCTOR, log);
} catch (Exception e) {
}
}
private static Field getDeferredField(Object handler) {
@@ -183,19 +198,15 @@ public final class CompilerMessageSuppressor {
logDiagnosticListener = null;
} catch (Exception e) {}
if (diagnosticHandlerField != null && queueCache.get() != null) try {
Object handler = diagnosticHandlerField.get(log);
Field field = getDeferredField(handler);
if (field != null) {
field.set(handler, queueCache.get());
queueCache.set(null);
}
} catch (Exception e) {}
if (deferDiagnosticsField != null && queueCache.get() != null) try {
deferredDiagnosticsField.set(log, queueCache.get());
queueCache.set(null);
} catch (Exception e) {}
if (diagnosticHandler != null) try {
Permit.invoke(POP_DIAGNOSTIC_HANDLER, log, diagnosticHandler);
} catch (Exception e) {}
diagnosticHandler = null;
}
public void removeAllBetween(JavaFileObject sourcefile, int startPos, int endPos) {
@@ -222,11 +233,12 @@ public final class CompilerMessageSuppressor {
if (field == null || receiver == null) return;
try {
ListBuffer<?> deferredDiagnostics = (ListBuffer<?>) field.get(receiver);
ListBuffer<Object> newDeferredDiagnostics = new ListBuffer<Object>();
Collection<?> deferredDiagnostics = (Collection<?>) field.get(receiver);
if (deferredDiagnostics.isEmpty()) return;
LinkedList<Object> newDeferredDiagnostics = new LinkedList<Object>();
for (Object diag_ : deferredDiagnostics) {
if (!(diag_ instanceof JCDiagnostic)) {
newDeferredDiagnostics.append(diag_);
newDeferredDiagnostics.add(diag_);
continue;
}
JCDiagnostic diag = (JCDiagnostic) diag_;
@@ -234,7 +246,7 @@ public final class CompilerMessageSuppressor {
if (here >= startPos && here < endPos && diag.getSource() == sourcefile) {
// We eliminate it
} else {
newDeferredDiagnostics.append(diag);
newDeferredDiagnostics.add(diag);
}
}
field.set(receiver, newDeferredDiagnostics);
+2
View File
@@ -68,6 +68,7 @@ import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
import com.sun.tools.javac.util.Log;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Position;
/**
* Wraps around javac's internal AST view to add useful features as well as the ability to visit parents from children,
@@ -595,6 +596,7 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> {
}
public void removeFromDeferredDiagnostics(int startPos, int endPos) {
if (startPos == Position.NOPOS || endPos == Position.NOPOS) return;
JCCompilationUnit self = (JCCompilationUnit) top().get();
new CompilerMessageSuppressor(getContext()).removeAllBetween(self.sourcefile, startPos, endPos);
}
+3 -3
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 The Project Lombok Authors.
* Copyright (C) 2011-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -416,7 +416,7 @@ public class JavacResolution {
}
private static int compare(Name a, Name b) {
return a.compareTo(b);
return a.toString().compareTo(b.toString());
}
private static boolean isLocalType(TypeSymbol symbol) {
@@ -469,7 +469,7 @@ public class JavacResolution {
winLevel = level;
continue;
}
if (compare(winner.tsym.getQualifiedName(), t.tsym.getQualifiedName()) < 0) winner = t;
if (compare(winner.tsym.getQualifiedName(), t.tsym.getQualifiedName()) > 0) winner = t;
}
if (winner == null) return createJavaLangObject(ast);
return typeToJCTree(winner, ast, allowCompound, allowVoid, allowCapture);
+9 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2021 The Project Lombok Authors.
* Copyright (C) 2018-2025 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -165,6 +165,14 @@ public class Permit {
return setAccessible(c.getDeclaredConstructor(parameterTypes));
}
public static <T> Constructor<T> permissiveGetConstructor(Class<T> c, Class<?>... parameterTypes) {
try {
return getConstructor(c, parameterTypes);
} catch (Exception ignore) {
return null;
}
}
private static Object reflectiveStaticFieldAccess(Class<?> c, String fName) {
try {
Field f = c.getDeclaredField(fName);
@@ -2,6 +2,8 @@
import java.util.function.Function;
public class ExtensionMethodInLambda {
private static final Function<String, String> testStatic = s -> ExtensionMethodInLambda.Extensions.reverse(s);
public void testSimple() {
String test = "test";
test = ExtensionMethodInLambda.Extensions.map(test, s -> ExtensionMethodInLambda.Extensions.reverse(s));
@@ -15,6 +15,9 @@ public @ExtensionMethod(value = ExtensionMethodInLambda.Extensions.class) class
return "0";
}
}
private static final Function<String, String> testStatic = (<no type> s) -> ExtensionMethodInLambda.Extensions.reverse(s);
<clinit>() {
}
public ExtensionMethodInLambda() {
super();
}
@@ -4,6 +4,8 @@ import lombok.experimental.ExtensionMethod;
@ExtensionMethod(value = ExtensionMethodInLambda.Extensions.class)
public class ExtensionMethodInLambda {
private static final Function<String, String> testStatic = s -> s.reverse();
public void testSimple() {
String test = "test";
test = test.map(s -> s.reverse());
@@ -1 +1 @@
19 cannot find symbol symbol: method invalid((s)->s.reverse()) location: variable test of type java.lang.String
21 cannot find symbol symbol: method invalid((s)->s.reverse()) location: variable test of type java.lang.String
@@ -1 +1,7 @@
6 Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved
7 Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved
8 Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved
9 Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved
10 Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved
11 Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved
12 Cannot use 'val' here because initializer expression does not have a representable type: Type cannot be resolved
@@ -1 +1 @@
19 The method invalid((<no type> s) -> {}) is undefined for the type String
21 The method invalid((<no type> s) -> {}) is undefined for the type String
@@ -1 +1 @@
17 cannot find symbol symbol: method invalid((s)->s.reverse()) location: variable test of type java.lang.String
19 cannot find symbol symbol: method invalid((s)->s.reverse()) location: variable test of type java.lang.String