Skip to content

Commit 30674c2

Browse files
committed
Reduce --add-opens list
- remove --add-opens flags if possible. - some usages (e.g. reflective constructor invocation) allow to change opens into exports when only public API is used - add comments to some of the encapsulation breaking usage
1 parent b03bb6c commit 30674c2

File tree

17 files changed

+29
-79
lines changed

17 files changed

+29
-79
lines changed

ide/editor.settings.lib/src/org/netbeans/modules/editor/settings/storage/preferences/PreferencesImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ private void asyncInvocationOfFlushSpi() {
478478
flushTask.schedule(200);
479479
}
480480

481+
// TODO this adds --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED
481482
// XXX: we probably should not extends AbstractPreferences and do it all ourselfs
482483
// including the firing of events. For the events delivery we could just reuse common
483484
// RequestProcessor threads.

ide/editor.util/src/org/netbeans/lib/editor/util/swing/DocumentUtilities.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ public static boolean isWriteLocked(Document doc) {
521521
} catch (NoSuchFieldException ex) {
522522
throw new IllegalStateException(ex);
523523
}
524+
// TODO adds --add-opens=java.desktop/javax.swing.text=ALL-UNNAMED
524525
f.setAccessible(true);
525526
currWriterField = f;
526527
}

ide/xsl/nbproject/project.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ javac.compilerargs=-Xlint -Xlint:-serial
1919
javac.source=1.8
2020

2121
test.config.stableBTD.includes=**/*Test.class
22+
23+
# at org.apache.tomcat.util.security.PrivilegedSetAccessControlContext.<clinit>(PrivilegedSetAccessControlContext.java:41)
24+
test.jms.flags=--add-opens=java.base/java.lang=ALL-UNNAMED

java/form/src/org/netbeans/modules/form/FormLAF.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public static PreviewInfo initPreviewLaf(Class lafClass, ClassLoader formClassLo
7575
!MetalLookAndFeel.class.equals(lafClass) && (lafToTheme.get(MetalLookAndFeel.class) == null)) {
7676
lafToTheme.put(MetalLookAndFeel.class, MetalLookAndFeel.getCurrentTheme());
7777
}
78+
// adds
79+
// --add-exports=java.desktop/com.sun.java.swing.plaf.gtk=ALL-UNNAMED
80+
// --add-exports=java.desktop/com.sun.java.swing.plaf.windows=ALL-UNNAMED
81+
// --add-exports=java.desktop/com.apple.eio=ALL-UNNAMED
82+
// --add-exports=java.desktop/com.sun.java.swing.plaf.motif=ALL-UNNAMED
7883
LookAndFeel previewLookAndFeel = (LookAndFeel)lafClass.getDeclaredConstructor().newInstance();
7984
if (previewLafIsMetal) {
8085
MetalTheme theme = lafToTheme.get(lafClass);
@@ -209,6 +214,7 @@ private static void initialize() throws Exception {
209214
}
210215

211216
java.lang.reflect.Method method = UIManager.class.getDeclaredMethod("getLAFState", new Class[0]); // NOI18N
217+
// TODO adds --add-opens=java.desktop/javax.swing=ALL-UNNAMED
212218
method.setAccessible(true);
213219
Object lafState = method.invoke(null, new Object[0]);
214220
method = lafState.getClass().getDeclaredMethod("setLookAndFeelDefaults", new Class[] {UIDefaults.class}); // NOI18N
@@ -373,6 +379,7 @@ private static void copyMultiUIDefaults(UIDefaults what, Map where) {
373379
// in MultiUIDefaults in JDK 6 Update 10
374380
try {
375381
java.lang.reflect.Method method = Hashtable.class.getDeclaredMethod("getIterator", new Class[] {int.class}); // NOI18N
382+
// TODO adds --add-opens=java.base/java.util=ALL-UNNAMED
376383
method.setAccessible(true);
377384
Object i = method.invoke(what, new Object[] {2/*Hashtable.ENTRIES*/});
378385
if (i instanceof Iterator) {

java/form/src/org/netbeans/modules/form/HandleLayer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,6 +2872,7 @@ private static void paintDraggedComponent(Component comp, Graphics g, float opac
28722872
static {
28732873
try {
28742874
Field field = Component.class.getDeclaredField("valid"); // NOI18N
2875+
// TODO adds --add-opens=java.desktop/java.awt=ALL-UNNAMED
28752876
field.setAccessible(true);
28762877
componentValidField = field;
28772878
} catch (NoSuchFieldException ex) {

java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ public void report(JCDiagnostic diag) {
990990
Env<AttrContext> result = attr.attributeAndCapture((JCTree) tree, env, (JCTree) to);
991991
try {
992992
Constructor<JavacScope> c = JavacScope.class.getDeclaredConstructor(Env.class);
993+
// TODO this adds --add-opens=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
993994
c.setAccessible(true);
994995
return c.newInstance(result);
995996
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {

java/java.source.base/src/org/netbeans/modules/java/source/NoJavacHelper.java

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,7 @@
1818
*/
1919
package org.netbeans.modules.java.source;
2020

21-
import java.lang.reflect.Field;
22-
import java.lang.reflect.Method;
23-
import java.security.ProtectionDomain;
24-
import java.util.logging.Level;
25-
import java.util.logging.Logger;
2621
import javax.lang.model.SourceVersion;
27-
import org.objectweb.asm.ClassWriter;
28-
import org.objectweb.asm.Opcodes;
29-
import org.openide.modules.OnStart;
3022

3123
/**
3224
*
@@ -51,57 +43,4 @@ public class NoJavacHelper {
5143
public static boolean hasWorkingJavac() {
5244
return HAS_WORKING_JAVAC;
5345
}
54-
55-
// safety net if someone manages to start NB on JDK 8 with nb-javac uninstalled
56-
@OnStart
57-
public static class FixClasses implements Runnable {
58-
59-
@Override
60-
public void run() {
61-
if (!hasWorkingJavac()) {
62-
String JavaVersion = System.getProperty("java.specification.version"); //NOI18N
63-
boolean isJdkVer8OrBelow = true;
64-
if (!JavaVersion.startsWith("1.")) { //NOI18N
65-
isJdkVer8OrBelow = false;
66-
}
67-
if (isJdkVer8OrBelow) {
68-
{
69-
ClassWriter w = new ClassWriter(0);
70-
w.visit(Opcodes.V1_8, Opcodes.ACC_ABSTRACT | Opcodes.ACC_PUBLIC, "com/sun/tools/javac/code/Scope$WriteableScope", null, "com/sun/tools/javac/code/Scope", null);
71-
byte[] classData = w.toByteArray();
72-
73-
defineClass("com.sun.tools.javac.code.Scope$WriteableScope",
74-
"com.sun.tools.javac.code.Scope",
75-
classData);
76-
}
77-
{
78-
ClassWriter w = new ClassWriter(0);
79-
w.visit(Opcodes.V1_8, Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_PUBLIC, "javax/lang/model/element/ModuleElement", null, "java/lang/Object", new String[] {"javax/lang/model/element/Element"});
80-
byte[] classData = w.toByteArray();
81-
82-
defineClass("javax.lang.model.element.ModuleElement",
83-
"com.sun.tools.javac.code.Scope",
84-
classData);
85-
}
86-
}
87-
}
88-
}
89-
90-
private void defineClass(String fqn, String injectToClass, byte[] classData) {
91-
try {
92-
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
93-
Field theUnsafe = unsafeClass.getDeclaredField("theUnsafe"); //NOI18N
94-
theUnsafe.setAccessible(true);
95-
Object unsafe = theUnsafe.get(null);
96-
97-
Class targetClass = Class.forName(injectToClass); //NOI18N
98-
99-
Method defineClass = unsafeClass.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class); //NOI18N
100-
defineClass.invoke(unsafe, fqn, classData, 0, classData.length, targetClass.getClassLoader(), targetClass.getProtectionDomain()); //NOI18N
101-
} catch (Throwable t) {
102-
//ignore...
103-
Logger.getLogger(NoJavacHelper.class.getName()).log(Level.WARNING, null, t);
104-
}
105-
}
106-
}
10746
}

java/java.source.base/src/org/netbeans/modules/java/source/builder/TreeFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,7 @@ public ReferenceTree Reference(ExpressionTree qualExpr, CharSequence member, Lis
20042004
paramTypesParam = lbl.toList();
20052005
}
20062006
Constructor<DCReference> c = DCReference.class.getDeclaredConstructor(String.class, JCExpression.class, JCTree.class, javax.lang.model.element.Name.class, List.class);
2007+
// TODO this adds --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
20072008
c.setAccessible(true);
20082009
DCReference result = c.newInstance("", (JCTree.JCExpression) qualExpr, qualExpr == null ? null : ((JCTree.JCExpression) qualExpr).getTree(), member != null ? (com.sun.tools.javac.util.Name) names.fromString(member.toString()) : null, paramTypesParam);
20092010
result.pos = NOPOS;

java/java.source.base/src/org/netbeans/modules/java/source/indexing/VanillaCompileWorker.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,14 @@ protected ParsingOutput compile(
301301
List<Element> activeTypes = new ArrayList<>();
302302
if (unit.getValue().jfo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE)) {
303303
final PackageTree pt = unit.getKey().getPackage();
304-
if (pt instanceof JCPackageDecl) {
305-
final Element sym = ((JCPackageDecl)pt).packge;
304+
if (pt instanceof JCPackageDecl decl) {
305+
final Element sym = decl.packge;
306306
if (sym != null)
307307
activeTypes.add(sym);
308308
}
309309
} else {
310310
for (Tree tree : unit.getKey().getTypeDecls()) {
311-
if (tree instanceof JCTree) {
312-
final JCTree jct = (JCTree)tree;
311+
if (tree instanceof JCTree jct) {
313312
if (jct.getTag() == JCTree.Tag.CLASSDEF) {
314313
final Element sym = ((JCClassDecl)tree).sym;
315314
if (sym != null)
@@ -480,7 +479,8 @@ private static void fallbackCopyExistingClassFiles(final Context context,
480479
} catch (IOException | InterruptedException | ExecutionException ex) {
481480
Exceptions.printStackTrace(ex);
482481
}
483-
}
482+
}
483+
484484
private static void copyRecursively(FileObject source, File targetRoot, File target, Set<String> filter, FileManagerTransaction fmtx, List<File> copied) throws IOException {
485485
if (source.isFolder()) {
486486
if (target.exists() && !target.isDirectory()) {
@@ -1114,6 +1114,7 @@ private void clearTypeVar(Type.TypeVar tv) {
11141114
for (String boundName : boundNames) {
11151115
try {
11161116
Field bound = tv.getClass().getDeclaredField(boundName);
1117+
// TODO this adds --add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
11171118
bound.setAccessible(true);
11181119
bound.set(tv, error2Object((Type) bound.get(tv)));
11191120
} catch (IllegalAccessException | NoSuchFieldException | SecurityException ex) {

java/java.source.base/src/org/netbeans/modules/java/source/parsing/Hacks.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private static JCDiagnostic getJCDiagnostic(Diagnostic<?> d) {
7878
} else if ("org.netbeans.modules.java.source.parsing.CompilationInfoImpl$DiagnosticListenerImpl$D".equals(d.getClass().getName())) {
7979
try {
8080
Field delegate = d.getClass().getDeclaredField("delegate");
81+
// TODO this adds --add-opens=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
8182
delegate.setAccessible(true);
8283
return getJCDiagnostic((Diagnostic<?>) delegate.get(d));
8384
} catch (Exception ex) {

0 commit comments

Comments
 (0)