8008207: Make constants array and source fields private
authorsundar
Wed, 20 Feb 2013 17:08:32 +0530
changeset 16250 96a7a6218ad3
parent 16249 45ef95e878be
child 16251 4a5d15b2f168
8008207: Make constants array and source fields private Reviewed-by: hannesw, lagergren
nashorn/src/jdk/nashorn/internal/codegen/ClassEmitter.java
nashorn/src/jdk/nashorn/internal/codegen/Compiler.java
--- a/nashorn/src/jdk/nashorn/internal/codegen/ClassEmitter.java	Tue Feb 19 20:33:07 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/codegen/ClassEmitter.java	Wed Feb 20 17:08:32 2013 +0530
@@ -212,11 +212,11 @@
     private void defineCommonStatics(final boolean strictMode) {
         // source - used to store the source data (text) for this script.  Shared across
         // compile units.  Set externally by the compiler.
-        field(EnumSet.of(Flag.PUBLIC, Flag.STATIC), SOURCE.tag(), Source.class);
+        field(EnumSet.of(Flag.PRIVATE, Flag.STATIC), SOURCE.tag(), Source.class);
 
         // constants - used to the constants array for this script.  Shared across
         // compile units.  Set externally by the compiler.
-        field(EnumSet.of(Flag.PUBLIC, Flag.STATIC), CONSTANTS.tag(), Object[].class);
+        field(EnumSet.of(Flag.PRIVATE, Flag.STATIC), CONSTANTS.tag(), Object[].class);
 
         // strictMode - was this script compiled in strict mode.  Set externally by the compiler.
         field(EnumSet.of(Flag.PUBLIC, Flag.STATIC, Flag.FINAL), STRICT_MODE.tag(), boolean.class, strictMode);
--- a/nashorn/src/jdk/nashorn/internal/codegen/Compiler.java	Tue Feb 19 20:33:07 2013 +0530
+++ b/nashorn/src/jdk/nashorn/internal/codegen/Compiler.java	Wed Feb 20 17:08:32 2013 +0530
@@ -33,6 +33,10 @@
 import static jdk.nashorn.internal.codegen.CompilerConstants.THIS;
 
 import java.io.File;
+import java.lang.reflect.Field;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.Arrays;
 import java.util.EnumSet;
 import java.util.HashMap;
@@ -274,10 +278,23 @@
             }
 
             try {
-                //use reflection to write source and constants table to installed classes
-                clazz.getField(SOURCE.tag()).set(null, getSource());
-                clazz.getField(CONSTANTS.tag()).set(null, getConstantData().toArray());
-            } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
+                final Source source = getSource();
+                final Object[] constants = getConstantData().toArray();
+                // Need doPrivileged because these fields are private
+                AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
+                    @Override
+                    public Void run() throws Exception {
+                        //use reflection to write source and constants table to installed classes
+                        final Field sourceField = clazz.getDeclaredField(SOURCE.tag());
+                        final Field constantsField = clazz.getDeclaredField(CONSTANTS.tag());
+                        sourceField.setAccessible(true);
+                        constantsField.setAccessible(true);
+                        sourceField.set(null, source);
+                        constantsField.set(null, constants);
+                        return null;
+                    }
+                });
+            } catch (final PrivilegedActionException e) {
                 throw new RuntimeException(e);
             }
         }