ClassReader.java revision 399
4278N/A * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 1879N/A/** This class provides operations to read a classfile into an internal 4299N/A * representation. The internal representation is anchored in a 1879N/A * ClassSymbol which contains in its scope symbol representations 1879N/A * for all other definitions in the classfile. Top-level Classes themselves 1929N/A * appear as members of the scopes of PackageSymbols. 1879N/A * <p><b>This is NOT part of any API supported by Sun Microsystems. If 4340N/A * you write code that depends on this, you do so at your own risk. 1879N/A * This code and its internal interfaces are subject to change or 1879N/A * deletion without notice.</b> 1879N/A /** The context key for the class reader. */ 1879N/A /** Switch: verbose output. 2073N/A /** Switch: check class file for correct minor version, unrecognized 2073N/A /** Switch: read constant pool and code sections. This switch is initially 2073N/A * set to false but can be turned on from outside. 0N/A /** Switch: read GJ signature information. 0N/A /** Switch: read varargs attribute. 0N/A /** Switch: allow annotations. 0N/A /** Switch: preserve parameter names from the variable table. 0N/A * Switch: cache completion failures unless -XDdev is used 0N/A * Switch: prefer source files instead of newer when both source 0N/A * and class are available 0N/A /** The log to use for verbose output 0N/A /** The name table. */ 0N/A /** Force a completion failure on this name 0N/A /** Factory for diagnostics 0N/A /** Can be reassigned from outside: 0N/A * the completer to be used for ".java" files. If this remains unassigned 0N/A * ".java" files will not be loaded. 0N/A /** A hashtable containing the encountered top-level and member classes, 0N/A * indexed by flat names. The table does not contain local classes. 0N/A /** A hashtable containing the encountered packages. 0N/A /** The current scope where type variables are entered. 0N/A /** The path name of the class file currently being read. 242N/A /** The class or method currently being read. 4433N/A /** The buffer containing the currently read class file. 2627N/A /** The current input pointer. 2627N/A /** The objects of the constant pool. 2627N/A /** For every constant pool entry, an index into buf where the 2627N/A * defining section of the entry is found. 2627N/A /** The major version number of the class file being read. */ 2627N/A /** The minor version number of the class file being read. */ 2627N/A /** Switch: debug output for JSR 308-related operations. 2627N/A /** Get the ClassReader instance for this invocation. */ 0N/A /** Initialize classes and packages, treating this as the definitive classreader. */ 0N/A /** Initialize classes and packages, optionally treating this as 0N/A * the definitive classreader. 20N/A /** Construct a new class reader, optionally treated as the 20N/A * definitive classreader for this invocation. 0N/A /** Add member to class unless it is synthetic. 0N/A/************************************************************************ 1010N/A ***********************************************************************/ 0N/A ?
"bad.source.file.header" :
"bad.class.file.header");
0N/A/************************************************************************ 0N/A ***********************************************************************/ 0N/A /** Read a character. 0N/A /** Read an integer. 1123N/A /** Extract a character at position bp from buf. 0N/A /** Extract an integer at position bp from buf. 0N/A /** Extract a long integer at position bp from buf. 0N/A /** Extract a float at position bp from buf. 0N/A /** Extract a double at position bp from buf. 0N/A/************************************************************************ 0N/A * Constant Pool Access 0N/A ***********************************************************************/ 509N/A /** Index all constant pool entries, writing their start addresses into 0N/A /** Read constant pool entry at start address i, use pool as a cache. 0N/A // FIXME: (footprint) do not use toString here 0N/A /** Read signature and convert to type. 0N/A /** If name is an array type or class signature, return the 0N/A * corresponding type; otherwise return a ClassSymbol with given name. 0N/A // by the above assertion, the following test can be 0N/A // simplified to (buf[start] == '[') 0N/A /** Read signature and convert to type parameters. 0N/A /** Read class entry. 0N/A/************************************************************************ 0N/A ***********************************************************************/ 0N/A /** The unread portion of the currently read type is 0N/A * signature[sigp..siglimit-1]. 0N/A /** Convert signature to type, where signature is a byte array segment. 0N/A /** Convert signature to type, where signature is implicit. 0N/A // int oldsigp = sigp; 0N/A "(please recompile from source)");
0N/A System.err.println(" decoded " + 0N/A new String(signature, oldsigp, sigp-oldsigp) + 0N/A " => " + t + " outer " + t.outer()); 0N/A /** Convert class signature to type, where signature is implicit. 199N/A case '<':
// generic arguments 0N/A // support old-style GJC signatures 0N/A // The signature produced was 0N/A /** Convert (implicit) signature to list of types 0N/A * until `terminator' is encountered. 0N/A /** Convert signature to type parameters, where signature is a byte 0N/A /** Convert signature to type parameters, where signature is implicit. 0N/A /** Convert (implicit) signature to type parameter. 0N/A /** Find type variable with given name in `typevars' scope. 0N/A // While reading the class attribute, the supertypes 0N/A // might refer to a type variable from an enclosing element 0N/A // (method or class). 0N/A // If the type variable is defined in the enclosing class, 0N/A // we can actually find it in 0N/A // currentOwner.owner.type.getTypeArguments() 0N/A // However, until we have read the enclosing method attribute 0N/A // we don't know for sure if this owner is correct. It could 0N/A // be a method and there is no way to tell before reading the 0N/A // enclosing method attribute. 0N/A // System.err.println("Missing type var " + name); 0N/A/************************************************************************ 0N/A * Reading Attributes 0N/A ***********************************************************************/ 0N/A // Ignore ConstantValue attribute if field not final. 0N/A // pick up parameter names from the variable table 0N/A // bridge methods are visible when generics not enabled 0N/A // standard v49 attributes 0N/A //- System.err.println(" # " + sym.type); 0N/A // v49 annotation attributes 0N/A // additional "legacy" v49 attributes, superceded by flags 0N/A // The following attributes for a Code attribute are not currently handled 0N/A // SourceDebugExtension 0N/A // LocalVariableTypeTable 0N/A /** Report unrecognized attribute. 0N/A // sym is a nested class with an "Enclosing Method" attribute 0N/A // remove sym from it's current owners scope and place it in 0N/A // the scope specified by the attribute 0N/A // See java.lang.Class 0N/A // not a constructor 0N/A // no enclosing instance 0N/A // A constructor of an inner class. 0N/A // Remove the first argument (the enclosing instance) 0N/A // Try searching again 0N/A /** Similar to Types.isSameType but avoids completion */ 0N/A * Character.isDigit answers <tt>true</tt> to some non-ascii 0N/A * digits. This one does not. <b>copied from java.lang.Class</b> 0N/A return '0' <= c && c <=
'9';
0N/A /** Read member attributes. 0N/A for (
int i =
0; i <
ac; i++) {
0N/A /** Read class attributes. 0N/A /** Read code block. 0N/A/************************************************************************ 0N/A * Reading Java-language annotations 0N/A ***********************************************************************/ 0N/A /** Attach annotations. 0N/A /** Attach parameter annotations. 0N/A /** Attach the default value for an annotation element. 0N/A // support preliminary jsr175-format class files 0N/A // support preliminary jsr175-format class files 0N/A // type parameter bounds 0N/A // Class extends and implements clauses 0N/A // method parameter: not specified 0N/A // method type argument: wasn't specified 0N/A // We don't need to worry abut these 0N/A for (
int i=
0; i<n; i++)
0N/A /** A temporary proxy representing a compound attribute. 0N/A /** A temporary proxy representing a type annotation. 0N/A // also must fill in types!!!! 1887N/A // The method wasn't found: emit a warning and recover 0N/A // Construct a new method type and symbol. Use bottom 0N/A // type (typeof null) as return type because this type is 0N/A // a subtype of all reference types and can be converted 0N/A // to primitive types by unboxing. 691N/A // implement Attribute.Visitor below 691N/A // assert value.type == type; 691N/A // type.tsym.flatName() should == proxy.enumFlatName 1929N/A // implement Annotate.Annotator.enterAnnotation() 0N/A return " ClassReader annotate " +
sym.
owner +
"." +
sym +
" with " + l;
0N/A // implement Annotate.Annotator.enterAnnotation() 0N/A/************************************************************************ 0N/A ***********************************************************************/ 4340N/A // Sometimes anonymous classes don't have an outer 4340N/A // instance, however, there is no reliable way to tell so 4340N/A /** Enter type variables of this classtype and all enclosing ones in 4340N/A /** Read contents of a given class symbol `c'. Both external and internal 4340N/A * versions of an inner class are read. 4340N/A // allocate scope for members 4340N/A // prepare type variable table 4340N/A // read flags, or skip if this is an inner class 4340N/A // read own class name and check that it matches 4340N/A // class attributes must be read before class 4340N/A // skip ahead to read class attributes 0N/A // reset and read rest of classinfo 0N/A for (
int i =
0; i < n; i++) {
0N/A for (
int i =
0; i < n; i++) {
0N/A /** Read a class file. 0N/A/************************************************************************ 0N/A ***********************************************************************/ 0N/A/************************************************************************ 1010N/A ***********************************************************************/ 0N/A /** Define a new class given its name and owner. 0N/A /** Create a new toplevel or member class symbol with given name 0N/A * and owner and enter in `classes' unless already there. 4340N/A // reassign fields of classes that might have been loaded with 4340N/A * Creates a new toplevel class symbol with given flat name and 4340N/A * given class (or source) file. 4340N/A * @param flatName a fully qualified binary class name 4340N/A * @param classFile the class file or compilation unit defining 4340N/A * the class (may be {@code null}) 4340N/A * @return a newly created class symbol 4340N/A * @throws AssertionError if the class symbol already exists 242N/A /** Create a new member or toplevel class symbol with given flat name 242N/A * and enter in `classes' unless already there. 0N/A /** Completion for classes to be loaded. Before a class is loaded 0N/A * we make sure its enclosing class (if any) is loaded. 3748N/A /** complete up through the enclosing package. */ 0N/A * Tries to complete lexically enclosing classes if c looks like a 0N/A * nested class. This is similar to completeOwners but handles 0N/A * the situation when a nested class is accessed directly as it is 0N/A * possible with the Tree API or javax.lang.model.*. 3748N/A /** We can only read a single class file at a time; this 3748N/A * flag keeps track of when we are currently reading a class 3748N/A /** Fill in definition of class `c' from corresponding class or 0N/A /* Ignore any errors, as this stream may have already 0N/A * thrown a related exception which is the one that 0N/A * should be reported. 0N/A /** Static factory for CompletionFailure objects. 0N/A * In practice, only one can be used at a time, so we share one 0N/A * to reduce the expense of allocating new exception objects. 0N/A // log.warning("proc.messager", 0N/A // Log.getLocalizedString("class.file.not.found", c.flatname)); 0N/A // c.debug.printStackTrace(); 0N/A /** Load a toplevel class with given fully qualified name 0N/A * The class is entered into `classes' only if load was successful. 0N/A/************************************************************************ 0N/A ***********************************************************************/ 0N/A /** Check to see if a package exists, given its fully qualified name. 0N/A /** Make a package, given its fully qualified name. 0N/A /** Make a package, given its unqualified name and enclosing package. 513N/A /** Include class corresponding to given class file in package, 0N/A * unless (1) we already have one the same kind (.class or .java), or 2149N/A * (2) we have one of the other kind, and the given class file 1562N/A // if c.classfile == null, we are currently compiling this class 1562N/A // and no further action is necessary. 1562N/A // if (c.flags_field & seen) != 0, we have already encountered 1562N/A // a file of the same kind; again no further action is necessary. 0N/A /** Implement policy to choose to derive information from a source 1562N/A * file or a class file when both are present. May be overridden 0N/A // 6449326: policy for bad lastModifiedTime in ClassReader 0N/A //assert adate >= 0 && bdate >= 0; 0N/A * specifies types of files to be read when filling in a package symbol 0N/A * this is used to support javadoc 0N/A /** Load directory of package into members scope. 0N/A // TODO pass binaryName to includeClassFile 0N/A /** Output for "-verbose" option. 0N/A * @param key The key to look up the correct internationalized string. 0N/A * @param arg An argument for substitution into the output string. 0N/A /** Output for "-checkclassfile" option. 0N/A * @param key The key to look up the correct internationalized string. 0N/A * @param arg An argument for substitution into the output string. 0N/A * A subclass of JavaFileObject for the sourcefile attribute found in a classfile. 0N/A * The attribute is only the last component of the original filename, so is unlikely 0N/A * to be valid as is, so operations other than those to access the name throw 0N/A * UnsupportedOperationException 0N/A /** The file's name. 0N/A super(
null);
// no file manager; never referenced for this file object 0N/A /** @deprecated see bug 6410637 */ 0N/A return true;
// fail-safe mode