8008207: Make constants array and source fields private

Wed, 20 Feb 2013 17:08:32 +0530

author
sundar
date
Wed, 20 Feb 2013 17:08:32 +0530
changeset 106
58eea0e8f369
parent 105
b632446ba138
child 107
671852e35ced

8008207: Make constants array and source fields private
Reviewed-by: hannesw, lagergren

src/jdk/nashorn/internal/codegen/ClassEmitter.java file | annotate | diff | comparison | revisions
src/jdk/nashorn/internal/codegen/Compiler.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/jdk/nashorn/internal/codegen/ClassEmitter.java	Tue Feb 19 20:33:07 2013 +0530
     1.2 +++ b/src/jdk/nashorn/internal/codegen/ClassEmitter.java	Wed Feb 20 17:08:32 2013 +0530
     1.3 @@ -212,11 +212,11 @@
     1.4      private void defineCommonStatics(final boolean strictMode) {
     1.5          // source - used to store the source data (text) for this script.  Shared across
     1.6          // compile units.  Set externally by the compiler.
     1.7 -        field(EnumSet.of(Flag.PUBLIC, Flag.STATIC), SOURCE.tag(), Source.class);
     1.8 +        field(EnumSet.of(Flag.PRIVATE, Flag.STATIC), SOURCE.tag(), Source.class);
     1.9  
    1.10          // constants - used to the constants array for this script.  Shared across
    1.11          // compile units.  Set externally by the compiler.
    1.12 -        field(EnumSet.of(Flag.PUBLIC, Flag.STATIC), CONSTANTS.tag(), Object[].class);
    1.13 +        field(EnumSet.of(Flag.PRIVATE, Flag.STATIC), CONSTANTS.tag(), Object[].class);
    1.14  
    1.15          // strictMode - was this script compiled in strict mode.  Set externally by the compiler.
    1.16          field(EnumSet.of(Flag.PUBLIC, Flag.STATIC, Flag.FINAL), STRICT_MODE.tag(), boolean.class, strictMode);
     2.1 --- a/src/jdk/nashorn/internal/codegen/Compiler.java	Tue Feb 19 20:33:07 2013 +0530
     2.2 +++ b/src/jdk/nashorn/internal/codegen/Compiler.java	Wed Feb 20 17:08:32 2013 +0530
     2.3 @@ -33,6 +33,10 @@
     2.4  import static jdk.nashorn.internal.codegen.CompilerConstants.THIS;
     2.5  
     2.6  import java.io.File;
     2.7 +import java.lang.reflect.Field;
     2.8 +import java.security.AccessController;
     2.9 +import java.security.PrivilegedActionException;
    2.10 +import java.security.PrivilegedExceptionAction;
    2.11  import java.util.Arrays;
    2.12  import java.util.EnumSet;
    2.13  import java.util.HashMap;
    2.14 @@ -274,10 +278,23 @@
    2.15              }
    2.16  
    2.17              try {
    2.18 -                //use reflection to write source and constants table to installed classes
    2.19 -                clazz.getField(SOURCE.tag()).set(null, getSource());
    2.20 -                clazz.getField(CONSTANTS.tag()).set(null, getConstantData().toArray());
    2.21 -            } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
    2.22 +                final Source source = getSource();
    2.23 +                final Object[] constants = getConstantData().toArray();
    2.24 +                // Need doPrivileged because these fields are private
    2.25 +                AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
    2.26 +                    @Override
    2.27 +                    public Void run() throws Exception {
    2.28 +                        //use reflection to write source and constants table to installed classes
    2.29 +                        final Field sourceField = clazz.getDeclaredField(SOURCE.tag());
    2.30 +                        final Field constantsField = clazz.getDeclaredField(CONSTANTS.tag());
    2.31 +                        sourceField.setAccessible(true);
    2.32 +                        constantsField.setAccessible(true);
    2.33 +                        sourceField.set(null, source);
    2.34 +                        constantsField.set(null, constants);
    2.35 +                        return null;
    2.36 +                    }
    2.37 +                });
    2.38 +            } catch (final PrivilegedActionException e) {
    2.39                  throw new RuntimeException(e);
    2.40              }
    2.41          }

mercurial