src/jdk/nashorn/internal/codegen/CompileUnit.java

Print this page
rev 1199 : 8072595: nashorn should not use obj.getClass() for null checks
Reviewed-by: hannesw, attila


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.codegen;
  27 
  28 import java.io.Serializable;

  29 import java.util.Set;
  30 import java.util.TreeSet;
  31 import jdk.nashorn.internal.ir.CompileUnitHolder;
  32 
  33 /**
  34   * Used to track split class compilation. Note that instances of the class are serializable, but all fields are
  35   * transient, making the serialized version of the class only useful for tracking the referential topology of other
  36   * AST nodes referencing the same or different compile units. We do want to preserve this topology though as
  37   * {@link CompileUnitHolder}s in a deserialized AST will undergo reinitialization.
  38   */
  39 public final class CompileUnit implements Comparable<CompileUnit>, Serializable {
  40     private static final long serialVersionUID = 1L;
  41 
  42     /** Current class name */
  43     private transient final String className;
  44 
  45     /** Current class generator */
  46     private transient ClassEmitter classEmitter;
  47 
  48     private transient long weight;


  96      */
  97     public void setUsed() {
  98         this.isUsed = true;
  99     }
 100 
 101     /**
 102      * Return the class that contains the code for this unit, null if not
 103      * generated yet
 104      *
 105      * @return class with compile unit code
 106      */
 107     public Class<?> getCode() {
 108         return clazz;
 109     }
 110 
 111     /**
 112      * Set class when it exists. Only accessible from compiler
 113      * @param clazz class with code for this compile unit
 114      */
 115     void setCode(final Class<?> clazz) {
 116         clazz.getClass(); // null check
 117         this.clazz = clazz;
 118         // Revisit this - refactor to avoid null-ed out non-final fields
 119         // null out emitter
 120         this.classEmitter = null;
 121     }
 122 
 123     /**
 124      * Add weight to this compile unit
 125      * @param w weight to add
 126      */
 127     void addWeight(final long w) {
 128         this.weight += w;
 129     }
 130 
 131     /**
 132      * Check if this compile unit can hold {@code weight} more units of weight
 133      * @param w weight to check if can be added
 134      * @return true if weight fits in this compile unit
 135      */
 136     public boolean canHold(final long w) {




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nashorn.internal.codegen;
  27 
  28 import java.io.Serializable;
  29 import java.util.Objects;
  30 import java.util.Set;
  31 import java.util.TreeSet;
  32 import jdk.nashorn.internal.ir.CompileUnitHolder;
  33 
  34 /**
  35   * Used to track split class compilation. Note that instances of the class are serializable, but all fields are
  36   * transient, making the serialized version of the class only useful for tracking the referential topology of other
  37   * AST nodes referencing the same or different compile units. We do want to preserve this topology though as
  38   * {@link CompileUnitHolder}s in a deserialized AST will undergo reinitialization.
  39   */
  40 public final class CompileUnit implements Comparable<CompileUnit>, Serializable {
  41     private static final long serialVersionUID = 1L;
  42 
  43     /** Current class name */
  44     private transient final String className;
  45 
  46     /** Current class generator */
  47     private transient ClassEmitter classEmitter;
  48 
  49     private transient long weight;


  97      */
  98     public void setUsed() {
  99         this.isUsed = true;
 100     }
 101 
 102     /**
 103      * Return the class that contains the code for this unit, null if not
 104      * generated yet
 105      *
 106      * @return class with compile unit code
 107      */
 108     public Class<?> getCode() {
 109         return clazz;
 110     }
 111 
 112     /**
 113      * Set class when it exists. Only accessible from compiler
 114      * @param clazz class with code for this compile unit
 115      */
 116     void setCode(final Class<?> clazz) {
 117         Objects.requireNonNull(clazz);
 118         this.clazz = clazz;
 119         // Revisit this - refactor to avoid null-ed out non-final fields
 120         // null out emitter
 121         this.classEmitter = null;
 122     }
 123 
 124     /**
 125      * Add weight to this compile unit
 126      * @param w weight to add
 127      */
 128     void addWeight(final long w) {
 129         this.weight += w;
 130     }
 131 
 132     /**
 133      * Check if this compile unit can hold {@code weight} more units of weight
 134      * @param w weight to check if can be added
 135      * @return true if weight fits in this compile unit
 136      */
 137     public boolean canHold(final long w) {