View Javadoc
1   /*
2    * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
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  /**
29   * Used to track split class compilation.
30   */
31  public class CompileUnit implements Comparable<CompileUnit> {
32      /** Current class name */
33      private final String className;
34  
35      /** Current class generator */
36      private ClassEmitter classEmitter;
37  
38      private long weight;
39  
40      private Class<?> clazz;
41  
42      CompileUnit(final String className, final ClassEmitter classEmitter) {
43          this(className, classEmitter, 0L);
44      }
45  
46      CompileUnit(final String className, final ClassEmitter classEmitter, final long initialWeight) {
47          this.className    = className;
48          this.classEmitter = classEmitter;
49          this.weight       = initialWeight;
50      }
51  
52      /**
53       * Return the class that contains the code for this unit, null if not
54       * generated yet
55       *
56       * @return class with compile unit code
57       */
58      public Class<?> getCode() {
59          return clazz;
60      }
61  
62      /**
63       * Set class when it exists. Only accessible from compiler
64       * @param clazz class with code for this compile unit
65       */
66      void setCode(final Class<?> clazz) {
67          clazz.getClass(); // null check
68          this.clazz = clazz;
69          // Revisit this - refactor to avoid null-ed out non-final fields
70          // null out emitter
71          this.classEmitter = null;
72      }
73  
74      /**
75       * Add weight to this compile unit
76       * @param w weight to add
77       */
78      void addWeight(final long w) {
79          this.weight += w;
80      }
81  
82      /**
83       * Get the current weight of the compile unit.
84       * @return the unit's weight
85       */
86      long getWeight() {
87          return weight;
88      }
89  
90      /**
91       * Check if this compile unit can hold {@code weight} more units of weight
92       * @param w weight to check if can be added
93       * @return true if weight fits in this compile unit
94       */
95      public boolean canHold(final long w) {
96          return (this.weight + w) < Splitter.SPLIT_THRESHOLD;
97      }
98  
99      /**
100      * Get the class emitter for this compile unit
101      * @return class emitter
102      */
103     public ClassEmitter getClassEmitter() {
104         return classEmitter;
105     }
106 
107     /**
108      * Get the class name for this compile unit
109      * @return the class name
110      */
111     public String getUnitClassName() {
112         return className;
113     }
114 
115     @Override
116     public String toString() {
117         return "[classname=" + className + " weight=" + weight + '/' + Splitter.SPLIT_THRESHOLD + ']';
118     }
119 
120     @Override
121     public int compareTo(CompileUnit o) {
122         return className.compareTo(o.className);
123     }
124 }