changeset 105:58eea0e8f369

8008207: Make constants array and source fields private Reviewed-by: hannesw, lagergren
author sundar
date Wed, 20 Feb 2013 17:08:32 +0530
parents b632446ba138
children 671852e35ced
files src/jdk/nashorn/internal/codegen/ClassEmitter.java src/jdk/nashorn/internal/codegen/Compiler.java
diffstat 2 files changed, 23 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/codegen/ClassEmitter.java	Tue Feb 19 20:33:07 2013 +0530
+++ b/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/src/jdk/nashorn/internal/codegen/Compiler.java	Tue Feb 19 20:33:07 2013 +0530
+++ b/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);
             }
         }