aoqi@0: /*
darcy@2707: * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.javac.jvm;
aoqi@0:
aoqi@0: import java.io.*;
aoqi@0: import java.net.URI;
aoqi@0: import java.net.URISyntaxException;
aoqi@0: import java.nio.CharBuffer;
aoqi@0: import java.util.Arrays;
aoqi@0: import java.util.EnumSet;
aoqi@0: import java.util.HashMap;
aoqi@0: import java.util.HashSet;
aoqi@0: import java.util.Map;
aoqi@0: import java.util.Set;
aoqi@0: import javax.lang.model.SourceVersion;
aoqi@0: import javax.tools.JavaFileObject;
aoqi@0: import javax.tools.JavaFileManager;
aoqi@0: import javax.tools.JavaFileManager.Location;
aoqi@0: import javax.tools.StandardJavaFileManager;
aoqi@0:
aoqi@0: import static javax.tools.StandardLocation.*;
aoqi@0:
aoqi@0: import com.sun.tools.javac.comp.Annotate;
aoqi@0: import com.sun.tools.javac.code.*;
aoqi@0: import com.sun.tools.javac.code.Lint.LintCategory;
aoqi@0: import com.sun.tools.javac.code.Type.*;
aoqi@0: import com.sun.tools.javac.code.Symbol.*;
aoqi@0: import com.sun.tools.javac.code.Symtab;
aoqi@0: import com.sun.tools.javac.file.BaseFileObject;
aoqi@0: import com.sun.tools.javac.util.*;
aoqi@0: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
aoqi@0:
aoqi@0: import static com.sun.tools.javac.code.Flags.*;
aoqi@0: import static com.sun.tools.javac.code.Kinds.*;
aoqi@0: import static com.sun.tools.javac.code.TypeTag.CLASS;
aoqi@0: import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
aoqi@0: import static com.sun.tools.javac.jvm.ClassFile.*;
aoqi@0: import static com.sun.tools.javac.jvm.ClassFile.Version.*;
aoqi@0:
aoqi@0: import static com.sun.tools.javac.main.Option.*;
aoqi@0:
aoqi@0: /** This class provides operations to read a classfile into an internal
aoqi@0: * representation. The internal representation is anchored in a
aoqi@0: * ClassSymbol which contains in its scope symbol representations
aoqi@0: * for all other definitions in the classfile. Top-level Classes themselves
aoqi@0: * appear as members of the scopes of PackageSymbols.
aoqi@0: *
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own risk.
aoqi@0: * This code and its internal interfaces are subject to change or
aoqi@0: * deletion without notice.
aoqi@0: */
aoqi@0: public class ClassReader {
aoqi@0: /** The context key for the class reader. */
aoqi@0: protected static final Context.Key classReaderKey =
aoqi@0: new Context.Key();
aoqi@0:
aoqi@0: public static final int INITIAL_BUFFER_SIZE = 0x0fff0;
aoqi@0:
aoqi@0: Annotate annotate;
aoqi@0:
aoqi@0: /** Switch: verbose output.
aoqi@0: */
aoqi@0: boolean verbose;
aoqi@0:
aoqi@0: /** Switch: check class file for correct minor version, unrecognized
aoqi@0: * attributes.
aoqi@0: */
aoqi@0: boolean checkClassFile;
aoqi@0:
aoqi@0: /** Switch: read constant pool and code sections. This switch is initially
aoqi@0: * set to false but can be turned on from outside.
aoqi@0: */
aoqi@0: public boolean readAllOfClassFile = false;
aoqi@0:
aoqi@0: /** Switch: read GJ signature information.
aoqi@0: */
aoqi@0: boolean allowGenerics;
aoqi@0:
aoqi@0: /** Switch: read varargs attribute.
aoqi@0: */
aoqi@0: boolean allowVarargs;
aoqi@0:
aoqi@0: /** Switch: allow annotations.
aoqi@0: */
aoqi@0: boolean allowAnnotations;
aoqi@0:
aoqi@0: /** Switch: allow simplified varargs.
aoqi@0: */
aoqi@0: boolean allowSimplifiedVarargs;
aoqi@0:
aoqi@0: /** Lint option: warn about classfile issues
aoqi@0: */
aoqi@0: boolean lintClassfile;
aoqi@0:
aoqi@0: /** Switch: preserve parameter names from the variable table.
aoqi@0: */
aoqi@0: public boolean saveParameterNames;
aoqi@0:
aoqi@0: /**
aoqi@0: * Switch: cache completion failures unless -XDdev is used
aoqi@0: */
aoqi@0: private boolean cacheCompletionFailure;
aoqi@0:
aoqi@0: /**
aoqi@0: * Switch: prefer source files instead of newer when both source
aoqi@0: * and class are available
aoqi@0: **/
aoqi@0: public boolean preferSource;
aoqi@0:
aoqi@0: /**
aoqi@0: * The currently selected profile.
aoqi@0: */
aoqi@0: public final Profile profile;
aoqi@0:
aoqi@0: /** The log to use for verbose output
aoqi@0: */
aoqi@0: final Log log;
aoqi@0:
aoqi@0: /** The symbol table. */
aoqi@0: Symtab syms;
aoqi@0:
aoqi@0: Types types;
aoqi@0:
aoqi@0: /** The name table. */
aoqi@0: final Names names;
aoqi@0:
aoqi@0: /** Force a completion failure on this name
aoqi@0: */
aoqi@0: final Name completionFailureName;
aoqi@0:
aoqi@0: /** Access to files
aoqi@0: */
aoqi@0: private final JavaFileManager fileManager;
aoqi@0:
aoqi@0: /** Factory for diagnostics
aoqi@0: */
aoqi@0: JCDiagnostic.Factory diagFactory;
aoqi@0:
aoqi@0: /** Can be reassigned from outside:
aoqi@0: * the completer to be used for ".java" files. If this remains unassigned
aoqi@0: * ".java" files will not be loaded.
aoqi@0: */
aoqi@0: public SourceCompleter sourceCompleter = null;
aoqi@0:
aoqi@0: /** A hashtable containing the encountered top-level and member classes,
aoqi@0: * indexed by flat names. The table does not contain local classes.
aoqi@0: */
aoqi@0: private Map classes;
aoqi@0:
aoqi@0: /** A hashtable containing the encountered packages.
aoqi@0: */
aoqi@0: private Map packages;
aoqi@0:
aoqi@0: /** The current scope where type variables are entered.
aoqi@0: */
aoqi@0: protected Scope typevars;
aoqi@0:
aoqi@0: /** The path name of the class file currently being read.
aoqi@0: */
aoqi@0: protected JavaFileObject currentClassFile = null;
aoqi@0:
aoqi@0: /** The class or method currently being read.
aoqi@0: */
aoqi@0: protected Symbol currentOwner = null;
aoqi@0:
aoqi@0: /** The buffer containing the currently read class file.
aoqi@0: */
aoqi@0: byte[] buf = new byte[INITIAL_BUFFER_SIZE];
aoqi@0:
aoqi@0: /** The current input pointer.
aoqi@0: */
aoqi@0: protected int bp;
aoqi@0:
aoqi@0: /** The objects of the constant pool.
aoqi@0: */
aoqi@0: Object[] poolObj;
aoqi@0:
aoqi@0: /** For every constant pool entry, an index into buf where the
aoqi@0: * defining section of the entry is found.
aoqi@0: */
aoqi@0: int[] poolIdx;
aoqi@0:
aoqi@0: /** The major version number of the class file being read. */
aoqi@0: int majorVersion;
aoqi@0: /** The minor version number of the class file being read. */
aoqi@0: int minorVersion;
aoqi@0:
aoqi@0: /** A table to hold the constant pool indices for method parameter
aoqi@0: * names, as given in LocalVariableTable attributes.
aoqi@0: */
aoqi@0: int[] parameterNameIndices;
aoqi@0:
aoqi@0: /**
aoqi@0: * Whether or not any parameter names have been found.
aoqi@0: */
aoqi@0: boolean haveParameterNameIndices;
aoqi@0:
aoqi@0: /** Set this to false every time we start reading a method
aoqi@0: * and are saving parameter names. Set it to true when we see
aoqi@0: * MethodParameters, if it's set when we see a LocalVariableTable,
aoqi@0: * then we ignore the parameter names from the LVT.
aoqi@0: */
aoqi@0: boolean sawMethodParameters;
aoqi@0:
aoqi@0: /**
aoqi@0: * The set of attribute names for which warnings have been generated for the current class
aoqi@0: */
aoqi@0: Set warnedAttrs = new HashSet();
aoqi@0:
aoqi@0: /**
aoqi@0: * Completer that delegates to the complete-method of this class.
aoqi@0: */
aoqi@0: private final Completer thisCompleter = new Completer() {
aoqi@0: @Override
aoqi@0: public void complete(Symbol sym) throws CompletionFailure {
aoqi@0: ClassReader.this.complete(sym);
aoqi@0: }
aoqi@0: };
aoqi@0:
aoqi@0:
aoqi@0: /** Get the ClassReader instance for this invocation. */
aoqi@0: public static ClassReader instance(Context context) {
aoqi@0: ClassReader instance = context.get(classReaderKey);
aoqi@0: if (instance == null)
aoqi@0: instance = new ClassReader(context, true);
aoqi@0: return instance;
aoqi@0: }
aoqi@0:
aoqi@0: /** Initialize classes and packages, treating this as the definitive classreader. */
aoqi@0: public void init(Symtab syms) {
aoqi@0: init(syms, true);
aoqi@0: }
aoqi@0:
aoqi@0: /** Initialize classes and packages, optionally treating this as
aoqi@0: * the definitive classreader.
aoqi@0: */
aoqi@0: private void init(Symtab syms, boolean definitive) {
aoqi@0: if (classes != null) return;
aoqi@0:
aoqi@0: if (definitive) {
aoqi@0: Assert.check(packages == null || packages == syms.packages);
aoqi@0: packages = syms.packages;
aoqi@0: Assert.check(classes == null || classes == syms.classes);
aoqi@0: classes = syms.classes;
aoqi@0: } else {
aoqi@0: packages = new HashMap();
aoqi@0: classes = new HashMap();
aoqi@0: }
aoqi@0:
aoqi@0: packages.put(names.empty, syms.rootPackage);
aoqi@0: syms.rootPackage.completer = thisCompleter;
aoqi@0: syms.unnamedPackage.completer = thisCompleter;
aoqi@0: }
aoqi@0:
aoqi@0: /** Construct a new class reader, optionally treated as the
aoqi@0: * definitive classreader for this invocation.
aoqi@0: */
aoqi@0: protected ClassReader(Context context, boolean definitive) {
aoqi@0: if (definitive) context.put(classReaderKey, this);
aoqi@0:
aoqi@0: names = Names.instance(context);
aoqi@0: syms = Symtab.instance(context);
aoqi@0: types = Types.instance(context);
aoqi@0: fileManager = context.get(JavaFileManager.class);
aoqi@0: if (fileManager == null)
aoqi@0: throw new AssertionError("FileManager initialization error");
aoqi@0: diagFactory = JCDiagnostic.Factory.instance(context);
aoqi@0:
aoqi@0: init(syms, definitive);
aoqi@0: log = Log.instance(context);
aoqi@0:
aoqi@0: Options options = Options.instance(context);
aoqi@0: annotate = Annotate.instance(context);
aoqi@0: verbose = options.isSet(VERBOSE);
aoqi@0: checkClassFile = options.isSet("-checkclassfile");
aoqi@0:
aoqi@0: Source source = Source.instance(context);
aoqi@0: allowGenerics = source.allowGenerics();
aoqi@0: allowVarargs = source.allowVarargs();
aoqi@0: allowAnnotations = source.allowAnnotations();
aoqi@0: allowSimplifiedVarargs = source.allowSimplifiedVarargs();
aoqi@0:
aoqi@0: saveParameterNames = options.isSet("save-parameter-names");
aoqi@0: cacheCompletionFailure = options.isUnset("dev");
aoqi@0: preferSource = "source".equals(options.get("-Xprefer"));
aoqi@0:
aoqi@0: profile = Profile.instance(context);
aoqi@0:
aoqi@0: completionFailureName =
aoqi@0: options.isSet("failcomplete")
aoqi@0: ? names.fromString(options.get("failcomplete"))
aoqi@0: : null;
aoqi@0:
aoqi@0: typevars = new Scope(syms.noSymbol);
aoqi@0:
aoqi@0: lintClassfile = Lint.instance(context).isEnabled(LintCategory.CLASSFILE);
aoqi@0:
aoqi@0: initAttributeReaders();
aoqi@0: }
aoqi@0:
aoqi@0: /** Add member to class unless it is synthetic.
aoqi@0: */
aoqi@0: private void enterMember(ClassSymbol c, Symbol sym) {
aoqi@0: // Synthetic members are not entered -- reason lost to history (optimization?).
aoqi@0: // Lambda methods must be entered because they may have inner classes (which reference them)
aoqi@0: if ((sym.flags_field & (SYNTHETIC|BRIDGE)) != SYNTHETIC || sym.name.startsWith(names.lambda))
aoqi@0: c.members_field.enter(sym);
aoqi@0: }
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Error Diagnoses
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0:
aoqi@0: public class BadClassFile extends CompletionFailure {
aoqi@0: private static final long serialVersionUID = 0;
aoqi@0:
aoqi@0: public BadClassFile(TypeSymbol sym, JavaFileObject file, JCDiagnostic diag) {
aoqi@0: super(sym, createBadClassFileDiagnostic(file, diag));
aoqi@0: }
aoqi@0: }
aoqi@0: // where
aoqi@0: private JCDiagnostic createBadClassFileDiagnostic(JavaFileObject file, JCDiagnostic diag) {
aoqi@0: String key = (file.getKind() == JavaFileObject.Kind.SOURCE
aoqi@0: ? "bad.source.file.header" : "bad.class.file.header");
aoqi@0: return diagFactory.fragment(key, file, diag);
aoqi@0: }
aoqi@0:
aoqi@0: public BadClassFile badClassFile(String key, Object... args) {
aoqi@0: return new BadClassFile (
aoqi@0: currentOwner.enclClass(),
aoqi@0: currentClassFile,
aoqi@0: diagFactory.fragment(key, args));
aoqi@0: }
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Buffer Access
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0: /** Read a character.
aoqi@0: */
aoqi@0: char nextChar() {
aoqi@0: return (char)(((buf[bp++] & 0xFF) << 8) + (buf[bp++] & 0xFF));
aoqi@0: }
aoqi@0:
aoqi@0: /** Read a byte.
aoqi@0: */
aoqi@0: int nextByte() {
aoqi@0: return buf[bp++] & 0xFF;
aoqi@0: }
aoqi@0:
aoqi@0: /** Read an integer.
aoqi@0: */
aoqi@0: int nextInt() {
aoqi@0: return
aoqi@0: ((buf[bp++] & 0xFF) << 24) +
aoqi@0: ((buf[bp++] & 0xFF) << 16) +
aoqi@0: ((buf[bp++] & 0xFF) << 8) +
aoqi@0: (buf[bp++] & 0xFF);
aoqi@0: }
aoqi@0:
aoqi@0: /** Extract a character at position bp from buf.
aoqi@0: */
aoqi@0: char getChar(int bp) {
aoqi@0: return
aoqi@0: (char)(((buf[bp] & 0xFF) << 8) + (buf[bp+1] & 0xFF));
aoqi@0: }
aoqi@0:
aoqi@0: /** Extract an integer at position bp from buf.
aoqi@0: */
aoqi@0: int getInt(int bp) {
aoqi@0: return
aoqi@0: ((buf[bp] & 0xFF) << 24) +
aoqi@0: ((buf[bp+1] & 0xFF) << 16) +
aoqi@0: ((buf[bp+2] & 0xFF) << 8) +
aoqi@0: (buf[bp+3] & 0xFF);
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: /** Extract a long integer at position bp from buf.
aoqi@0: */
aoqi@0: long getLong(int bp) {
aoqi@0: DataInputStream bufin =
aoqi@0: new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
aoqi@0: try {
aoqi@0: return bufin.readLong();
aoqi@0: } catch (IOException e) {
aoqi@0: throw new AssertionError(e);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Extract a float at position bp from buf.
aoqi@0: */
aoqi@0: float getFloat(int bp) {
aoqi@0: DataInputStream bufin =
aoqi@0: new DataInputStream(new ByteArrayInputStream(buf, bp, 4));
aoqi@0: try {
aoqi@0: return bufin.readFloat();
aoqi@0: } catch (IOException e) {
aoqi@0: throw new AssertionError(e);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Extract a double at position bp from buf.
aoqi@0: */
aoqi@0: double getDouble(int bp) {
aoqi@0: DataInputStream bufin =
aoqi@0: new DataInputStream(new ByteArrayInputStream(buf, bp, 8));
aoqi@0: try {
aoqi@0: return bufin.readDouble();
aoqi@0: } catch (IOException e) {
aoqi@0: throw new AssertionError(e);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Constant Pool Access
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0: /** Index all constant pool entries, writing their start addresses into
aoqi@0: * poolIdx.
aoqi@0: */
aoqi@0: void indexPool() {
aoqi@0: poolIdx = new int[nextChar()];
aoqi@0: poolObj = new Object[poolIdx.length];
aoqi@0: int i = 1;
aoqi@0: while (i < poolIdx.length) {
aoqi@0: poolIdx[i++] = bp;
aoqi@0: byte tag = buf[bp++];
aoqi@0: switch (tag) {
aoqi@0: case CONSTANT_Utf8: case CONSTANT_Unicode: {
aoqi@0: int len = nextChar();
aoqi@0: bp = bp + len;
aoqi@0: break;
aoqi@0: }
aoqi@0: case CONSTANT_Class:
aoqi@0: case CONSTANT_String:
aoqi@0: case CONSTANT_MethodType:
aoqi@0: bp = bp + 2;
aoqi@0: break;
aoqi@0: case CONSTANT_MethodHandle:
aoqi@0: bp = bp + 3;
aoqi@0: break;
aoqi@0: case CONSTANT_Fieldref:
aoqi@0: case CONSTANT_Methodref:
aoqi@0: case CONSTANT_InterfaceMethodref:
aoqi@0: case CONSTANT_NameandType:
aoqi@0: case CONSTANT_Integer:
aoqi@0: case CONSTANT_Float:
aoqi@0: case CONSTANT_InvokeDynamic:
aoqi@0: bp = bp + 4;
aoqi@0: break;
aoqi@0: case CONSTANT_Long:
aoqi@0: case CONSTANT_Double:
aoqi@0: bp = bp + 8;
aoqi@0: i++;
aoqi@0: break;
aoqi@0: default:
aoqi@0: throw badClassFile("bad.const.pool.tag.at",
aoqi@0: Byte.toString(tag),
aoqi@0: Integer.toString(bp -1));
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Read constant pool entry at start address i, use pool as a cache.
aoqi@0: */
aoqi@0: Object readPool(int i) {
aoqi@0: Object result = poolObj[i];
aoqi@0: if (result != null) return result;
aoqi@0:
aoqi@0: int index = poolIdx[i];
aoqi@0: if (index == 0) return null;
aoqi@0:
aoqi@0: byte tag = buf[index];
aoqi@0: switch (tag) {
aoqi@0: case CONSTANT_Utf8:
aoqi@0: poolObj[i] = names.fromUtf(buf, index + 3, getChar(index + 1));
aoqi@0: break;
aoqi@0: case CONSTANT_Unicode:
aoqi@0: throw badClassFile("unicode.str.not.supported");
aoqi@0: case CONSTANT_Class:
aoqi@0: poolObj[i] = readClassOrType(getChar(index + 1));
aoqi@0: break;
aoqi@0: case CONSTANT_String:
aoqi@0: // FIXME: (footprint) do not use toString here
aoqi@0: poolObj[i] = readName(getChar(index + 1)).toString();
aoqi@0: break;
aoqi@0: case CONSTANT_Fieldref: {
aoqi@0: ClassSymbol owner = readClassSymbol(getChar(index + 1));
aoqi@0: NameAndType nt = readNameAndType(getChar(index + 3));
aoqi@0: poolObj[i] = new VarSymbol(0, nt.name, nt.uniqueType.type, owner);
aoqi@0: break;
aoqi@0: }
aoqi@0: case CONSTANT_Methodref:
aoqi@0: case CONSTANT_InterfaceMethodref: {
aoqi@0: ClassSymbol owner = readClassSymbol(getChar(index + 1));
aoqi@0: NameAndType nt = readNameAndType(getChar(index + 3));
aoqi@0: poolObj[i] = new MethodSymbol(0, nt.name, nt.uniqueType.type, owner);
aoqi@0: break;
aoqi@0: }
aoqi@0: case CONSTANT_NameandType:
aoqi@0: poolObj[i] = new NameAndType(
aoqi@0: readName(getChar(index + 1)),
aoqi@0: readType(getChar(index + 3)), types);
aoqi@0: break;
aoqi@0: case CONSTANT_Integer:
aoqi@0: poolObj[i] = getInt(index + 1);
aoqi@0: break;
aoqi@0: case CONSTANT_Float:
aoqi@0: poolObj[i] = new Float(getFloat(index + 1));
aoqi@0: break;
aoqi@0: case CONSTANT_Long:
aoqi@0: poolObj[i] = new Long(getLong(index + 1));
aoqi@0: break;
aoqi@0: case CONSTANT_Double:
aoqi@0: poolObj[i] = new Double(getDouble(index + 1));
aoqi@0: break;
aoqi@0: case CONSTANT_MethodHandle:
aoqi@0: skipBytes(4);
aoqi@0: break;
aoqi@0: case CONSTANT_MethodType:
aoqi@0: skipBytes(3);
aoqi@0: break;
aoqi@0: case CONSTANT_InvokeDynamic:
aoqi@0: skipBytes(5);
aoqi@0: break;
aoqi@0: default:
aoqi@0: throw badClassFile("bad.const.pool.tag", Byte.toString(tag));
aoqi@0: }
aoqi@0: return poolObj[i];
aoqi@0: }
aoqi@0:
aoqi@0: /** Read signature and convert to type.
aoqi@0: */
aoqi@0: Type readType(int i) {
aoqi@0: int index = poolIdx[i];
aoqi@0: return sigToType(buf, index + 3, getChar(index + 1));
aoqi@0: }
aoqi@0:
aoqi@0: /** If name is an array type or class signature, return the
aoqi@0: * corresponding type; otherwise return a ClassSymbol with given name.
aoqi@0: */
aoqi@0: Object readClassOrType(int i) {
aoqi@0: int index = poolIdx[i];
aoqi@0: int len = getChar(index + 1);
aoqi@0: int start = index + 3;
aoqi@0: Assert.check(buf[start] == '[' || buf[start + len - 1] != ';');
aoqi@0: // by the above assertion, the following test can be
aoqi@0: // simplified to (buf[start] == '[')
aoqi@0: return (buf[start] == '[' || buf[start + len - 1] == ';')
aoqi@0: ? (Object)sigToType(buf, start, len)
aoqi@0: : (Object)enterClass(names.fromUtf(internalize(buf, start,
aoqi@0: len)));
aoqi@0: }
aoqi@0:
aoqi@0: /** Read signature and convert to type parameters.
aoqi@0: */
aoqi@0: List readTypeParams(int i) {
aoqi@0: int index = poolIdx[i];
aoqi@0: return sigToTypeParams(buf, index + 3, getChar(index + 1));
aoqi@0: }
aoqi@0:
aoqi@0: /** Read class entry.
aoqi@0: */
aoqi@0: ClassSymbol readClassSymbol(int i) {
aoqi@0: Object obj = readPool(i);
aoqi@0: if (obj != null && !(obj instanceof ClassSymbol))
aoqi@0: throw badClassFile("bad.const.pool.entry",
aoqi@0: currentClassFile.toString(),
aoqi@0: "CONSTANT_Class_info", i);
aoqi@0: return (ClassSymbol)obj;
aoqi@0: }
aoqi@0:
aoqi@0: /** Read name.
aoqi@0: */
aoqi@0: Name readName(int i) {
aoqi@0: Object obj = readPool(i);
aoqi@0: if (obj != null && !(obj instanceof Name))
aoqi@0: throw badClassFile("bad.const.pool.entry",
aoqi@0: currentClassFile.toString(),
aoqi@0: "CONSTANT_Utf8_info or CONSTANT_String_info", i);
aoqi@0: return (Name)obj;
aoqi@0: }
aoqi@0:
aoqi@0: /** Read name and type.
aoqi@0: */
aoqi@0: NameAndType readNameAndType(int i) {
aoqi@0: Object obj = readPool(i);
aoqi@0: if (obj != null && !(obj instanceof NameAndType))
aoqi@0: throw badClassFile("bad.const.pool.entry",
aoqi@0: currentClassFile.toString(),
aoqi@0: "CONSTANT_NameAndType_info", i);
aoqi@0: return (NameAndType)obj;
aoqi@0: }
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Reading Types
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0: /** The unread portion of the currently read type is
aoqi@0: * signature[sigp..siglimit-1].
aoqi@0: */
aoqi@0: byte[] signature;
aoqi@0: int sigp;
aoqi@0: int siglimit;
aoqi@0: boolean sigEnterPhase = false;
aoqi@0:
aoqi@0: /** Convert signature to type, where signature is a byte array segment.
aoqi@0: */
aoqi@0: Type sigToType(byte[] sig, int offset, int len) {
aoqi@0: signature = sig;
aoqi@0: sigp = offset;
aoqi@0: siglimit = offset + len;
aoqi@0: return sigToType();
aoqi@0: }
aoqi@0:
aoqi@0: /** Convert signature to type, where signature is implicit.
aoqi@0: */
aoqi@0: Type sigToType() {
aoqi@0: switch ((char) signature[sigp]) {
aoqi@0: case 'T':
aoqi@0: sigp++;
aoqi@0: int start = sigp;
aoqi@0: while (signature[sigp] != ';') sigp++;
aoqi@0: sigp++;
aoqi@0: return sigEnterPhase
aoqi@0: ? Type.noType
aoqi@0: : findTypeVar(names.fromUtf(signature, start, sigp - 1 - start));
aoqi@0: case '+': {
aoqi@0: sigp++;
aoqi@0: Type t = sigToType();
aoqi@0: return new WildcardType(t, BoundKind.EXTENDS,
aoqi@0: syms.boundClass);
aoqi@0: }
aoqi@0: case '*':
aoqi@0: sigp++;
aoqi@0: return new WildcardType(syms.objectType, BoundKind.UNBOUND,
aoqi@0: syms.boundClass);
aoqi@0: case '-': {
aoqi@0: sigp++;
aoqi@0: Type t = sigToType();
aoqi@0: return new WildcardType(t, BoundKind.SUPER,
aoqi@0: syms.boundClass);
aoqi@0: }
aoqi@0: case 'B':
aoqi@0: sigp++;
aoqi@0: return syms.byteType;
aoqi@0: case 'C':
aoqi@0: sigp++;
aoqi@0: return syms.charType;
aoqi@0: case 'D':
aoqi@0: sigp++;
aoqi@0: return syms.doubleType;
aoqi@0: case 'F':
aoqi@0: sigp++;
aoqi@0: return syms.floatType;
aoqi@0: case 'I':
aoqi@0: sigp++;
aoqi@0: return syms.intType;
aoqi@0: case 'J':
aoqi@0: sigp++;
aoqi@0: return syms.longType;
aoqi@0: case 'L':
aoqi@0: {
aoqi@0: // int oldsigp = sigp;
aoqi@0: Type t = classSigToType();
aoqi@0: if (sigp < siglimit && signature[sigp] == '.')
aoqi@0: throw badClassFile("deprecated inner class signature syntax " +
aoqi@0: "(please recompile from source)");
aoqi@0: /*
aoqi@0: System.err.println(" decoded " +
aoqi@0: new String(signature, oldsigp, sigp-oldsigp) +
aoqi@0: " => " + t + " outer " + t.outer());
aoqi@0: */
aoqi@0: return t;
aoqi@0: }
aoqi@0: case 'S':
aoqi@0: sigp++;
aoqi@0: return syms.shortType;
aoqi@0: case 'V':
aoqi@0: sigp++;
aoqi@0: return syms.voidType;
aoqi@0: case 'Z':
aoqi@0: sigp++;
aoqi@0: return syms.booleanType;
aoqi@0: case '[':
aoqi@0: sigp++;
aoqi@0: return new ArrayType(sigToType(), syms.arrayClass);
aoqi@0: case '(':
aoqi@0: sigp++;
aoqi@0: List argtypes = sigToTypes(')');
aoqi@0: Type restype = sigToType();
aoqi@0: List thrown = List.nil();
aoqi@0: while (signature[sigp] == '^') {
aoqi@0: sigp++;
aoqi@0: thrown = thrown.prepend(sigToType());
aoqi@0: }
aoqi@0: // if there is a typevar in the throws clause we should state it.
aoqi@0: for (List l = thrown; l.nonEmpty(); l = l.tail) {
aoqi@0: if (l.head.hasTag(TYPEVAR)) {
aoqi@0: l.head.tsym.flags_field |= THROWS;
aoqi@0: }
aoqi@0: }
aoqi@0: return new MethodType(argtypes,
aoqi@0: restype,
aoqi@0: thrown.reverse(),
aoqi@0: syms.methodClass);
aoqi@0: case '<':
aoqi@0: typevars = typevars.dup(currentOwner);
aoqi@0: Type poly = new ForAll(sigToTypeParams(), sigToType());
aoqi@0: typevars = typevars.leave();
aoqi@0: return poly;
aoqi@0: default:
aoqi@0: throw badClassFile("bad.signature",
aoqi@0: Convert.utf2string(signature, sigp, 10));
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: byte[] signatureBuffer = new byte[0];
aoqi@0: int sbp = 0;
aoqi@0: /** Convert class signature to type, where signature is implicit.
aoqi@0: */
aoqi@0: Type classSigToType() {
aoqi@0: if (signature[sigp] != 'L')
aoqi@0: throw badClassFile("bad.class.signature",
aoqi@0: Convert.utf2string(signature, sigp, 10));
aoqi@0: sigp++;
aoqi@0: Type outer = Type.noType;
aoqi@0: int startSbp = sbp;
aoqi@0:
aoqi@0: while (true) {
aoqi@0: final byte c = signature[sigp++];
aoqi@0: switch (c) {
aoqi@0:
aoqi@0: case ';': { // end
aoqi@0: ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
aoqi@0: startSbp,
aoqi@0: sbp - startSbp));
aoqi@0:
aoqi@0: try {
aoqi@0: return (outer == Type.noType) ?
aoqi@0: t.erasure(types) :
aoqi@0: new ClassType(outer, List.nil(), t);
aoqi@0: } finally {
aoqi@0: sbp = startSbp;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: case '<': // generic arguments
aoqi@0: ClassSymbol t = enterClass(names.fromUtf(signatureBuffer,
aoqi@0: startSbp,
aoqi@0: sbp - startSbp));
aoqi@0: outer = new ClassType(outer, sigToTypes('>'), t) {
aoqi@0: boolean completed = false;
aoqi@0: @Override
aoqi@0: public Type getEnclosingType() {
aoqi@0: if (!completed) {
aoqi@0: completed = true;
aoqi@0: tsym.complete();
aoqi@0: Type enclosingType = tsym.type.getEnclosingType();
aoqi@0: if (enclosingType != Type.noType) {
aoqi@0: List typeArgs =
aoqi@0: super.getEnclosingType().allparams();
aoqi@0: List typeParams =
aoqi@0: enclosingType.allparams();
aoqi@0: if (typeParams.length() != typeArgs.length()) {
aoqi@0: // no "rare" types
aoqi@0: super.setEnclosingType(types.erasure(enclosingType));
aoqi@0: } else {
aoqi@0: super.setEnclosingType(types.subst(enclosingType,
aoqi@0: typeParams,
aoqi@0: typeArgs));
aoqi@0: }
aoqi@0: } else {
aoqi@0: super.setEnclosingType(Type.noType);
aoqi@0: }
aoqi@0: }
aoqi@0: return super.getEnclosingType();
aoqi@0: }
aoqi@0: @Override
aoqi@0: public void setEnclosingType(Type outer) {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0: };
aoqi@0: switch (signature[sigp++]) {
aoqi@0: case ';':
aoqi@0: if (sigp < signature.length && signature[sigp] == '.') {
aoqi@0: // support old-style GJC signatures
aoqi@0: // The signature produced was
aoqi@0: // Lfoo/Outer;.Lfoo/Outer$Inner;
aoqi@0: // rather than say
aoqi@0: // Lfoo/Outer.Inner;
aoqi@0: // so we skip past ".Lfoo/Outer$"
aoqi@0: sigp += (sbp - startSbp) + // "foo/Outer"
aoqi@0: 3; // ".L" and "$"
aoqi@0: signatureBuffer[sbp++] = (byte)'$';
aoqi@0: break;
aoqi@0: } else {
aoqi@0: sbp = startSbp;
aoqi@0: return outer;
aoqi@0: }
aoqi@0: case '.':
aoqi@0: signatureBuffer[sbp++] = (byte)'$';
aoqi@0: break;
aoqi@0: default:
aoqi@0: throw new AssertionError(signature[sigp-1]);
aoqi@0: }
aoqi@0: continue;
aoqi@0:
aoqi@0: case '.':
aoqi@0: //we have seen an enclosing non-generic class
aoqi@0: if (outer != Type.noType) {
aoqi@0: t = enterClass(names.fromUtf(signatureBuffer,
aoqi@0: startSbp,
aoqi@0: sbp - startSbp));
aoqi@0: outer = new ClassType(outer, List.nil(), t);
aoqi@0: }
aoqi@0: signatureBuffer[sbp++] = (byte)'$';
aoqi@0: continue;
aoqi@0: case '/':
aoqi@0: signatureBuffer[sbp++] = (byte)'.';
aoqi@0: continue;
aoqi@0: default:
aoqi@0: signatureBuffer[sbp++] = c;
aoqi@0: continue;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Convert (implicit) signature to list of types
aoqi@0: * until `terminator' is encountered.
aoqi@0: */
aoqi@0: List sigToTypes(char terminator) {
aoqi@0: List head = List.of(null);
aoqi@0: List tail = head;
aoqi@0: while (signature[sigp] != terminator)
aoqi@0: tail = tail.setTail(List.of(sigToType()));
aoqi@0: sigp++;
aoqi@0: return head.tail;
aoqi@0: }
aoqi@0:
aoqi@0: /** Convert signature to type parameters, where signature is a byte
aoqi@0: * array segment.
aoqi@0: */
aoqi@0: List sigToTypeParams(byte[] sig, int offset, int len) {
aoqi@0: signature = sig;
aoqi@0: sigp = offset;
aoqi@0: siglimit = offset + len;
aoqi@0: return sigToTypeParams();
aoqi@0: }
aoqi@0:
aoqi@0: /** Convert signature to type parameters, where signature is implicit.
aoqi@0: */
aoqi@0: List sigToTypeParams() {
aoqi@0: List tvars = List.nil();
aoqi@0: if (signature[sigp] == '<') {
aoqi@0: sigp++;
aoqi@0: int start = sigp;
aoqi@0: sigEnterPhase = true;
aoqi@0: while (signature[sigp] != '>')
aoqi@0: tvars = tvars.prepend(sigToTypeParam());
aoqi@0: sigEnterPhase = false;
aoqi@0: sigp = start;
aoqi@0: while (signature[sigp] != '>')
aoqi@0: sigToTypeParam();
aoqi@0: sigp++;
aoqi@0: }
aoqi@0: return tvars.reverse();
aoqi@0: }
aoqi@0:
aoqi@0: /** Convert (implicit) signature to type parameter.
aoqi@0: */
aoqi@0: Type sigToTypeParam() {
aoqi@0: int start = sigp;
aoqi@0: while (signature[sigp] != ':') sigp++;
aoqi@0: Name name = names.fromUtf(signature, start, sigp - start);
aoqi@0: TypeVar tvar;
aoqi@0: if (sigEnterPhase) {
aoqi@0: tvar = new TypeVar(name, currentOwner, syms.botType);
aoqi@0: typevars.enter(tvar.tsym);
aoqi@0: } else {
aoqi@0: tvar = (TypeVar)findTypeVar(name);
aoqi@0: }
aoqi@0: List bounds = List.nil();
aoqi@0: boolean allInterfaces = false;
aoqi@0: if (signature[sigp] == ':' && signature[sigp+1] == ':') {
aoqi@0: sigp++;
aoqi@0: allInterfaces = true;
aoqi@0: }
aoqi@0: while (signature[sigp] == ':') {
aoqi@0: sigp++;
aoqi@0: bounds = bounds.prepend(sigToType());
aoqi@0: }
aoqi@0: if (!sigEnterPhase) {
aoqi@0: types.setBounds(tvar, bounds.reverse(), allInterfaces);
aoqi@0: }
aoqi@0: return tvar;
aoqi@0: }
aoqi@0:
aoqi@0: /** Find type variable with given name in `typevars' scope.
aoqi@0: */
aoqi@0: Type findTypeVar(Name name) {
aoqi@0: Scope.Entry e = typevars.lookup(name);
aoqi@0: if (e.scope != null) {
aoqi@0: return e.sym.type;
aoqi@0: } else {
aoqi@0: if (readingClassAttr) {
aoqi@0: // While reading the class attribute, the supertypes
aoqi@0: // might refer to a type variable from an enclosing element
aoqi@0: // (method or class).
aoqi@0: // If the type variable is defined in the enclosing class,
aoqi@0: // we can actually find it in
aoqi@0: // currentOwner.owner.type.getTypeArguments()
aoqi@0: // However, until we have read the enclosing method attribute
aoqi@0: // we don't know for sure if this owner is correct. It could
aoqi@0: // be a method and there is no way to tell before reading the
aoqi@0: // enclosing method attribute.
aoqi@0: TypeVar t = new TypeVar(name, currentOwner, syms.botType);
aoqi@0: missingTypeVariables = missingTypeVariables.prepend(t);
aoqi@0: // System.err.println("Missing type var " + name);
aoqi@0: return t;
aoqi@0: }
aoqi@0: throw badClassFile("undecl.type.var", name);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Reading Attributes
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0: protected enum AttributeKind { CLASS, MEMBER };
aoqi@0: protected abstract class AttributeReader {
aoqi@0: protected AttributeReader(Name name, ClassFile.Version version, Set kinds) {
aoqi@0: this.name = name;
aoqi@0: this.version = version;
aoqi@0: this.kinds = kinds;
aoqi@0: }
aoqi@0:
aoqi@0: protected boolean accepts(AttributeKind kind) {
aoqi@0: if (kinds.contains(kind)) {
aoqi@0: if (majorVersion > version.major || (majorVersion == version.major && minorVersion >= version.minor))
aoqi@0: return true;
aoqi@0:
aoqi@0: if (lintClassfile && !warnedAttrs.contains(name)) {
aoqi@0: JavaFileObject prev = log.useSource(currentClassFile);
aoqi@0: try {
aoqi@0: log.warning(LintCategory.CLASSFILE, (DiagnosticPosition) null, "future.attr",
aoqi@0: name, version.major, version.minor, majorVersion, minorVersion);
aoqi@0: } finally {
aoqi@0: log.useSource(prev);
aoqi@0: }
aoqi@0: warnedAttrs.add(name);
aoqi@0: }
aoqi@0: }
aoqi@0: return false;
aoqi@0: }
aoqi@0:
aoqi@0: protected abstract void read(Symbol sym, int attrLen);
aoqi@0:
aoqi@0: protected final Name name;
aoqi@0: protected final ClassFile.Version version;
aoqi@0: protected final Set kinds;
aoqi@0: }
aoqi@0:
aoqi@0: protected Set CLASS_ATTRIBUTE =
aoqi@0: EnumSet.of(AttributeKind.CLASS);
aoqi@0: protected Set MEMBER_ATTRIBUTE =
aoqi@0: EnumSet.of(AttributeKind.MEMBER);
aoqi@0: protected Set CLASS_OR_MEMBER_ATTRIBUTE =
aoqi@0: EnumSet.of(AttributeKind.CLASS, AttributeKind.MEMBER);
aoqi@0:
aoqi@0: protected Map attributeReaders = new HashMap();
aoqi@0:
aoqi@0: private void initAttributeReaders() {
aoqi@0: AttributeReader[] readers = {
aoqi@0: // v45.3 attributes
aoqi@0:
aoqi@0: new AttributeReader(names.Code, V45_3, MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: if (readAllOfClassFile || saveParameterNames)
aoqi@0: ((MethodSymbol)sym).code = readCode(sym);
aoqi@0: else
aoqi@0: bp = bp + attrLen;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.ConstantValue, V45_3, MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: Object v = readPool(nextChar());
aoqi@0: // Ignore ConstantValue attribute if field not final.
aoqi@0: if ((sym.flags() & FINAL) != 0)
aoqi@0: ((VarSymbol) sym).setData(v);
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.Deprecated, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: sym.flags_field |= DEPRECATED;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.Exceptions, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: int nexceptions = nextChar();
aoqi@0: List thrown = List.nil();
aoqi@0: for (int j = 0; j < nexceptions; j++)
aoqi@0: thrown = thrown.prepend(readClassSymbol(nextChar()).type);
aoqi@0: if (sym.type.getThrownTypes().isEmpty())
aoqi@0: sym.type.asMethodType().thrown = thrown.reverse();
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.InnerClasses, V45_3, CLASS_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: ClassSymbol c = (ClassSymbol) sym;
aoqi@0: readInnerClasses(c);
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.LocalVariableTable, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: int newbp = bp + attrLen;
aoqi@0: if (saveParameterNames && !sawMethodParameters) {
aoqi@0: // Pick up parameter names from the variable table.
aoqi@0: // Parameter names are not explicitly identified as such,
aoqi@0: // but all parameter name entries in the LocalVariableTable
aoqi@0: // have a start_pc of 0. Therefore, we record the name
aoqi@0: // indicies of all slots with a start_pc of zero in the
aoqi@0: // parameterNameIndicies array.
aoqi@0: // Note that this implicitly honors the JVMS spec that
aoqi@0: // there may be more than one LocalVariableTable, and that
aoqi@0: // there is no specified ordering for the entries.
aoqi@0: int numEntries = nextChar();
aoqi@0: for (int i = 0; i < numEntries; i++) {
aoqi@0: int start_pc = nextChar();
aoqi@0: int length = nextChar();
aoqi@0: int nameIndex = nextChar();
aoqi@0: int sigIndex = nextChar();
aoqi@0: int register = nextChar();
aoqi@0: if (start_pc == 0) {
aoqi@0: // ensure array large enough
aoqi@0: if (register >= parameterNameIndices.length) {
aoqi@0: int newSize = Math.max(register, parameterNameIndices.length + 8);
aoqi@0: parameterNameIndices =
aoqi@0: Arrays.copyOf(parameterNameIndices, newSize);
aoqi@0: }
aoqi@0: parameterNameIndices[register] = nameIndex;
aoqi@0: haveParameterNameIndices = true;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: bp = newbp;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.MethodParameters, V52, MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrlen) {
aoqi@0: int newbp = bp + attrlen;
aoqi@0: if (saveParameterNames) {
aoqi@0: sawMethodParameters = true;
aoqi@0: int numEntries = nextByte();
aoqi@0: parameterNameIndices = new int[numEntries];
aoqi@0: haveParameterNameIndices = true;
aoqi@0: for (int i = 0; i < numEntries; i++) {
aoqi@0: int nameIndex = nextChar();
aoqi@0: int flags = nextChar();
aoqi@0: parameterNameIndices[i] = nameIndex;
aoqi@0: }
aoqi@0: }
aoqi@0: bp = newbp;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0:
aoqi@0: new AttributeReader(names.SourceFile, V45_3, CLASS_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: ClassSymbol c = (ClassSymbol) sym;
aoqi@0: Name n = readName(nextChar());
aoqi@0: c.sourcefile = new SourceFileObject(n, c.flatname);
aoqi@0: // If the class is a toplevel class, originating from a Java source file,
aoqi@0: // but the class name does not match the file name, then it is
aoqi@0: // an auxiliary class.
aoqi@0: String sn = n.toString();
aoqi@0: if (c.owner.kind == Kinds.PCK &&
aoqi@0: sn.endsWith(".java") &&
aoqi@0: !sn.equals(c.name.toString()+".java")) {
aoqi@0: c.flags_field |= AUXILIARY;
aoqi@0: }
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.Synthetic, V45_3, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: // bridge methods are visible when generics not enabled
aoqi@0: if (allowGenerics || (sym.flags_field & BRIDGE) == 0)
aoqi@0: sym.flags_field |= SYNTHETIC;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: // standard v49 attributes
aoqi@0:
aoqi@0: new AttributeReader(names.EnclosingMethod, V49, CLASS_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: int newbp = bp + attrLen;
aoqi@0: readEnclosingMethodAttr(sym);
aoqi@0: bp = newbp;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.Signature, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: @Override
aoqi@0: protected boolean accepts(AttributeKind kind) {
aoqi@0: return super.accepts(kind) && allowGenerics;
aoqi@0: }
aoqi@0:
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: if (sym.kind == TYP) {
aoqi@0: ClassSymbol c = (ClassSymbol) sym;
aoqi@0: readingClassAttr = true;
aoqi@0: try {
aoqi@0: ClassType ct1 = (ClassType)c.type;
aoqi@0: Assert.check(c == currentOwner);
aoqi@0: ct1.typarams_field = readTypeParams(nextChar());
aoqi@0: ct1.supertype_field = sigToType();
aoqi@0: ListBuffer is = new ListBuffer();
aoqi@0: while (sigp != siglimit) is.append(sigToType());
aoqi@0: ct1.interfaces_field = is.toList();
aoqi@0: } finally {
aoqi@0: readingClassAttr = false;
aoqi@0: }
aoqi@0: } else {
aoqi@0: List thrown = sym.type.getThrownTypes();
aoqi@0: sym.type = readType(nextChar());
aoqi@0: //- System.err.println(" # " + sym.type);
aoqi@0: if (sym.kind == MTH && sym.type.getThrownTypes().isEmpty())
aoqi@0: sym.type.asMethodType().thrown = thrown;
aoqi@0:
aoqi@0: }
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: // v49 annotation attributes
aoqi@0:
aoqi@0: new AttributeReader(names.AnnotationDefault, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: attachAnnotationDefault(sym);
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.RuntimeInvisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: attachAnnotations(sym);
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.RuntimeInvisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: attachParameterAnnotations(sym);
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.RuntimeVisibleAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: attachAnnotations(sym);
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.RuntimeVisibleParameterAnnotations, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: attachParameterAnnotations(sym);
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: // additional "legacy" v49 attributes, superceded by flags
aoqi@0:
aoqi@0: new AttributeReader(names.Annotation, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: if (allowAnnotations)
aoqi@0: sym.flags_field |= ANNOTATION;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.Bridge, V49, MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: sym.flags_field |= BRIDGE;
aoqi@0: if (!allowGenerics)
aoqi@0: sym.flags_field &= ~SYNTHETIC;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.Enum, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: sym.flags_field |= ENUM;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.Varargs, V49, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: if (allowVarargs)
aoqi@0: sym.flags_field |= VARARGS;
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.RuntimeVisibleTypeAnnotations, V52, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: attachTypeAnnotations(sym);
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0: new AttributeReader(names.RuntimeInvisibleTypeAnnotations, V52, CLASS_OR_MEMBER_ATTRIBUTE) {
aoqi@0: protected void read(Symbol sym, int attrLen) {
aoqi@0: attachTypeAnnotations(sym);
aoqi@0: }
aoqi@0: },
aoqi@0:
aoqi@0:
aoqi@0: // The following attributes for a Code attribute are not currently handled
aoqi@0: // StackMapTable
aoqi@0: // SourceDebugExtension
aoqi@0: // LineNumberTable
aoqi@0: // LocalVariableTypeTable
aoqi@0: };
aoqi@0:
aoqi@0: for (AttributeReader r: readers)
aoqi@0: attributeReaders.put(r.name, r);
aoqi@0: }
aoqi@0:
aoqi@0: /** Report unrecognized attribute.
aoqi@0: */
aoqi@0: void unrecognized(Name attrName) {
aoqi@0: if (checkClassFile)
aoqi@0: printCCF("ccf.unrecognized.attribute", attrName);
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0:
aoqi@0: protected void readEnclosingMethodAttr(Symbol sym) {
aoqi@0: // sym is a nested class with an "Enclosing Method" attribute
aoqi@0: // remove sym from it's current owners scope and place it in
aoqi@0: // the scope specified by the attribute
aoqi@0: sym.owner.members().remove(sym);
aoqi@0: ClassSymbol self = (ClassSymbol)sym;
aoqi@0: ClassSymbol c = readClassSymbol(nextChar());
aoqi@0: NameAndType nt = readNameAndType(nextChar());
aoqi@0:
aoqi@0: if (c.members_field == null)
aoqi@0: throw badClassFile("bad.enclosing.class", self, c);
aoqi@0:
aoqi@0: MethodSymbol m = findMethod(nt, c.members_field, self.flags());
aoqi@0: if (nt != null && m == null)
aoqi@0: throw badClassFile("bad.enclosing.method", self);
aoqi@0:
aoqi@0: self.name = simpleBinaryName(self.flatname, c.flatname) ;
aoqi@0: self.owner = m != null ? m : c;
aoqi@0: if (self.name.isEmpty())
aoqi@0: self.fullname = names.empty;
aoqi@0: else
aoqi@0: self.fullname = ClassSymbol.formFullName(self.name, self.owner);
aoqi@0:
aoqi@0: if (m != null) {
aoqi@0: ((ClassType)sym.type).setEnclosingType(m.type);
aoqi@0: } else if ((self.flags_field & STATIC) == 0) {
aoqi@0: ((ClassType)sym.type).setEnclosingType(c.type);
aoqi@0: } else {
aoqi@0: ((ClassType)sym.type).setEnclosingType(Type.noType);
aoqi@0: }
aoqi@0: enterTypevars(self);
aoqi@0: if (!missingTypeVariables.isEmpty()) {
aoqi@0: ListBuffer typeVars = new ListBuffer();
aoqi@0: for (Type typevar : missingTypeVariables) {
aoqi@0: typeVars.append(findTypeVar(typevar.tsym.name));
aoqi@0: }
aoqi@0: foundTypeVariables = typeVars.toList();
aoqi@0: } else {
aoqi@0: foundTypeVariables = List.nil();
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: // See java.lang.Class
aoqi@0: private Name simpleBinaryName(Name self, Name enclosing) {
aoqi@0: String simpleBinaryName = self.toString().substring(enclosing.toString().length());
aoqi@0: if (simpleBinaryName.length() < 1 || simpleBinaryName.charAt(0) != '$')
aoqi@0: throw badClassFile("bad.enclosing.method", self);
aoqi@0: int index = 1;
aoqi@0: while (index < simpleBinaryName.length() &&
aoqi@0: isAsciiDigit(simpleBinaryName.charAt(index)))
aoqi@0: index++;
aoqi@0: return names.fromString(simpleBinaryName.substring(index));
aoqi@0: }
aoqi@0:
aoqi@0: private MethodSymbol findMethod(NameAndType nt, Scope scope, long flags) {
aoqi@0: if (nt == null)
aoqi@0: return null;
aoqi@0:
aoqi@0: MethodType type = nt.uniqueType.type.asMethodType();
aoqi@0:
aoqi@0: for (Scope.Entry e = scope.lookup(nt.name); e.scope != null; e = e.next())
aoqi@0: if (e.sym.kind == MTH && isSameBinaryType(e.sym.type.asMethodType(), type))
aoqi@0: return (MethodSymbol)e.sym;
aoqi@0:
aoqi@0: if (nt.name != names.init)
aoqi@0: // not a constructor
aoqi@0: return null;
aoqi@0: if ((flags & INTERFACE) != 0)
aoqi@0: // no enclosing instance
aoqi@0: return null;
aoqi@0: if (nt.uniqueType.type.getParameterTypes().isEmpty())
aoqi@0: // no parameters
aoqi@0: return null;
aoqi@0:
aoqi@0: // A constructor of an inner class.
aoqi@0: // Remove the first argument (the enclosing instance)
aoqi@0: nt.setType(new MethodType(nt.uniqueType.type.getParameterTypes().tail,
aoqi@0: nt.uniqueType.type.getReturnType(),
aoqi@0: nt.uniqueType.type.getThrownTypes(),
aoqi@0: syms.methodClass));
aoqi@0: // Try searching again
aoqi@0: return findMethod(nt, scope, flags);
aoqi@0: }
aoqi@0:
aoqi@0: /** Similar to Types.isSameType but avoids completion */
aoqi@0: private boolean isSameBinaryType(MethodType mt1, MethodType mt2) {
aoqi@0: List types1 = types.erasure(mt1.getParameterTypes())
aoqi@0: .prepend(types.erasure(mt1.getReturnType()));
aoqi@0: List types2 = mt2.getParameterTypes().prepend(mt2.getReturnType());
aoqi@0: while (!types1.isEmpty() && !types2.isEmpty()) {
aoqi@0: if (types1.head.tsym != types2.head.tsym)
aoqi@0: return false;
aoqi@0: types1 = types1.tail;
aoqi@0: types2 = types2.tail;
aoqi@0: }
aoqi@0: return types1.isEmpty() && types2.isEmpty();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Character.isDigit answers true to some non-ascii
aoqi@0: * digits. This one does not. copied from java.lang.Class
aoqi@0: */
aoqi@0: private static boolean isAsciiDigit(char c) {
aoqi@0: return '0' <= c && c <= '9';
aoqi@0: }
aoqi@0:
aoqi@0: /** Read member attributes.
aoqi@0: */
aoqi@0: void readMemberAttrs(Symbol sym) {
aoqi@0: readAttrs(sym, AttributeKind.MEMBER);
aoqi@0: }
aoqi@0:
aoqi@0: void readAttrs(Symbol sym, AttributeKind kind) {
aoqi@0: char ac = nextChar();
aoqi@0: for (int i = 0; i < ac; i++) {
aoqi@0: Name attrName = readName(nextChar());
aoqi@0: int attrLen = nextInt();
aoqi@0: AttributeReader r = attributeReaders.get(attrName);
aoqi@0: if (r != null && r.accepts(kind))
aoqi@0: r.read(sym, attrLen);
aoqi@0: else {
aoqi@0: unrecognized(attrName);
aoqi@0: bp = bp + attrLen;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: private boolean readingClassAttr = false;
aoqi@0: private List missingTypeVariables = List.nil();
aoqi@0: private List foundTypeVariables = List.nil();
aoqi@0:
aoqi@0: /** Read class attributes.
aoqi@0: */
aoqi@0: void readClassAttrs(ClassSymbol c) {
aoqi@0: readAttrs(c, AttributeKind.CLASS);
aoqi@0: }
aoqi@0:
aoqi@0: /** Read code block.
aoqi@0: */
aoqi@0: Code readCode(Symbol owner) {
aoqi@0: nextChar(); // max_stack
aoqi@0: nextChar(); // max_locals
aoqi@0: final int code_length = nextInt();
aoqi@0: bp += code_length;
aoqi@0: final char exception_table_length = nextChar();
aoqi@0: bp += exception_table_length * 8;
aoqi@0: readMemberAttrs(owner);
aoqi@0: return null;
aoqi@0: }
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Reading Java-language annotations
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0: /** Attach annotations.
aoqi@0: */
aoqi@0: void attachAnnotations(final Symbol sym) {
aoqi@0: int numAttributes = nextChar();
aoqi@0: if (numAttributes != 0) {
aoqi@0: ListBuffer proxies =
aoqi@0: new ListBuffer();
aoqi@0: for (int i = 0; i v: proxy.values) {
aoqi@0: if (v.fst == names.value && v.snd instanceof Attribute.Constant) {
aoqi@0: Attribute.Constant c = (Attribute.Constant) v.snd;
aoqi@0: if (c.type == syms.intType && ((Integer) c.value) > profile.value) {
aoqi@0: sym.flags_field |= NOT_IN_PROFILE;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: } else
aoqi@0: proxies.append(proxy);
aoqi@0: }
aoqi@0: annotate.normal(new AnnotationCompleter(sym, proxies.toList()));
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Attach parameter annotations.
aoqi@0: */
aoqi@0: void attachParameterAnnotations(final Symbol method) {
aoqi@0: final MethodSymbol meth = (MethodSymbol)method;
aoqi@0: int numParameters = buf[bp++] & 0xFF;
aoqi@0: List parameters = meth.params();
aoqi@0: int pnum = 0;
aoqi@0: while (parameters.tail != null) {
aoqi@0: attachAnnotations(parameters.head);
aoqi@0: parameters = parameters.tail;
aoqi@0: pnum++;
aoqi@0: }
aoqi@0: if (pnum != numParameters) {
aoqi@0: throw badClassFile("bad.runtime.invisible.param.annotations", meth);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: void attachTypeAnnotations(final Symbol sym) {
aoqi@0: int numAttributes = nextChar();
aoqi@0: if (numAttributes != 0) {
aoqi@0: ListBuffer proxies = new ListBuffer<>();
aoqi@0: for (int i = 0; i < numAttributes; i++)
aoqi@0: proxies.append(readTypeAnnotation());
aoqi@0: annotate.normal(new TypeAnnotationCompleter(sym, proxies.toList()));
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Attach the default value for an annotation element.
aoqi@0: */
aoqi@0: void attachAnnotationDefault(final Symbol sym) {
aoqi@0: final MethodSymbol meth = (MethodSymbol)sym; // only on methods
aoqi@0: final Attribute value = readAttributeValue();
aoqi@0:
aoqi@0: // The default value is set later during annotation. It might
aoqi@0: // be the case that the Symbol sym is annotated _after_ the
aoqi@0: // repeating instances that depend on this default value,
aoqi@0: // because of this we set an interim value that tells us this
aoqi@0: // element (most likely) has a default.
aoqi@0: //
aoqi@0: // Set interim value for now, reset just before we do this
aoqi@0: // properly at annotate time.
aoqi@0: meth.defaultValue = value;
aoqi@0: annotate.normal(new AnnotationDefaultCompleter(meth, value));
aoqi@0: }
aoqi@0:
aoqi@0: Type readTypeOrClassSymbol(int i) {
aoqi@0: // support preliminary jsr175-format class files
aoqi@0: if (buf[poolIdx[i]] == CONSTANT_Class)
aoqi@0: return readClassSymbol(i).type;
aoqi@0: return readType(i);
aoqi@0: }
aoqi@0: Type readEnumType(int i) {
aoqi@0: // support preliminary jsr175-format class files
aoqi@0: int index = poolIdx[i];
aoqi@0: int length = getChar(index + 1);
aoqi@0: if (buf[index + length + 2] != ';')
aoqi@0: return enterClass(readName(i)).type;
aoqi@0: return readType(i);
aoqi@0: }
aoqi@0:
aoqi@0: CompoundAnnotationProxy readCompoundAnnotation() {
aoqi@0: Type t = readTypeOrClassSymbol(nextChar());
aoqi@0: int numFields = nextChar();
aoqi@0: ListBuffer> pairs =
aoqi@0: new ListBuffer>();
aoqi@0: for (int i=0; i(name, value));
aoqi@0: }
aoqi@0: return new CompoundAnnotationProxy(t, pairs.toList());
aoqi@0: }
aoqi@0:
aoqi@0: TypeAnnotationProxy readTypeAnnotation() {
aoqi@0: TypeAnnotationPosition position = readPosition();
aoqi@0: CompoundAnnotationProxy proxy = readCompoundAnnotation();
aoqi@0:
aoqi@0: return new TypeAnnotationProxy(proxy, position);
aoqi@0: }
aoqi@0:
aoqi@0: TypeAnnotationPosition readPosition() {
aoqi@0: int tag = nextByte(); // TargetType tag is a byte
aoqi@0:
aoqi@0: if (!TargetType.isValidTargetTypeValue(tag))
aoqi@0: throw this.badClassFile("bad.type.annotation.value", String.format("0x%02X", tag));
aoqi@0:
aoqi@0: TypeAnnotationPosition position = new TypeAnnotationPosition();
aoqi@0: TargetType type = TargetType.fromTargetTypeValue(tag);
aoqi@0:
aoqi@0: position.type = type;
aoqi@0:
aoqi@0: switch (type) {
aoqi@0: // instanceof
aoqi@0: case INSTANCEOF:
aoqi@0: // new expression
aoqi@0: case NEW:
aoqi@0: // constructor/method reference receiver
aoqi@0: case CONSTRUCTOR_REFERENCE:
aoqi@0: case METHOD_REFERENCE:
aoqi@0: position.offset = nextChar();
aoqi@0: break;
aoqi@0: // local variable
aoqi@0: case LOCAL_VARIABLE:
aoqi@0: // resource variable
aoqi@0: case RESOURCE_VARIABLE:
aoqi@0: int table_length = nextChar();
aoqi@0: position.lvarOffset = new int[table_length];
aoqi@0: position.lvarLength = new int[table_length];
aoqi@0: position.lvarIndex = new int[table_length];
aoqi@0:
aoqi@0: for (int i = 0; i < table_length; ++i) {
aoqi@0: position.lvarOffset[i] = nextChar();
aoqi@0: position.lvarLength[i] = nextChar();
aoqi@0: position.lvarIndex[i] = nextChar();
aoqi@0: }
aoqi@0: break;
aoqi@0: // exception parameter
aoqi@0: case EXCEPTION_PARAMETER:
aoqi@0: position.exception_index = nextChar();
aoqi@0: break;
aoqi@0: // method receiver
aoqi@0: case METHOD_RECEIVER:
aoqi@0: // Do nothing
aoqi@0: break;
aoqi@0: // type parameter
aoqi@0: case CLASS_TYPE_PARAMETER:
aoqi@0: case METHOD_TYPE_PARAMETER:
aoqi@0: position.parameter_index = nextByte();
aoqi@0: break;
aoqi@0: // type parameter bound
aoqi@0: case CLASS_TYPE_PARAMETER_BOUND:
aoqi@0: case METHOD_TYPE_PARAMETER_BOUND:
aoqi@0: position.parameter_index = nextByte();
aoqi@0: position.bound_index = nextByte();
aoqi@0: break;
aoqi@0: // class extends or implements clause
aoqi@0: case CLASS_EXTENDS:
aoqi@0: position.type_index = nextChar();
aoqi@0: break;
aoqi@0: // throws
aoqi@0: case THROWS:
aoqi@0: position.type_index = nextChar();
aoqi@0: break;
aoqi@0: // method parameter
aoqi@0: case METHOD_FORMAL_PARAMETER:
aoqi@0: position.parameter_index = nextByte();
aoqi@0: break;
aoqi@0: // type cast
aoqi@0: case CAST:
aoqi@0: // method/constructor/reference type argument
aoqi@0: case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
aoqi@0: case METHOD_INVOCATION_TYPE_ARGUMENT:
aoqi@0: case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
aoqi@0: case METHOD_REFERENCE_TYPE_ARGUMENT:
aoqi@0: position.offset = nextChar();
aoqi@0: position.type_index = nextByte();
aoqi@0: break;
aoqi@0: // We don't need to worry about these
aoqi@0: case METHOD_RETURN:
aoqi@0: case FIELD:
aoqi@0: break;
aoqi@0: case UNKNOWN:
aoqi@0: throw new AssertionError("jvm.ClassReader: UNKNOWN target type should never occur!");
aoqi@0: default:
aoqi@0: throw new AssertionError("jvm.ClassReader: Unknown target type for position: " + position);
aoqi@0: }
aoqi@0:
aoqi@0: { // See whether there is location info and read it
aoqi@0: int len = nextByte();
aoqi@0: ListBuffer loc = new ListBuffer<>();
aoqi@0: for (int i = 0; i < len * TypeAnnotationPosition.TypePathEntry.bytesPerEntry; ++i)
aoqi@0: loc = loc.append(nextByte());
aoqi@0: position.location = TypeAnnotationPosition.getTypePathFromBinary(loc.toList());
aoqi@0: }
aoqi@0:
aoqi@0: return position;
aoqi@0: }
aoqi@0:
aoqi@0: Attribute readAttributeValue() {
aoqi@0: char c = (char) buf[bp++];
aoqi@0: switch (c) {
aoqi@0: case 'B':
aoqi@0: return new Attribute.Constant(syms.byteType, readPool(nextChar()));
aoqi@0: case 'C':
aoqi@0: return new Attribute.Constant(syms.charType, readPool(nextChar()));
aoqi@0: case 'D':
aoqi@0: return new Attribute.Constant(syms.doubleType, readPool(nextChar()));
aoqi@0: case 'F':
aoqi@0: return new Attribute.Constant(syms.floatType, readPool(nextChar()));
aoqi@0: case 'I':
aoqi@0: return new Attribute.Constant(syms.intType, readPool(nextChar()));
aoqi@0: case 'J':
aoqi@0: return new Attribute.Constant(syms.longType, readPool(nextChar()));
aoqi@0: case 'S':
aoqi@0: return new Attribute.Constant(syms.shortType, readPool(nextChar()));
aoqi@0: case 'Z':
aoqi@0: return new Attribute.Constant(syms.booleanType, readPool(nextChar()));
aoqi@0: case 's':
aoqi@0: return new Attribute.Constant(syms.stringType, readPool(nextChar()).toString());
aoqi@0: case 'e':
aoqi@0: return new EnumAttributeProxy(readEnumType(nextChar()), readName(nextChar()));
aoqi@0: case 'c':
aoqi@0: return new Attribute.Class(types, readTypeOrClassSymbol(nextChar()));
aoqi@0: case '[': {
aoqi@0: int n = nextChar();
aoqi@0: ListBuffer l = new ListBuffer();
aoqi@0: for (int i=0; i values;
aoqi@0: ArrayAttributeProxy(List values) {
aoqi@0: super(null);
aoqi@0: this.values = values;
aoqi@0: }
aoqi@0: public void accept(Visitor v) { ((ProxyVisitor)v).visitArrayAttributeProxy(this); }
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return "{" + values + "}";
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** A temporary proxy representing a compound attribute.
aoqi@0: */
aoqi@0: static class CompoundAnnotationProxy extends Attribute {
aoqi@0: final List> values;
aoqi@0: public CompoundAnnotationProxy(Type type,
aoqi@0: List> values) {
aoqi@0: super(type);
aoqi@0: this.values = values;
aoqi@0: }
aoqi@0: public void accept(Visitor v) { ((ProxyVisitor)v).visitCompoundAnnotationProxy(this); }
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: StringBuilder buf = new StringBuilder();
aoqi@0: buf.append("@");
aoqi@0: buf.append(type.tsym.getQualifiedName());
aoqi@0: buf.append("/*proxy*/{");
aoqi@0: boolean first = true;
aoqi@0: for (List> v = values;
aoqi@0: v.nonEmpty(); v = v.tail) {
aoqi@0: Pair value = v.head;
aoqi@0: if (!first) buf.append(",");
aoqi@0: first = false;
aoqi@0: buf.append(value.fst);
aoqi@0: buf.append("=");
aoqi@0: buf.append(value.snd);
aoqi@0: }
aoqi@0: buf.append("}");
aoqi@0: return buf.toString();
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** A temporary proxy representing a type annotation.
aoqi@0: */
aoqi@0: static class TypeAnnotationProxy {
aoqi@0: final CompoundAnnotationProxy compound;
aoqi@0: final TypeAnnotationPosition position;
aoqi@0: public TypeAnnotationProxy(CompoundAnnotationProxy compound,
aoqi@0: TypeAnnotationPosition position) {
aoqi@0: this.compound = compound;
aoqi@0: this.position = position;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: class AnnotationDeproxy implements ProxyVisitor {
aoqi@0: private ClassSymbol requestingOwner = currentOwner.kind == MTH
aoqi@0: ? currentOwner.enclClass() : (ClassSymbol)currentOwner;
aoqi@0:
aoqi@0: List deproxyCompoundList(List pl) {
aoqi@0: // also must fill in types!!!!
aoqi@0: ListBuffer buf =
aoqi@0: new ListBuffer();
aoqi@0: for (List l = pl; l.nonEmpty(); l=l.tail) {
aoqi@0: buf.append(deproxyCompound(l.head));
aoqi@0: }
aoqi@0: return buf.toList();
aoqi@0: }
aoqi@0:
aoqi@0: Attribute.Compound deproxyCompound(CompoundAnnotationProxy a) {
aoqi@0: ListBuffer> buf =
aoqi@0: new ListBuffer>();
aoqi@0: for (List> l = a.values;
aoqi@0: l.nonEmpty();
aoqi@0: l = l.tail) {
aoqi@0: MethodSymbol meth = findAccessMethod(a.type, l.head.fst);
aoqi@0: buf.append(new Pair
aoqi@0: (meth, deproxy(meth.type.getReturnType(), l.head.snd)));
aoqi@0: }
aoqi@0: return new Attribute.Compound(a.type, buf.toList());
aoqi@0: }
aoqi@0:
aoqi@0: MethodSymbol findAccessMethod(Type container, Name name) {
aoqi@0: CompletionFailure failure = null;
aoqi@0: try {
aoqi@0: for (Scope.Entry e = container.tsym.members().lookup(name);
aoqi@0: e.scope != null;
aoqi@0: e = e.next()) {
aoqi@0: Symbol sym = e.sym;
aoqi@0: if (sym.kind == MTH && sym.type.getParameterTypes().length() == 0)
aoqi@0: return (MethodSymbol) sym;
aoqi@0: }
aoqi@0: } catch (CompletionFailure ex) {
aoqi@0: failure = ex;
aoqi@0: }
aoqi@0: // The method wasn't found: emit a warning and recover
aoqi@0: JavaFileObject prevSource = log.useSource(requestingOwner.classfile);
aoqi@0: try {
darcy@2707: if (lintClassfile) {
darcy@2707: if (failure == null) {
darcy@2707: log.warning("annotation.method.not.found",
darcy@2707: container,
darcy@2707: name);
darcy@2707: } else {
darcy@2707: log.warning("annotation.method.not.found.reason",
darcy@2707: container,
darcy@2707: name,
darcy@2707: failure.getDetailValue()); //diagnostic, if present
darcy@2707: }
aoqi@0: }
aoqi@0: } finally {
aoqi@0: log.useSource(prevSource);
aoqi@0: }
aoqi@0: // Construct a new method type and symbol. Use bottom
aoqi@0: // type (typeof null) as return type because this type is
aoqi@0: // a subtype of all reference types and can be converted
aoqi@0: // to primitive types by unboxing.
aoqi@0: MethodType mt = new MethodType(List.nil(),
aoqi@0: syms.botType,
aoqi@0: List.nil(),
aoqi@0: syms.methodClass);
aoqi@0: return new MethodSymbol(PUBLIC | ABSTRACT, name, mt, container.tsym);
aoqi@0: }
aoqi@0:
aoqi@0: Attribute result;
aoqi@0: Type type;
aoqi@0: Attribute deproxy(Type t, Attribute a) {
aoqi@0: Type oldType = type;
aoqi@0: try {
aoqi@0: type = t;
aoqi@0: a.accept(this);
aoqi@0: return result;
aoqi@0: } finally {
aoqi@0: type = oldType;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: // implement Attribute.Visitor below
aoqi@0:
aoqi@0: public void visitConstant(Attribute.Constant value) {
aoqi@0: // assert value.type == type;
aoqi@0: result = value;
aoqi@0: }
aoqi@0:
aoqi@0: public void visitClass(Attribute.Class clazz) {
aoqi@0: result = clazz;
aoqi@0: }
aoqi@0:
aoqi@0: public void visitEnum(Attribute.Enum e) {
aoqi@0: throw new AssertionError(); // shouldn't happen
aoqi@0: }
aoqi@0:
aoqi@0: public void visitCompound(Attribute.Compound compound) {
aoqi@0: throw new AssertionError(); // shouldn't happen
aoqi@0: }
aoqi@0:
aoqi@0: public void visitArray(Attribute.Array array) {
aoqi@0: throw new AssertionError(); // shouldn't happen
aoqi@0: }
aoqi@0:
aoqi@0: public void visitError(Attribute.Error e) {
aoqi@0: throw new AssertionError(); // shouldn't happen
aoqi@0: }
aoqi@0:
aoqi@0: public void visitEnumAttributeProxy(EnumAttributeProxy proxy) {
aoqi@0: // type.tsym.flatName() should == proxy.enumFlatName
aoqi@0: TypeSymbol enumTypeSym = proxy.enumType.tsym;
aoqi@0: VarSymbol enumerator = null;
aoqi@0: CompletionFailure failure = null;
aoqi@0: try {
aoqi@0: for (Scope.Entry e = enumTypeSym.members().lookup(proxy.enumerator);
aoqi@0: e.scope != null;
aoqi@0: e = e.next()) {
aoqi@0: if (e.sym.kind == VAR) {
aoqi@0: enumerator = (VarSymbol)e.sym;
aoqi@0: break;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: catch (CompletionFailure ex) {
aoqi@0: failure = ex;
aoqi@0: }
aoqi@0: if (enumerator == null) {
aoqi@0: if (failure != null) {
aoqi@0: log.warning("unknown.enum.constant.reason",
aoqi@0: currentClassFile, enumTypeSym, proxy.enumerator,
aoqi@0: failure.getDiagnostic());
aoqi@0: } else {
aoqi@0: log.warning("unknown.enum.constant",
aoqi@0: currentClassFile, enumTypeSym, proxy.enumerator);
aoqi@0: }
aoqi@0: result = new Attribute.Enum(enumTypeSym.type,
aoqi@0: new VarSymbol(0, proxy.enumerator, syms.botType, enumTypeSym));
aoqi@0: } else {
aoqi@0: result = new Attribute.Enum(enumTypeSym.type, enumerator);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: public void visitArrayAttributeProxy(ArrayAttributeProxy proxy) {
aoqi@0: int length = proxy.values.length();
aoqi@0: Attribute[] ats = new Attribute[length];
aoqi@0: Type elemtype = types.elemtype(type);
aoqi@0: int i = 0;
aoqi@0: for (List p = proxy.values; p.nonEmpty(); p = p.tail) {
aoqi@0: ats[i++] = deproxy(elemtype, p.head);
aoqi@0: }
aoqi@0: result = new Attribute.Array(type, ats);
aoqi@0: }
aoqi@0:
aoqi@0: public void visitCompoundAnnotationProxy(CompoundAnnotationProxy proxy) {
aoqi@0: result = deproxyCompound(proxy);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: class AnnotationDefaultCompleter extends AnnotationDeproxy implements Annotate.Worker {
aoqi@0: final MethodSymbol sym;
aoqi@0: final Attribute value;
aoqi@0: final JavaFileObject classFile = currentClassFile;
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return " ClassReader store default for " + sym.owner + "." + sym + " is " + value;
aoqi@0: }
aoqi@0: AnnotationDefaultCompleter(MethodSymbol sym, Attribute value) {
aoqi@0: this.sym = sym;
aoqi@0: this.value = value;
aoqi@0: }
aoqi@0: // implement Annotate.Worker.run()
aoqi@0: public void run() {
aoqi@0: JavaFileObject previousClassFile = currentClassFile;
aoqi@0: try {
aoqi@0: // Reset the interim value set earlier in
aoqi@0: // attachAnnotationDefault().
aoqi@0: sym.defaultValue = null;
aoqi@0: currentClassFile = classFile;
aoqi@0: sym.defaultValue = deproxy(sym.type.getReturnType(), value);
aoqi@0: } finally {
aoqi@0: currentClassFile = previousClassFile;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: class AnnotationCompleter extends AnnotationDeproxy implements Annotate.Worker {
aoqi@0: final Symbol sym;
aoqi@0: final List l;
aoqi@0: final JavaFileObject classFile;
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return " ClassReader annotate " + sym.owner + "." + sym + " with " + l;
aoqi@0: }
aoqi@0: AnnotationCompleter(Symbol sym, List l) {
aoqi@0: this.sym = sym;
aoqi@0: this.l = l;
aoqi@0: this.classFile = currentClassFile;
aoqi@0: }
aoqi@0: // implement Annotate.Worker.run()
aoqi@0: public void run() {
aoqi@0: JavaFileObject previousClassFile = currentClassFile;
aoqi@0: try {
aoqi@0: currentClassFile = classFile;
aoqi@0: List newList = deproxyCompoundList(l);
aoqi@0: if (sym.annotationsPendingCompletion()) {
aoqi@0: sym.setDeclarationAttributes(newList);
aoqi@0: } else {
aoqi@0: sym.appendAttributes(newList);
aoqi@0: }
aoqi@0: } finally {
aoqi@0: currentClassFile = previousClassFile;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: class TypeAnnotationCompleter extends AnnotationCompleter {
aoqi@0:
aoqi@0: List proxies;
aoqi@0:
aoqi@0: TypeAnnotationCompleter(Symbol sym,
aoqi@0: List proxies) {
aoqi@0: super(sym, List.nil());
aoqi@0: this.proxies = proxies;
aoqi@0: }
aoqi@0:
aoqi@0: List deproxyTypeCompoundList(List proxies) {
aoqi@0: ListBuffer buf = new ListBuffer<>();
aoqi@0: for (TypeAnnotationProxy proxy: proxies) {
aoqi@0: Attribute.Compound compound = deproxyCompound(proxy.compound);
aoqi@0: Attribute.TypeCompound typeCompound = new Attribute.TypeCompound(compound, proxy.position);
aoqi@0: buf.add(typeCompound);
aoqi@0: }
aoqi@0: return buf.toList();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public void run() {
aoqi@0: JavaFileObject previousClassFile = currentClassFile;
aoqi@0: try {
aoqi@0: currentClassFile = classFile;
aoqi@0: List newList = deproxyTypeCompoundList(proxies);
aoqi@0: sym.setTypeAttributes(newList.prependList(sym.getRawTypeAttributes()));
aoqi@0: } finally {
aoqi@0: currentClassFile = previousClassFile;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Reading Symbols
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0: /** Read a field.
aoqi@0: */
aoqi@0: VarSymbol readField() {
aoqi@0: long flags = adjustFieldFlags(nextChar());
aoqi@0: Name name = readName(nextChar());
aoqi@0: Type type = readType(nextChar());
aoqi@0: VarSymbol v = new VarSymbol(flags, name, type, currentOwner);
aoqi@0: readMemberAttrs(v);
aoqi@0: return v;
aoqi@0: }
aoqi@0:
aoqi@0: /** Read a method.
aoqi@0: */
aoqi@0: MethodSymbol readMethod() {
aoqi@0: long flags = adjustMethodFlags(nextChar());
aoqi@0: Name name = readName(nextChar());
aoqi@0: Type type = readType(nextChar());
aoqi@0: if (currentOwner.isInterface() &&
aoqi@0: (flags & ABSTRACT) == 0 && !name.equals(names.clinit)) {
aoqi@0: if (majorVersion > Target.JDK1_8.majorVersion ||
aoqi@0: (majorVersion == Target.JDK1_8.majorVersion && minorVersion >= Target.JDK1_8.minorVersion)) {
aoqi@0: if ((flags & STATIC) == 0) {
aoqi@0: currentOwner.flags_field |= DEFAULT;
aoqi@0: flags |= DEFAULT | ABSTRACT;
aoqi@0: }
aoqi@0: } else {
aoqi@0: //protect against ill-formed classfiles
aoqi@0: throw badClassFile((flags & STATIC) == 0 ? "invalid.default.interface" : "invalid.static.interface",
aoqi@0: Integer.toString(majorVersion),
aoqi@0: Integer.toString(minorVersion));
aoqi@0: }
aoqi@0: }
aoqi@0: if (name == names.init && currentOwner.hasOuterInstance()) {
aoqi@0: // Sometimes anonymous classes don't have an outer
aoqi@0: // instance, however, there is no reliable way to tell so
aoqi@0: // we never strip this$n
aoqi@0: if (!currentOwner.name.isEmpty())
aoqi@0: type = new MethodType(adjustMethodParams(flags, type.getParameterTypes()),
aoqi@0: type.getReturnType(),
aoqi@0: type.getThrownTypes(),
aoqi@0: syms.methodClass);
aoqi@0: }
aoqi@0: MethodSymbol m = new MethodSymbol(flags, name, type, currentOwner);
aoqi@0: if (types.isSignaturePolymorphic(m)) {
aoqi@0: m.flags_field |= SIGNATURE_POLYMORPHIC;
aoqi@0: }
aoqi@0: if (saveParameterNames)
aoqi@0: initParameterNames(m);
aoqi@0: Symbol prevOwner = currentOwner;
aoqi@0: currentOwner = m;
aoqi@0: try {
aoqi@0: readMemberAttrs(m);
aoqi@0: } finally {
aoqi@0: currentOwner = prevOwner;
aoqi@0: }
aoqi@0: if (saveParameterNames)
aoqi@0: setParameterNames(m, type);
aoqi@0: return m;
aoqi@0: }
aoqi@0:
aoqi@0: private List adjustMethodParams(long flags, List args) {
aoqi@0: boolean isVarargs = (flags & VARARGS) != 0;
aoqi@0: if (isVarargs) {
aoqi@0: Type varargsElem = args.last();
aoqi@0: ListBuffer adjustedArgs = new ListBuffer<>();
aoqi@0: for (Type t : args) {
aoqi@0: adjustedArgs.append(t != varargsElem ?
aoqi@0: t :
aoqi@0: ((ArrayType)t).makeVarargs());
aoqi@0: }
aoqi@0: args = adjustedArgs.toList();
aoqi@0: }
aoqi@0: return args.tail;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Init the parameter names array.
aoqi@0: * Parameter names are currently inferred from the names in the
aoqi@0: * LocalVariableTable attributes of a Code attribute.
aoqi@0: * (Note: this means parameter names are currently not available for
aoqi@0: * methods without a Code attribute.)
aoqi@0: * This method initializes an array in which to store the name indexes
aoqi@0: * of parameter names found in LocalVariableTable attributes. It is
aoqi@0: * slightly supersized to allow for additional slots with a start_pc of 0.
aoqi@0: */
aoqi@0: void initParameterNames(MethodSymbol sym) {
aoqi@0: // make allowance for synthetic parameters.
aoqi@0: final int excessSlots = 4;
aoqi@0: int expectedParameterSlots =
aoqi@0: Code.width(sym.type.getParameterTypes()) + excessSlots;
aoqi@0: if (parameterNameIndices == null
aoqi@0: || parameterNameIndices.length < expectedParameterSlots) {
aoqi@0: parameterNameIndices = new int[expectedParameterSlots];
aoqi@0: } else
aoqi@0: Arrays.fill(parameterNameIndices, 0);
aoqi@0: haveParameterNameIndices = false;
aoqi@0: sawMethodParameters = false;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the parameter names for a symbol from the name index in the
aoqi@0: * parameterNameIndicies array. The type of the symbol may have changed
aoqi@0: * while reading the method attributes (see the Signature attribute).
aoqi@0: * This may be because of generic information or because anonymous
aoqi@0: * synthetic parameters were added. The original type (as read from
aoqi@0: * the method descriptor) is used to help guess the existence of
aoqi@0: * anonymous synthetic parameters.
aoqi@0: * On completion, sym.savedParameter names will either be null (if
aoqi@0: * no parameter names were found in the class file) or will be set to a
aoqi@0: * list of names, one per entry in sym.type.getParameterTypes, with
aoqi@0: * any missing names represented by the empty name.
aoqi@0: */
aoqi@0: void setParameterNames(MethodSymbol sym, Type jvmType) {
aoqi@0: // if no names were found in the class file, there's nothing more to do
aoqi@0: if (!haveParameterNameIndices)
aoqi@0: return;
aoqi@0: // If we get parameter names from MethodParameters, then we
aoqi@0: // don't need to skip.
aoqi@0: int firstParam = 0;
aoqi@0: if (!sawMethodParameters) {
aoqi@0: firstParam = ((sym.flags() & STATIC) == 0) ? 1 : 0;
aoqi@0: // the code in readMethod may have skipped the first
aoqi@0: // parameter when setting up the MethodType. If so, we
aoqi@0: // make a corresponding allowance here for the position of
aoqi@0: // the first parameter. Note that this assumes the
aoqi@0: // skipped parameter has a width of 1 -- i.e. it is not
aoqi@0: // a double width type (long or double.)
aoqi@0: if (sym.name == names.init && currentOwner.hasOuterInstance()) {
aoqi@0: // Sometimes anonymous classes don't have an outer
aoqi@0: // instance, however, there is no reliable way to tell so
aoqi@0: // we never strip this$n
aoqi@0: if (!currentOwner.name.isEmpty())
aoqi@0: firstParam += 1;
aoqi@0: }
aoqi@0:
aoqi@0: if (sym.type != jvmType) {
aoqi@0: // reading the method attributes has caused the
aoqi@0: // symbol's type to be changed. (i.e. the Signature
aoqi@0: // attribute.) This may happen if there are hidden
aoqi@0: // (synthetic) parameters in the descriptor, but not
aoqi@0: // in the Signature. The position of these hidden
aoqi@0: // parameters is unspecified; for now, assume they are
aoqi@0: // at the beginning, and so skip over them. The
aoqi@0: // primary case for this is two hidden parameters
aoqi@0: // passed into Enum constructors.
aoqi@0: int skip = Code.width(jvmType.getParameterTypes())
aoqi@0: - Code.width(sym.type.getParameterTypes());
aoqi@0: firstParam += skip;
aoqi@0: }
aoqi@0: }
aoqi@0: List paramNames = List.nil();
aoqi@0: int index = firstParam;
aoqi@0: for (Type t: sym.type.getParameterTypes()) {
aoqi@0: int nameIdx = (index < parameterNameIndices.length
aoqi@0: ? parameterNameIndices[index] : 0);
aoqi@0: Name name = nameIdx == 0 ? names.empty : readName(nameIdx);
aoqi@0: paramNames = paramNames.prepend(name);
aoqi@0: index += Code.width(t);
aoqi@0: }
aoqi@0: sym.savedParameterNames = paramNames.reverse();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * skip n bytes
aoqi@0: */
aoqi@0: void skipBytes(int n) {
aoqi@0: bp = bp + n;
aoqi@0: }
aoqi@0:
aoqi@0: /** Skip a field or method
aoqi@0: */
aoqi@0: void skipMember() {
aoqi@0: bp = bp + 6;
aoqi@0: char ac = nextChar();
aoqi@0: for (int i = 0; i < ac; i++) {
aoqi@0: bp = bp + 2;
aoqi@0: int attrLen = nextInt();
aoqi@0: bp = bp + attrLen;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Enter type variables of this classtype and all enclosing ones in
aoqi@0: * `typevars'.
aoqi@0: */
aoqi@0: protected void enterTypevars(Type t) {
aoqi@0: if (t.getEnclosingType() != null && t.getEnclosingType().hasTag(CLASS))
aoqi@0: enterTypevars(t.getEnclosingType());
aoqi@0: for (List xs = t.getTypeArguments(); xs.nonEmpty(); xs = xs.tail)
aoqi@0: typevars.enter(xs.head.tsym);
aoqi@0: }
aoqi@0:
aoqi@0: protected void enterTypevars(Symbol sym) {
aoqi@0: if (sym.owner.kind == MTH) {
aoqi@0: enterTypevars(sym.owner);
aoqi@0: enterTypevars(sym.owner.owner);
aoqi@0: }
aoqi@0: enterTypevars(sym.type);
aoqi@0: }
aoqi@0:
aoqi@0: /** Read contents of a given class symbol `c'. Both external and internal
aoqi@0: * versions of an inner class are read.
aoqi@0: */
aoqi@0: void readClass(ClassSymbol c) {
aoqi@0: ClassType ct = (ClassType)c.type;
aoqi@0:
aoqi@0: // allocate scope for members
aoqi@0: c.members_field = new Scope(c);
aoqi@0:
aoqi@0: // prepare type variable table
aoqi@0: typevars = typevars.dup(currentOwner);
aoqi@0: if (ct.getEnclosingType().hasTag(CLASS))
aoqi@0: enterTypevars(ct.getEnclosingType());
aoqi@0:
aoqi@0: // read flags, or skip if this is an inner class
aoqi@0: long flags = adjustClassFlags(nextChar());
aoqi@0: if (c.owner.kind == PCK) c.flags_field = flags;
aoqi@0:
aoqi@0: // read own class name and check that it matches
aoqi@0: ClassSymbol self = readClassSymbol(nextChar());
aoqi@0: if (c != self)
aoqi@0: throw badClassFile("class.file.wrong.class",
aoqi@0: self.flatname);
aoqi@0:
aoqi@0: // class attributes must be read before class
aoqi@0: // skip ahead to read class attributes
aoqi@0: int startbp = bp;
aoqi@0: nextChar();
aoqi@0: char interfaceCount = nextChar();
aoqi@0: bp += interfaceCount * 2;
aoqi@0: char fieldCount = nextChar();
aoqi@0: for (int i = 0; i < fieldCount; i++) skipMember();
aoqi@0: char methodCount = nextChar();
aoqi@0: for (int i = 0; i < methodCount; i++) skipMember();
aoqi@0: readClassAttrs(c);
aoqi@0:
aoqi@0: if (readAllOfClassFile) {
aoqi@0: for (int i = 1; i < poolObj.length; i++) readPool(i);
aoqi@0: c.pool = new Pool(poolObj.length, poolObj, types);
aoqi@0: }
aoqi@0:
aoqi@0: // reset and read rest of classinfo
aoqi@0: bp = startbp;
aoqi@0: int n = nextChar();
aoqi@0: if (ct.supertype_field == null)
aoqi@0: ct.supertype_field = (n == 0)
aoqi@0: ? Type.noType
aoqi@0: : readClassSymbol(n).erasure(types);
aoqi@0: n = nextChar();
aoqi@0: List is = List.nil();
aoqi@0: for (int i = 0; i < n; i++) {
aoqi@0: Type _inter = readClassSymbol(nextChar()).erasure(types);
aoqi@0: is = is.prepend(_inter);
aoqi@0: }
aoqi@0: if (ct.interfaces_field == null)
aoqi@0: ct.interfaces_field = is.reverse();
aoqi@0:
aoqi@0: Assert.check(fieldCount == nextChar());
aoqi@0: for (int i = 0; i < fieldCount; i++) enterMember(c, readField());
aoqi@0: Assert.check(methodCount == nextChar());
aoqi@0: for (int i = 0; i < methodCount; i++) enterMember(c, readMethod());
aoqi@0:
aoqi@0: typevars = typevars.leave();
aoqi@0: }
aoqi@0:
aoqi@0: /** Read inner class info. For each inner/outer pair allocate a
aoqi@0: * member class.
aoqi@0: */
aoqi@0: void readInnerClasses(ClassSymbol c) {
aoqi@0: int n = nextChar();
aoqi@0: for (int i = 0; i < n; i++) {
aoqi@0: nextChar(); // skip inner class symbol
aoqi@0: ClassSymbol outer = readClassSymbol(nextChar());
aoqi@0: Name name = readName(nextChar());
aoqi@0: if (name == null) name = names.empty;
aoqi@0: long flags = adjustClassFlags(nextChar());
aoqi@0: if (outer != null) { // we have a member class
aoqi@0: if (name == names.empty)
aoqi@0: name = names.one;
aoqi@0: ClassSymbol member = enterClass(name, outer);
aoqi@0: if ((flags & STATIC) == 0) {
aoqi@0: ((ClassType)member.type).setEnclosingType(outer.type);
aoqi@0: if (member.erasure_field != null)
aoqi@0: ((ClassType)member.erasure_field).setEnclosingType(types.erasure(outer.type));
aoqi@0: }
aoqi@0: if (c == outer) {
aoqi@0: member.flags_field = flags;
aoqi@0: enterMember(c, member);
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Read a class file.
aoqi@0: */
aoqi@0: private void readClassFile(ClassSymbol c) throws IOException {
aoqi@0: int magic = nextInt();
aoqi@0: if (magic != JAVA_MAGIC)
aoqi@0: throw badClassFile("illegal.start.of.class.file");
aoqi@0:
aoqi@0: minorVersion = nextChar();
aoqi@0: majorVersion = nextChar();
aoqi@0: int maxMajor = Target.MAX().majorVersion;
aoqi@0: int maxMinor = Target.MAX().minorVersion;
aoqi@0: if (majorVersion > maxMajor ||
aoqi@0: majorVersion * 1000 + minorVersion <
aoqi@0: Target.MIN().majorVersion * 1000 + Target.MIN().minorVersion)
aoqi@0: {
aoqi@0: if (majorVersion == (maxMajor + 1))
aoqi@0: log.warning("big.major.version",
aoqi@0: currentClassFile,
aoqi@0: majorVersion,
aoqi@0: maxMajor);
aoqi@0: else
aoqi@0: throw badClassFile("wrong.version",
aoqi@0: Integer.toString(majorVersion),
aoqi@0: Integer.toString(minorVersion),
aoqi@0: Integer.toString(maxMajor),
aoqi@0: Integer.toString(maxMinor));
aoqi@0: }
aoqi@0: else if (checkClassFile &&
aoqi@0: majorVersion == maxMajor &&
aoqi@0: minorVersion > maxMinor)
aoqi@0: {
aoqi@0: printCCF("found.later.version",
aoqi@0: Integer.toString(minorVersion));
aoqi@0: }
aoqi@0: indexPool();
aoqi@0: if (signatureBuffer.length < bp) {
aoqi@0: int ns = Integer.highestOneBit(bp) << 1;
aoqi@0: signatureBuffer = new byte[ns];
aoqi@0: }
aoqi@0: readClass(c);
aoqi@0: }
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Adjusting flags
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0: long adjustFieldFlags(long flags) {
aoqi@0: return flags;
aoqi@0: }
aoqi@0: long adjustMethodFlags(long flags) {
aoqi@0: if ((flags & ACC_BRIDGE) != 0) {
aoqi@0: flags &= ~ACC_BRIDGE;
aoqi@0: flags |= BRIDGE;
aoqi@0: if (!allowGenerics)
aoqi@0: flags &= ~SYNTHETIC;
aoqi@0: }
aoqi@0: if ((flags & ACC_VARARGS) != 0) {
aoqi@0: flags &= ~ACC_VARARGS;
aoqi@0: flags |= VARARGS;
aoqi@0: }
aoqi@0: return flags;
aoqi@0: }
aoqi@0: long adjustClassFlags(long flags) {
aoqi@0: return flags & ~ACC_SUPER; // SUPER and SYNCHRONIZED bits overloaded
aoqi@0: }
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Loading Classes
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0: /** Define a new class given its name and owner.
aoqi@0: */
aoqi@0: public ClassSymbol defineClass(Name name, Symbol owner) {
aoqi@0: ClassSymbol c = new ClassSymbol(0, name, owner);
aoqi@0: if (owner.kind == PCK)
aoqi@0: Assert.checkNull(classes.get(c.flatname), c);
aoqi@0: c.completer = thisCompleter;
aoqi@0: return c;
aoqi@0: }
aoqi@0:
aoqi@0: /** Create a new toplevel or member class symbol with given name
aoqi@0: * and owner and enter in `classes' unless already there.
aoqi@0: */
aoqi@0: public ClassSymbol enterClass(Name name, TypeSymbol owner) {
aoqi@0: Name flatname = TypeSymbol.formFlatName(name, owner);
aoqi@0: ClassSymbol c = classes.get(flatname);
aoqi@0: if (c == null) {
aoqi@0: c = defineClass(name, owner);
aoqi@0: classes.put(flatname, c);
aoqi@0: } else if ((c.name != name || c.owner != owner) && owner.kind == TYP && c.owner.kind == PCK) {
aoqi@0: // reassign fields of classes that might have been loaded with
aoqi@0: // their flat names.
aoqi@0: c.owner.members().remove(c);
aoqi@0: c.name = name;
aoqi@0: c.owner = owner;
aoqi@0: c.fullname = ClassSymbol.formFullName(name, owner);
aoqi@0: }
aoqi@0: return c;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Creates a new toplevel class symbol with given flat name and
aoqi@0: * given class (or source) file.
aoqi@0: *
aoqi@0: * @param flatName a fully qualified binary class name
aoqi@0: * @param classFile the class file or compilation unit defining
aoqi@0: * the class (may be {@code null})
aoqi@0: * @return a newly created class symbol
aoqi@0: * @throws AssertionError if the class symbol already exists
aoqi@0: */
aoqi@0: public ClassSymbol enterClass(Name flatName, JavaFileObject classFile) {
aoqi@0: ClassSymbol cs = classes.get(flatName);
aoqi@0: if (cs != null) {
aoqi@0: String msg = Log.format("%s: completer = %s; class file = %s; source file = %s",
aoqi@0: cs.fullname,
aoqi@0: cs.completer,
aoqi@0: cs.classfile,
aoqi@0: cs.sourcefile);
aoqi@0: throw new AssertionError(msg);
aoqi@0: }
aoqi@0: Name packageName = Convert.packagePart(flatName);
aoqi@0: PackageSymbol owner = packageName.isEmpty()
aoqi@0: ? syms.unnamedPackage
aoqi@0: : enterPackage(packageName);
aoqi@0: cs = defineClass(Convert.shortName(flatName), owner);
aoqi@0: cs.classfile = classFile;
aoqi@0: classes.put(flatName, cs);
aoqi@0: return cs;
aoqi@0: }
aoqi@0:
aoqi@0: /** Create a new member or toplevel class symbol with given flat name
aoqi@0: * and enter in `classes' unless already there.
aoqi@0: */
aoqi@0: public ClassSymbol enterClass(Name flatname) {
aoqi@0: ClassSymbol c = classes.get(flatname);
aoqi@0: if (c == null)
aoqi@0: return enterClass(flatname, (JavaFileObject)null);
aoqi@0: else
aoqi@0: return c;
aoqi@0: }
aoqi@0:
aoqi@0: /** Completion for classes to be loaded. Before a class is loaded
aoqi@0: * we make sure its enclosing class (if any) is loaded.
aoqi@0: */
aoqi@0: private void complete(Symbol sym) throws CompletionFailure {
aoqi@0: if (sym.kind == TYP) {
aoqi@0: ClassSymbol c = (ClassSymbol)sym;
aoqi@0: c.members_field = new Scope.ErrorScope(c); // make sure it's always defined
aoqi@0: annotate.enterStart();
aoqi@0: try {
aoqi@0: completeOwners(c.owner);
aoqi@0: completeEnclosing(c);
aoqi@0: } finally {
aoqi@0: // The flush needs to happen only after annotations
aoqi@0: // are filled in.
aoqi@0: annotate.enterDoneWithoutFlush();
aoqi@0: }
aoqi@0: fillIn(c);
aoqi@0: } else if (sym.kind == PCK) {
aoqi@0: PackageSymbol p = (PackageSymbol)sym;
aoqi@0: try {
aoqi@0: fillIn(p);
aoqi@0: } catch (IOException ex) {
aoqi@0: throw new CompletionFailure(sym, ex.getLocalizedMessage()).initCause(ex);
aoqi@0: }
aoqi@0: }
aoqi@0: if (!filling)
aoqi@0: annotate.flush(); // finish attaching annotations
aoqi@0: }
aoqi@0:
aoqi@0: /** complete up through the enclosing package. */
aoqi@0: private void completeOwners(Symbol o) {
aoqi@0: if (o.kind != PCK) completeOwners(o.owner);
aoqi@0: o.complete();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Tries to complete lexically enclosing classes if c looks like a
aoqi@0: * nested class. This is similar to completeOwners but handles
aoqi@0: * the situation when a nested class is accessed directly as it is
aoqi@0: * possible with the Tree API or javax.lang.model.*.
aoqi@0: */
aoqi@0: private void completeEnclosing(ClassSymbol c) {
aoqi@0: if (c.owner.kind == PCK) {
aoqi@0: Symbol owner = c.owner;
aoqi@0: for (Name name : Convert.enclosingCandidates(Convert.shortName(c.name))) {
aoqi@0: Symbol encl = owner.members().lookup(name).sym;
aoqi@0: if (encl == null)
aoqi@0: encl = classes.get(TypeSymbol.formFlatName(name, owner));
aoqi@0: if (encl != null)
aoqi@0: encl.complete();
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** We can only read a single class file at a time; this
aoqi@0: * flag keeps track of when we are currently reading a class
aoqi@0: * file.
aoqi@0: */
aoqi@0: private boolean filling = false;
aoqi@0:
aoqi@0: /** Fill in definition of class `c' from corresponding class or
aoqi@0: * source file.
aoqi@0: */
aoqi@0: private void fillIn(ClassSymbol c) {
aoqi@0: if (completionFailureName == c.fullname) {
aoqi@0: throw new CompletionFailure(c, "user-selected completion failure by class name");
aoqi@0: }
aoqi@0: currentOwner = c;
aoqi@0: warnedAttrs.clear();
aoqi@0: JavaFileObject classfile = c.classfile;
aoqi@0: if (classfile != null) {
aoqi@0: JavaFileObject previousClassFile = currentClassFile;
aoqi@0: try {
aoqi@0: if (filling) {
aoqi@0: Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
aoqi@0: }
aoqi@0: currentClassFile = classfile;
aoqi@0: if (verbose) {
aoqi@0: log.printVerbose("loading", currentClassFile.toString());
aoqi@0: }
aoqi@0: if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
aoqi@0: filling = true;
aoqi@0: try {
aoqi@0: bp = 0;
aoqi@0: buf = readInputStream(buf, classfile.openInputStream());
aoqi@0: readClassFile(c);
aoqi@0: if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
aoqi@0: List missing = missingTypeVariables;
aoqi@0: List found = foundTypeVariables;
aoqi@0: missingTypeVariables = List.nil();
aoqi@0: foundTypeVariables = List.nil();
aoqi@0: filling = false;
aoqi@0: ClassType ct = (ClassType)currentOwner.type;
aoqi@0: ct.supertype_field =
aoqi@0: types.subst(ct.supertype_field, missing, found);
aoqi@0: ct.interfaces_field =
aoqi@0: types.subst(ct.interfaces_field, missing, found);
aoqi@0: } else if (missingTypeVariables.isEmpty() !=
aoqi@0: foundTypeVariables.isEmpty()) {
aoqi@0: Name name = missingTypeVariables.head.tsym.name;
aoqi@0: throw badClassFile("undecl.type.var", name);
aoqi@0: }
aoqi@0: } finally {
aoqi@0: missingTypeVariables = List.nil();
aoqi@0: foundTypeVariables = List.nil();
aoqi@0: filling = false;
aoqi@0: }
aoqi@0: } else {
aoqi@0: if (sourceCompleter != null) {
aoqi@0: sourceCompleter.complete(c);
aoqi@0: } else {
aoqi@0: throw new IllegalStateException("Source completer required to read "
aoqi@0: + classfile.toUri());
aoqi@0: }
aoqi@0: }
aoqi@0: return;
aoqi@0: } catch (IOException ex) {
aoqi@0: throw badClassFile("unable.to.access.file", ex.getMessage());
aoqi@0: } finally {
aoqi@0: currentClassFile = previousClassFile;
aoqi@0: }
aoqi@0: } else {
aoqi@0: JCDiagnostic diag =
aoqi@0: diagFactory.fragment("class.file.not.found", c.flatname);
aoqi@0: throw
aoqi@0: newCompletionFailure(c, diag);
aoqi@0: }
aoqi@0: }
aoqi@0: // where
aoqi@0: private static byte[] readInputStream(byte[] buf, InputStream s) throws IOException {
aoqi@0: try {
aoqi@0: buf = ensureCapacity(buf, s.available());
aoqi@0: int r = s.read(buf);
aoqi@0: int bp = 0;
aoqi@0: while (r != -1) {
aoqi@0: bp += r;
aoqi@0: buf = ensureCapacity(buf, bp);
aoqi@0: r = s.read(buf, bp, buf.length - bp);
aoqi@0: }
aoqi@0: return buf;
aoqi@0: } finally {
aoqi@0: try {
aoqi@0: s.close();
aoqi@0: } catch (IOException e) {
aoqi@0: /* Ignore any errors, as this stream may have already
aoqi@0: * thrown a related exception which is the one that
aoqi@0: * should be reported.
aoqi@0: */
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: /*
aoqi@0: * ensureCapacity will increase the buffer as needed, taking note that
aoqi@0: * the new buffer will always be greater than the needed and never
aoqi@0: * exactly equal to the needed size or bp. If equal then the read (above)
aoqi@0: * will infinitely loop as buf.length - bp == 0.
aoqi@0: */
aoqi@0: private static byte[] ensureCapacity(byte[] buf, int needed) {
aoqi@0: if (buf.length <= needed) {
aoqi@0: byte[] old = buf;
aoqi@0: buf = new byte[Integer.highestOneBit(needed) << 1];
aoqi@0: System.arraycopy(old, 0, buf, 0, old.length);
aoqi@0: }
aoqi@0: return buf;
aoqi@0: }
aoqi@0: /** Static factory for CompletionFailure objects.
aoqi@0: * In practice, only one can be used at a time, so we share one
aoqi@0: * to reduce the expense of allocating new exception objects.
aoqi@0: */
aoqi@0: private CompletionFailure newCompletionFailure(TypeSymbol c,
aoqi@0: JCDiagnostic diag) {
aoqi@0: if (!cacheCompletionFailure) {
aoqi@0: // log.warning("proc.messager",
aoqi@0: // Log.getLocalizedString("class.file.not.found", c.flatname));
aoqi@0: // c.debug.printStackTrace();
aoqi@0: return new CompletionFailure(c, diag);
aoqi@0: } else {
aoqi@0: CompletionFailure result = cachedCompletionFailure;
aoqi@0: result.sym = c;
aoqi@0: result.diag = diag;
aoqi@0: return result;
aoqi@0: }
aoqi@0: }
aoqi@0: private CompletionFailure cachedCompletionFailure =
aoqi@0: new CompletionFailure(null, (JCDiagnostic) null);
aoqi@0: {
aoqi@0: cachedCompletionFailure.setStackTrace(new StackTraceElement[0]);
aoqi@0: }
aoqi@0:
aoqi@0: /** Load a toplevel class with given fully qualified name
aoqi@0: * The class is entered into `classes' only if load was successful.
aoqi@0: */
aoqi@0: public ClassSymbol loadClass(Name flatname) throws CompletionFailure {
aoqi@0: boolean absent = classes.get(flatname) == null;
aoqi@0: ClassSymbol c = enterClass(flatname);
aoqi@0: if (c.members_field == null && c.completer != null) {
aoqi@0: try {
aoqi@0: c.complete();
aoqi@0: } catch (CompletionFailure ex) {
aoqi@0: if (absent) classes.remove(flatname);
aoqi@0: throw ex;
aoqi@0: }
aoqi@0: }
aoqi@0: return c;
aoqi@0: }
aoqi@0:
aoqi@0: /************************************************************************
aoqi@0: * Loading Packages
aoqi@0: ***********************************************************************/
aoqi@0:
aoqi@0: /** Check to see if a package exists, given its fully qualified name.
aoqi@0: */
aoqi@0: public boolean packageExists(Name fullname) {
aoqi@0: return enterPackage(fullname).exists();
aoqi@0: }
aoqi@0:
aoqi@0: /** Make a package, given its fully qualified name.
aoqi@0: */
aoqi@0: public PackageSymbol enterPackage(Name fullname) {
aoqi@0: PackageSymbol p = packages.get(fullname);
aoqi@0: if (p == null) {
aoqi@0: Assert.check(!fullname.isEmpty(), "rootPackage missing!");
aoqi@0: p = new PackageSymbol(
aoqi@0: Convert.shortName(fullname),
aoqi@0: enterPackage(Convert.packagePart(fullname)));
aoqi@0: p.completer = thisCompleter;
aoqi@0: packages.put(fullname, p);
aoqi@0: }
aoqi@0: return p;
aoqi@0: }
aoqi@0:
aoqi@0: /** Make a package, given its unqualified name and enclosing package.
aoqi@0: */
aoqi@0: public PackageSymbol enterPackage(Name name, PackageSymbol owner) {
aoqi@0: return enterPackage(TypeSymbol.formFullName(name, owner));
aoqi@0: }
aoqi@0:
aoqi@0: /** Include class corresponding to given class file in package,
aoqi@0: * unless (1) we already have one the same kind (.class or .java), or
aoqi@0: * (2) we have one of the other kind, and the given class file
aoqi@0: * is older.
aoqi@0: */
aoqi@0: protected void includeClassFile(PackageSymbol p, JavaFileObject file) {
aoqi@0: if ((p.flags_field & EXISTS) == 0)
aoqi@0: for (Symbol q = p; q != null && q.kind == PCK; q = q.owner)
aoqi@0: q.flags_field |= EXISTS;
aoqi@0: JavaFileObject.Kind kind = file.getKind();
aoqi@0: int seen;
aoqi@0: if (kind == JavaFileObject.Kind.CLASS)
aoqi@0: seen = CLASS_SEEN;
aoqi@0: else
aoqi@0: seen = SOURCE_SEEN;
aoqi@0: String binaryName = fileManager.inferBinaryName(currentLoc, file);
aoqi@0: int lastDot = binaryName.lastIndexOf(".");
aoqi@0: Name classname = names.fromString(binaryName.substring(lastDot + 1));
aoqi@0: boolean isPkgInfo = classname == names.package_info;
aoqi@0: ClassSymbol c = isPkgInfo
aoqi@0: ? p.package_info
aoqi@0: : (ClassSymbol) p.members_field.lookup(classname).sym;
aoqi@0: if (c == null) {
aoqi@0: c = enterClass(classname, p);
aoqi@0: if (c.classfile == null) // only update the file if's it's newly created
aoqi@0: c.classfile = file;
aoqi@0: if (isPkgInfo) {
aoqi@0: p.package_info = c;
aoqi@0: } else {
aoqi@0: if (c.owner == p) // it might be an inner class
aoqi@0: p.members_field.enter(c);
aoqi@0: }
aoqi@0: } else if (c.classfile != null && (c.flags_field & seen) == 0) {
aoqi@0: // if c.classfile == null, we are currently compiling this class
aoqi@0: // and no further action is necessary.
aoqi@0: // if (c.flags_field & seen) != 0, we have already encountered
aoqi@0: // a file of the same kind; again no further action is necessary.
aoqi@0: if ((c.flags_field & (CLASS_SEEN | SOURCE_SEEN)) != 0)
aoqi@0: c.classfile = preferredFileObject(file, c.classfile);
aoqi@0: }
aoqi@0: c.flags_field |= seen;
aoqi@0: }
aoqi@0:
aoqi@0: /** Implement policy to choose to derive information from a source
aoqi@0: * file or a class file when both are present. May be overridden
aoqi@0: * by subclasses.
aoqi@0: */
aoqi@0: protected JavaFileObject preferredFileObject(JavaFileObject a,
aoqi@0: JavaFileObject b) {
aoqi@0:
aoqi@0: if (preferSource)
aoqi@0: return (a.getKind() == JavaFileObject.Kind.SOURCE) ? a : b;
aoqi@0: else {
aoqi@0: long adate = a.getLastModified();
aoqi@0: long bdate = b.getLastModified();
aoqi@0: // 6449326: policy for bad lastModifiedTime in ClassReader
aoqi@0: //assert adate >= 0 && bdate >= 0;
aoqi@0: return (adate > bdate) ? a : b;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * specifies types of files to be read when filling in a package symbol
aoqi@0: */
aoqi@0: protected EnumSet getPackageFileKinds() {
aoqi@0: return EnumSet.of(JavaFileObject.Kind.CLASS, JavaFileObject.Kind.SOURCE);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * this is used to support javadoc
aoqi@0: */
aoqi@0: protected void extraFileActions(PackageSymbol pack, JavaFileObject fe) {
aoqi@0: }
aoqi@0:
aoqi@0: protected Location currentLoc; // FIXME
aoqi@0:
aoqi@0: private boolean verbosePath = true;
aoqi@0:
aoqi@0: /** Load directory of package into members scope.
aoqi@0: */
aoqi@0: private void fillIn(PackageSymbol p) throws IOException {
aoqi@0: if (p.members_field == null) p.members_field = new Scope(p);
aoqi@0: String packageName = p.fullname.toString();
aoqi@0:
aoqi@0: Set kinds = getPackageFileKinds();
aoqi@0:
aoqi@0: fillIn(p, PLATFORM_CLASS_PATH,
aoqi@0: fileManager.list(PLATFORM_CLASS_PATH,
aoqi@0: packageName,
aoqi@0: EnumSet.of(JavaFileObject.Kind.CLASS),
aoqi@0: false));
aoqi@0:
aoqi@0: Set classKinds = EnumSet.copyOf(kinds);
aoqi@0: classKinds.remove(JavaFileObject.Kind.SOURCE);
aoqi@0: boolean wantClassFiles = !classKinds.isEmpty();
aoqi@0:
aoqi@0: Set sourceKinds = EnumSet.copyOf(kinds);
aoqi@0: sourceKinds.remove(JavaFileObject.Kind.CLASS);
aoqi@0: boolean wantSourceFiles = !sourceKinds.isEmpty();
aoqi@0:
aoqi@0: boolean haveSourcePath = fileManager.hasLocation(SOURCE_PATH);
aoqi@0:
aoqi@0: if (verbose && verbosePath) {
aoqi@0: if (fileManager instanceof StandardJavaFileManager) {
aoqi@0: StandardJavaFileManager fm = (StandardJavaFileManager)fileManager;
aoqi@0: if (haveSourcePath && wantSourceFiles) {
aoqi@0: List path = List.nil();
aoqi@0: for (File file : fm.getLocation(SOURCE_PATH)) {
aoqi@0: path = path.prepend(file);
aoqi@0: }
aoqi@0: log.printVerbose("sourcepath", path.reverse().toString());
aoqi@0: } else if (wantSourceFiles) {
aoqi@0: List path = List.nil();
aoqi@0: for (File file : fm.getLocation(CLASS_PATH)) {
aoqi@0: path = path.prepend(file);
aoqi@0: }
aoqi@0: log.printVerbose("sourcepath", path.reverse().toString());
aoqi@0: }
aoqi@0: if (wantClassFiles) {
aoqi@0: List path = List.nil();
aoqi@0: for (File file : fm.getLocation(PLATFORM_CLASS_PATH)) {
aoqi@0: path = path.prepend(file);
aoqi@0: }
aoqi@0: for (File file : fm.getLocation(CLASS_PATH)) {
aoqi@0: path = path.prepend(file);
aoqi@0: }
aoqi@0: log.printVerbose("classpath", path.reverse().toString());
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: if (wantSourceFiles && !haveSourcePath) {
aoqi@0: fillIn(p, CLASS_PATH,
aoqi@0: fileManager.list(CLASS_PATH,
aoqi@0: packageName,
aoqi@0: kinds,
aoqi@0: false));
aoqi@0: } else {
aoqi@0: if (wantClassFiles)
aoqi@0: fillIn(p, CLASS_PATH,
aoqi@0: fileManager.list(CLASS_PATH,
aoqi@0: packageName,
aoqi@0: classKinds,
aoqi@0: false));
aoqi@0: if (wantSourceFiles)
aoqi@0: fillIn(p, SOURCE_PATH,
aoqi@0: fileManager.list(SOURCE_PATH,
aoqi@0: packageName,
aoqi@0: sourceKinds,
aoqi@0: false));
aoqi@0: }
aoqi@0: verbosePath = false;
aoqi@0: }
aoqi@0: // where
aoqi@0: private void fillIn(PackageSymbol p,
aoqi@0: Location location,
aoqi@0: Iterable files)
aoqi@0: {
aoqi@0: currentLoc = location;
aoqi@0: for (JavaFileObject fo : files) {
aoqi@0: switch (fo.getKind()) {
aoqi@0: case CLASS:
aoqi@0: case SOURCE: {
aoqi@0: // TODO pass binaryName to includeClassFile
aoqi@0: String binaryName = fileManager.inferBinaryName(currentLoc, fo);
aoqi@0: String simpleName = binaryName.substring(binaryName.lastIndexOf(".") + 1);
aoqi@0: if (SourceVersion.isIdentifier(simpleName) ||
aoqi@0: simpleName.equals("package-info"))
aoqi@0: includeClassFile(p, fo);
aoqi@0: break;
aoqi@0: }
aoqi@0: default:
aoqi@0: extraFileActions(p, fo);
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /** Output for "-checkclassfile" option.
aoqi@0: * @param key The key to look up the correct internationalized string.
aoqi@0: * @param arg An argument for substitution into the output string.
aoqi@0: */
aoqi@0: private void printCCF(String key, Object arg) {
aoqi@0: log.printLines(key, arg);
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: public interface SourceCompleter {
aoqi@0: void complete(ClassSymbol sym)
aoqi@0: throws CompletionFailure;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * A subclass of JavaFileObject for the sourcefile attribute found in a classfile.
aoqi@0: * The attribute is only the last component of the original filename, so is unlikely
aoqi@0: * to be valid as is, so operations other than those to access the name throw
aoqi@0: * UnsupportedOperationException
aoqi@0: */
aoqi@0: private static class SourceFileObject extends BaseFileObject {
aoqi@0:
aoqi@0: /** The file's name.
aoqi@0: */
aoqi@0: private Name name;
aoqi@0: private Name flatname;
aoqi@0:
aoqi@0: public SourceFileObject(Name name, Name flatname) {
aoqi@0: super(null); // no file manager; never referenced for this file object
aoqi@0: this.name = name;
aoqi@0: this.flatname = flatname;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public URI toUri() {
aoqi@0: try {
aoqi@0: return new URI(null, name.toString(), null);
aoqi@0: } catch (URISyntaxException e) {
aoqi@0: throw new CannotCreateUriError(name.toString(), e);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public String getName() {
aoqi@0: return name.toString();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public String getShortName() {
aoqi@0: return getName();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public JavaFileObject.Kind getKind() {
aoqi@0: return getKind(getName());
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public InputStream openInputStream() {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public OutputStream openOutputStream() {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public CharBuffer getCharContent(boolean ignoreEncodingErrors) {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Reader openReader(boolean ignoreEncodingErrors) {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Writer openWriter() {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public long getLastModified() {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public boolean delete() {
aoqi@0: throw new UnsupportedOperationException();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: protected String inferBinaryName(Iterable extends File> path) {
aoqi@0: return flatname.toString();
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public boolean isNameCompatible(String simpleName, JavaFileObject.Kind kind) {
aoqi@0: return true; // fail-safe mode
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Check if two file objects are equal.
aoqi@0: * SourceFileObjects are just placeholder objects for the value of a
aoqi@0: * SourceFile attribute, and do not directly represent specific files.
aoqi@0: * Two SourceFileObjects are equal if their names are equal.
aoqi@0: */
aoqi@0: @Override
aoqi@0: public boolean equals(Object other) {
aoqi@0: if (this == other)
aoqi@0: return true;
aoqi@0:
aoqi@0: if (!(other instanceof SourceFileObject))
aoqi@0: return false;
aoqi@0:
aoqi@0: SourceFileObject o = (SourceFileObject) other;
aoqi@0: return name.equals(o.name);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public int hashCode() {
aoqi@0: return name.hashCode();
aoqi@0: }
aoqi@0: }
aoqi@0: }