Class Environment

  • All Implemented Interfaces:
    Constants, RuntimeConstants
    Direct Known Subclasses:
    BatchEnvironment, ContextEnvironment, ImportEnvironment

    public class Environment
    extends java.lang.Object
    implements Constants
    This class defines the environment for a compilation. It is used to load classes, resolve class names and report errors. It is an abstract class, a subclass must define implementations for some of the functions.

    An environment has a source object associated with it. This is the thing against which errors are reported, it is usually a file name, a field or a class.

    Environments can be nested to change the source object.

    WARNING: The contents of this source file are not part of any supported API. Code that depends on them does so at its own risk: they are subject to change or removal without notice.

    • Field Detail

      • env

        Environment env
        The actual environment to which everything is forwarded.
      • encoding

        java.lang.String encoding
        External character encoding name
      • source

        java.lang.Object source
        The object that is currently being parsed/compiled. It is either a file name (String) or a field (MemberDefinition) or a class (ClassDeclaration or ClassDefinition).
      • debugging

        private static boolean debugging
      • dependtrace

        private static boolean dependtrace
        Debug tracing. Currently, this code is used only for tracing the loading and checking of classes, particularly the demand-driven aspects. This code should probably be integrated with 'debugOutput' above, but we need to give more thought to the issue of classifying debugging messages and allowing those only those of interest to be enabled. Calls to these methods are generally conditioned on the final variable 'Constants.tracing', which allows the calls to be completely omitted in a production release to avoid space and time overhead.
      • dumpmodifiers

        private static boolean dumpmodifiers
        Enable diagnostic dump of class modifier bits, including those in InnerClasses attributes, as they are written to the classfile. In the future, may also enable dumping field and method modifiers.
    • Constructor Detail

      • Environment

        public Environment​(Environment env,
                           java.lang.Object source)
      • Environment

        public Environment()
    • Method Detail

      • isExemptPackage

        public boolean isExemptPackage​(Identifier id)
        Tells whether an Identifier refers to a package which should be exempt from the "exists" check in Imports#resolve().
      • getClassDeclaration

        public ClassDeclaration getClassDeclaration​(Identifier nm)
        Return a class declaration given a fully qualified class name.
      • getClassDefinition

        public final ClassDefinition getClassDefinition​(Identifier nm)
                                                 throws ClassNotFound
        Return a class definition given a fully qualified class name.

        Should be called only with 'internal' class names, i.e., the result of a call to 'resolveName' or a synthetic class name.

        Throws:
        ClassNotFound
      • getClassDeclaration

        public ClassDeclaration getClassDeclaration​(Type t)
        Return a class declaration given a type. Only works for class types.
      • classExists

        public boolean classExists​(Identifier nm)
        Check if a class exists (without actually loading it). (Since inner classes cannot in general be examined without loading source, this method does not accept inner names.)
      • classExists

        public final boolean classExists​(Type t)
      • getPackage

        public Package getPackage​(Identifier pkg)
                           throws java.io.IOException
        Get the package path for a package
        Throws:
        java.io.IOException
      • loadDefinition

        public void loadDefinition​(ClassDeclaration c)
        Load the definition of a class.
      • getSource

        public final java.lang.Object getSource()
        Return the source of the environment (ie: the thing being compiled/parsed).
      • resolve

        public boolean resolve​(long where,
                               ClassDefinition c,
                               Type t)
        Resolve a type. Make sure that all the classes referred to by the type have a definition. Report errors. Return true if the type is well-formed. Presently used for types appearing in member declarations, which represent named types internally as qualified identifiers. Type names appearing in local variable declarations and within expressions are represented as identifier or field expressions, and are resolved by 'toType', which delegates handling of the non-inner portion of the name to this method.

        In 'toType', the various stages of qualification are represented by separate AST nodes. Here, we are given a single identifier which contains the entire qualification structure. It is not possible in general to set the error location to the exact position of a component that is in error, so an error message must refer to the entire qualified name. An attempt to keep track of the string length of the components of the name and to offset the location accordingly fails because the initial prefix of the name may have been rewritten by an earlier call to 'resolveName'. See 'SourceMember.resolveTypeStructure'. The situation is actually even worse than this, because only a single location is passed in for an entire declaration, which may contain many type names. All error messages are thus poorly localized. These checks should be done while traversing the parse tree for the type, not the type descriptor.

        DESIGN NOTE: As far as I can tell, the two-stage resolution of names represented in string form is an artifact of the late implementation of inner classes and the use of mangled names internally within the compiler. All qualified names should have their hiearchical structure made explicit in the parse tree at the phase at which they are presented for static semantic checking. This would affect class names appearing in 'extends', 'implements', and 'throws' clauses, as well as in member declarations.

      • resolveByName

        public boolean resolveByName​(long where,
                                     ClassDefinition c,
                                     Identifier nm)
        Given its fully-qualified name, verify that a class is defined and accessible. Used to check components of qualified names in contexts where a class is expected. Like 'resolve', but is given a single type name, not a type descriptor.
      • getQualifiedClassDefinition

        public final ClassDefinition getQualifiedClassDefinition​(long where,
                                                                 Identifier nm,
                                                                 ClassDefinition ctxClass,
                                                                 boolean isExtends)
                                                          throws ClassNotFound
        Like 'getClassDefinition(env)', but check access on each component. Currently called only by 'resolve' above. It is doubtful that calls to 'getClassDefinition(env)' are appropriate now.
        Throws:
        ClassNotFound
      • resolveNames

        public Type resolveNames​(ClassDefinition c,
                                 Type t,
                                 boolean synth)
        Resolve the names within a type, returning the adjusted type. Adjust class names to reflect scoping. Do not report errors.

        NOTE: It would be convenient to check for errors here, such as verifying that each component of a qualified name exists and is accessible. Why must this be done in a separate phase?

        If the 'synth' argument is true, indicating that the member whose type is being resolved is synthetic, names are resolved with respect to the package scope. (Fix for 4097882)

      • resolveName

        public Identifier resolveName​(Identifier name)
        Resolve a class name, using only package and import directives. Report no errors.

      • resolvePackageQualifiedName

        public final Identifier resolvePackageQualifiedName​(Identifier name)
        Discover if name consists of a package prefix, followed by the name of a class (that actually exists), followed possibly by some inner class names. If we can't find a class that exists, return the name unchanged.

        This routine is used after a class name fails to be resolved by means of imports or inner classes. However, import processing uses this routine directly, since import names must be exactly qualified to start with.

      • getImports

        public Imports getImports()
        Get the imports used to resolve class names.
      • isMoreSpecific

        public boolean isMoreSpecific​(Type from,
                                      Type to)
                               throws ClassNotFound
        Returns true if "from" is a more specific type than "to"
        Throws:
        ClassNotFound
      • implicitCast

        public boolean implicitCast​(Type from,
                                    Type to)
                             throws ClassNotFound
        Return true if an implicit cast from this type to the given type is allowed.
        Throws:
        ClassNotFound
      • explicitCast

        public boolean explicitCast​(Type from,
                                    Type to)
                             throws ClassNotFound
        Return true if an explicit cast from this type to the given type is allowed.
        Throws:
        ClassNotFound
      • getFlags

        public int getFlags()
        Flags.
      • debug_lines

        public final boolean debug_lines()
        Debugging flags. There used to be a method debug() that has been replaced because -g has changed meaning (it now cooperates with -O and line number, variable range and source file info can be toggled separately).
      • debug_vars

        public final boolean debug_vars()
      • debug_source

        public final boolean debug_source()
      • opt

        public final boolean opt()
        Optimization flags. There used to be a method optimize() that has been replaced because -O has changed meaning in javac to be replaced with -O and -O:interclass.
      • opt_interclass

        public final boolean opt_interclass()
      • verbose

        public final boolean verbose()
        Verbose
      • dump

        public final boolean dump()
        Dump debugging stuff
      • warnings

        public final boolean warnings()
        Verbose
      • dependencies

        public final boolean dependencies()
        Dependencies
      • print_dependencies

        public final boolean print_dependencies()
        Print Dependencies to stdout
      • deprecation

        public final boolean deprecation()
        Deprecation warnings are enabled.
      • version12

        public final boolean version12()
        Do not support virtual machines before version 1.2. This option is not supported and is only here for testing purposes.
      • strictdefault

        public final boolean strictdefault()
        Floating point is strict by default
      • shutdown

        public void shutdown()
        Release resources, if any.
      • error

        public void error​(java.lang.Object source,
                          long where,
                          java.lang.String err,
                          java.lang.Object arg1,
                          java.lang.Object arg2,
                          java.lang.Object arg3)
        Issue an error. source - the input source, usually a file name string offset - the offset in the source of the error err - the error number (as defined in this interface) arg1 - an optional argument to the error (null if not applicable) arg2 - a second optional argument to the error (null if not applicable) arg3 - a third optional argument to the error (null if not applicable)
      • error

        public final void error​(long where,
                                java.lang.String err,
                                java.lang.Object arg1,
                                java.lang.Object arg2,
                                java.lang.Object arg3)
      • error

        public final void error​(long where,
                                java.lang.String err,
                                java.lang.Object arg1,
                                java.lang.Object arg2)
      • error

        public final void error​(long where,
                                java.lang.String err,
                                java.lang.Object arg1)
      • error

        public final void error​(long where,
                                java.lang.String err)
      • output

        public void output​(java.lang.String msg)
        Output a string. This can either be an error message or something for debugging. This should be used instead of println.
      • debugOutput

        public static void debugOutput​(java.lang.Object msg)
      • setCharacterEncoding

        public void setCharacterEncoding​(java.lang.String encoding)
        set character encoding name
      • getCharacterEncoding

        public java.lang.String getCharacterEncoding()
        Return character encoding name
      • getMajorVersion

        public short getMajorVersion()
        Return major version to use in generated class files.
      • getMinorVersion

        public short getMinorVersion()
        Return minor version to use in generated class files.
      • coverage

        public final boolean coverage()
        get coverage flag
      • covdata

        public final boolean covdata()
        get flag of generation the coverage data file
      • getcovFile

        public java.io.File getcovFile()
        Return the coverage data file
      • dtEnter

        public void dtEnter​(java.lang.String s)
      • dtExit

        public void dtExit​(java.lang.String s)
      • dtEvent

        public void dtEvent​(java.lang.String s)
      • dumpModifiers

        public boolean dumpModifiers()