# HG changeset patch # User sundar # Date 1361360312 -19800 # Node ID 58eea0e8f369e226f3a9dca936526ddb0eccf6ac # Parent b632446ba1384c1d5674f7151e806dc096ed891b 8008207: Make constants array and source fields private Reviewed-by: hannesw, lagergren diff -r b632446ba138 -r 58eea0e8f369 src/jdk/nashorn/internal/codegen/ClassEmitter.java --- 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); diff -r b632446ba138 -r 58eea0e8f369 src/jdk/nashorn/internal/codegen/Compiler.java --- 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() { + @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); } }