0N/A/*
2362N/A * Copyright (c) 1994, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.tools.javac;
0N/A
0N/Aimport sun.tools.java.*;
0N/Aimport sun.tools.tree.*;
0N/Aimport sun.tools.tree.CompoundStatement;
0N/Aimport sun.tools.asm.Assembler;
0N/Aimport sun.tools.asm.ConstantPool;
0N/Aimport java.util.Vector;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Iterator;
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.DataOutputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.File;
0N/A
0N/A/**
0N/A * This class represents an Java class as it is read from
0N/A * an Java source file.
0N/A *
0N/A * WARNING: The contents of this source file are not part of any
0N/A * supported API. Code that depends on them does so at its own risk:
0N/A * they are subject to change or removal without notice.
0N/A */
0N/A@Deprecated
0N/Apublic
0N/Aclass SourceClass extends ClassDefinition {
0N/A
0N/A /**
0N/A * The toplevel environment, shared with the parser
0N/A */
0N/A Environment toplevelEnv;
0N/A
0N/A /**
0N/A * The default constructor
0N/A */
0N/A SourceMember defConstructor;
0N/A
0N/A /**
0N/A * The constant pool
0N/A */
0N/A ConstantPool tab = new ConstantPool();
0N/A
0N/A /**
0N/A * The list of class dependencies
0N/A */
0N/A Hashtable deps = new Hashtable(11);
0N/A
0N/A /**
0N/A * The field used to represent "this" in all of my code.
0N/A */
0N/A LocalMember thisArg;
0N/A
0N/A /**
0N/A * Last token of class, as reported by parser.
0N/A */
0N/A long endPosition;
0N/A
0N/A /**
0N/A * Access methods for constructors are distinguished from
0N/A * the constructors themselves by a dummy first argument.
0N/A * A unique type used for this purpose and shared by all
0N/A * constructor access methods within a package-member class is
0N/A * maintained here.
0N/A * <p>
0N/A * This field is null except in an outermost class containing
0N/A * one or more classes needing such an access method.
0N/A */
0N/A private Type dummyArgumentType = null;
0N/A
0N/A /**
0N/A * Constructor
0N/A */
0N/A public SourceClass(Environment env, long where,
0N/A ClassDeclaration declaration, String documentation,
0N/A int modifiers, IdentifierToken superClass,
0N/A IdentifierToken interfaces[],
0N/A SourceClass outerClass, Identifier localName) {
0N/A super(env.getSource(), where,
0N/A declaration, modifiers, superClass, interfaces);
0N/A setOuterClass(outerClass);
0N/A
0N/A this.toplevelEnv = env;
0N/A this.documentation = documentation;
0N/A
0N/A if (ClassDefinition.containsDeprecated(documentation)) {
0N/A this.modifiers |= M_DEPRECATED;
0N/A }
0N/A
0N/A // Check for a package level class which is declared static.
0N/A if (isStatic() && outerClass == null) {
0N/A env.error(where, "static.class", this);
0N/A this.modifiers &=~ M_STATIC;
0N/A }
0N/A
0N/A // Inner classes cannot be static, nor can they be interfaces
0N/A // (which are implicitly static). Static classes and interfaces
0N/A // can only occur as top-level entities.
0N/A //
0N/A // Note that we do not have to check for local classes declared
0N/A // to be static (this is currently caught by the parser) but
0N/A // we check anyway in case the parser is modified to allow this.
0N/A if (isLocal() || (outerClass != null && !outerClass.isTopLevel())) {
0N/A if (isInterface()) {
0N/A env.error(where, "inner.interface");
0N/A } else if (isStatic()) {
0N/A env.error(where, "static.inner.class", this);
0N/A this.modifiers &=~ M_STATIC;
0N/A if (innerClassMember != null) {
0N/A innerClassMember.subModifiers(M_STATIC);
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (isPrivate() && outerClass == null) {
0N/A env.error(where, "private.class", this);
0N/A this.modifiers &=~ M_PRIVATE;
0N/A }
0N/A if (isProtected() && outerClass == null) {
0N/A env.error(where, "protected.class", this);
0N/A this.modifiers &=~ M_PROTECTED;
0N/A }
0N/A /*----*
0N/A if ((isPublic() || isProtected()) && isInsideLocal()) {
0N/A env.error(where, "warn.public.local.class", this);
0N/A }
0N/A *----*/
0N/A
0N/A // maybe define an uplevel "A.this" current instance field
0N/A if (!isTopLevel() && !isLocal()) {
0N/A LocalMember outerArg = ((SourceClass)outerClass).getThisArgument();
0N/A UplevelReference r = getReference(outerArg);
0N/A setOuterMember(r.getLocalField(env));
0N/A }
0N/A
0N/A // Set simple, unmangled local name for a local or anonymous class.
0N/A // NOTE: It would be OK to do this unconditionally, as null is the
0N/A // correct value for a member (non-local) class.
0N/A if (localName != null)
0N/A setLocalName(localName);
0N/A
0N/A // Check for inner class with same simple name as one of
0N/A // its enclosing classes. Note that 'getLocalName' returns
0N/A // the simple, unmangled source-level name of any class.
0N/A // The previous version of this code was not careful to avoid
0N/A // mangled local class names. This version fixes 4047746.
0N/A Identifier thisName = getLocalName();
0N/A if (thisName != idNull) {
0N/A // Test above suppresses error for nested anonymous classes,
0N/A // which have an internal "name", but are not named in source code.
0N/A for (ClassDefinition scope = outerClass; scope != null;
0N/A scope = scope.getOuterClass()) {
0N/A Identifier outerName = scope.getLocalName();
0N/A if (thisName.equals(outerName))
0N/A env.error(where, "inner.redefined", thisName);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return last position in this class.
0N/A * @see #getWhere
0N/A */
0N/A public long getEndPosition() {
0N/A return endPosition;
0N/A }
0N/A
0N/A public void setEndPosition(long endPosition) {
0N/A this.endPosition = endPosition;
0N/A }
0N/A
0N/A
0N/A// JCOV
0N/A /**
0N/A * Return absolute name of source file
0N/A */
0N/A public String getAbsoluteName() {
0N/A String AbsName = ((ClassFile)getSource()).getAbsoluteName();
0N/A
0N/A return AbsName;
0N/A }
0N/A//end JCOV
0N/A
0N/A /**
0N/A * Return imports
0N/A */
0N/A public Imports getImports() {
0N/A return toplevelEnv.getImports();
0N/A }
0N/A
0N/A /**
0N/A * Find or create my "this" argument, which is used for all methods.
0N/A */
0N/A public LocalMember getThisArgument() {
0N/A if (thisArg == null) {
0N/A thisArg = new LocalMember(where, this, 0, getType(), idThis);
0N/A }
0N/A return thisArg;
0N/A }
0N/A
0N/A /**
0N/A * Add a dependency
0N/A */
0N/A public void addDependency(ClassDeclaration c) {
0N/A if (tab != null) {
0N/A tab.put(c);
0N/A }
0N/A // If doing -xdepend option, save away list of class dependencies
0N/A // making sure to NOT include duplicates or the class we are in
0N/A // (Hashtable's put() makes sure we don't have duplicates)
0N/A if ( toplevelEnv.print_dependencies() && c != getClassDeclaration() ) {
0N/A deps.put(c,c);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Add a field (check it first)
0N/A */
0N/A public void addMember(Environment env, MemberDefinition f) {
0N/A // Make sure the access permissions are self-consistent:
0N/A switch (f.getModifiers() & (M_PUBLIC | M_PRIVATE | M_PROTECTED)) {
0N/A case M_PUBLIC:
0N/A case M_PRIVATE:
0N/A case M_PROTECTED:
0N/A case 0:
0N/A break;
0N/A default:
0N/A env.error(f.getWhere(), "inconsistent.modifier", f);
0N/A // Cut out the more restrictive modifier(s):
0N/A if (f.isPublic()) {
0N/A f.subModifiers(M_PRIVATE | M_PROTECTED);
0N/A } else {
0N/A f.subModifiers(M_PRIVATE);
0N/A }
0N/A break;
0N/A }
0N/A
0N/A // Note exemption for synthetic members below.
0N/A if (f.isStatic() && !isTopLevel() && !f.isSynthetic()) {
0N/A if (f.isMethod()) {
0N/A env.error(f.getWhere(), "static.inner.method", f, this);
0N/A f.subModifiers(M_STATIC);
0N/A } else if (f.isVariable()) {
0N/A if (!f.isFinal() || f.isBlankFinal()) {
0N/A env.error(f.getWhere(), "static.inner.field", f.getName(), this);
0N/A f.subModifiers(M_STATIC);
0N/A }
0N/A // Even if a static passes this test, there is still another
0N/A // check in 'SourceMember.check'. The check is delayed so
0N/A // that the initializer may be inspected more closely, using
0N/A // 'isConstant()'. Part of fix for 4095568.
0N/A } else {
0N/A // Static inner classes are diagnosed in 'SourceClass.<init>'.
0N/A f.subModifiers(M_STATIC);
0N/A }
0N/A }
0N/A
0N/A if (f.isMethod()) {
0N/A if (f.isConstructor()) {
0N/A if (f.getClassDefinition().isInterface()) {
0N/A env.error(f.getWhere(), "intf.constructor");
0N/A return;
0N/A }
0N/A if (f.isNative() || f.isAbstract() ||
0N/A f.isStatic() || f.isSynchronized() || f.isFinal()) {
0N/A env.error(f.getWhere(), "constr.modifier", f);
0N/A f.subModifiers(M_NATIVE | M_ABSTRACT |
0N/A M_STATIC | M_SYNCHRONIZED | M_FINAL);
0N/A }
0N/A } else if (f.isInitializer()) {
0N/A if (f.getClassDefinition().isInterface()) {
0N/A env.error(f.getWhere(), "intf.initializer");
0N/A return;
0N/A }
0N/A }
0N/A
0N/A // f is not allowed to return an array of void
0N/A if ((f.getType().getReturnType()).isVoidArray()) {
0N/A env.error(f.getWhere(), "void.array");
0N/A }
0N/A
0N/A if (f.getClassDefinition().isInterface() &&
0N/A (f.isStatic() || f.isSynchronized() || f.isNative()
0N/A || f.isFinal() || f.isPrivate() || f.isProtected())) {
0N/A env.error(f.getWhere(), "intf.modifier.method", f);
0N/A f.subModifiers(M_STATIC | M_SYNCHRONIZED | M_NATIVE |
0N/A M_FINAL | M_PRIVATE);
0N/A }
0N/A if (f.isTransient()) {
0N/A env.error(f.getWhere(), "transient.meth", f);
0N/A f.subModifiers(M_TRANSIENT);
0N/A }
0N/A if (f.isVolatile()) {
0N/A env.error(f.getWhere(), "volatile.meth", f);
0N/A f.subModifiers(M_VOLATILE);
0N/A }
0N/A if (f.isAbstract()) {
0N/A if (f.isPrivate()) {
0N/A env.error(f.getWhere(), "abstract.private.modifier", f);
0N/A f.subModifiers(M_PRIVATE);
0N/A }
0N/A if (f.isStatic()) {
0N/A env.error(f.getWhere(), "abstract.static.modifier", f);
0N/A f.subModifiers(M_STATIC);
0N/A }
0N/A if (f.isFinal()) {
0N/A env.error(f.getWhere(), "abstract.final.modifier", f);
0N/A f.subModifiers(M_FINAL);
0N/A }
0N/A if (f.isNative()) {
0N/A env.error(f.getWhere(), "abstract.native.modifier", f);
0N/A f.subModifiers(M_NATIVE);
0N/A }
0N/A if (f.isSynchronized()) {
0N/A env.error(f.getWhere(),"abstract.synchronized.modifier",f);
0N/A f.subModifiers(M_SYNCHRONIZED);
0N/A }
0N/A }
0N/A if (f.isAbstract() || f.isNative()) {
0N/A if (f.getValue() != null) {
0N/A env.error(f.getWhere(), "invalid.meth.body", f);
0N/A f.setValue(null);
0N/A }
0N/A } else {
0N/A if (f.getValue() == null) {
0N/A if (f.isConstructor()) {
0N/A env.error(f.getWhere(), "no.constructor.body", f);
0N/A } else {
0N/A env.error(f.getWhere(), "no.meth.body", f);
0N/A }
0N/A f.addModifiers(M_ABSTRACT);
0N/A }
0N/A }
0N/A Vector arguments = f.getArguments();
0N/A if (arguments != null) {
0N/A // arguments can be null if this is an implicit abstract method
0N/A int argumentLength = arguments.size();
0N/A Type argTypes[] = f.getType().getArgumentTypes();
0N/A for (int i = 0; i < argTypes.length; i++) {
0N/A Object arg = arguments.elementAt(i);
0N/A long where = f.getWhere();
0N/A if (arg instanceof MemberDefinition) {
0N/A where = ((MemberDefinition)arg).getWhere();
0N/A arg = ((MemberDefinition)arg).getName();
0N/A }
0N/A // (arg should be an Identifier now)
0N/A if (argTypes[i].isType(TC_VOID)
0N/A || argTypes[i].isVoidArray()) {
0N/A env.error(where, "void.argument", arg);
0N/A }
0N/A }
0N/A }
0N/A } else if (f.isInnerClass()) {
0N/A if (f.isVolatile() ||
0N/A f.isTransient() || f.isNative() || f.isSynchronized()) {
0N/A env.error(f.getWhere(), "inner.modifier", f);
0N/A f.subModifiers(M_VOLATILE | M_TRANSIENT |
0N/A M_NATIVE | M_SYNCHRONIZED);
0N/A }
0N/A // same check as for fields, below:
0N/A if (f.getClassDefinition().isInterface() &&
0N/A (f.isPrivate() || f.isProtected())) {
0N/A env.error(f.getWhere(), "intf.modifier.field", f);
0N/A f.subModifiers(M_PRIVATE | M_PROTECTED);
0N/A f.addModifiers(M_PUBLIC);
0N/A // Fix up the class itself to agree with
0N/A // the inner-class member.
0N/A ClassDefinition c = f.getInnerClass();
0N/A c.subModifiers(M_PRIVATE | M_PROTECTED);
0N/A c.addModifiers(M_PUBLIC);
0N/A }
0N/A } else {
0N/A if (f.getType().isType(TC_VOID) || f.getType().isVoidArray()) {
0N/A env.error(f.getWhere(), "void.inst.var", f.getName());
0N/A // REMIND: set type to error
0N/A return;
0N/A }
0N/A
0N/A if (f.isSynchronized() || f.isAbstract() || f.isNative()) {
0N/A env.error(f.getWhere(), "var.modifier", f);
0N/A f.subModifiers(M_SYNCHRONIZED | M_ABSTRACT | M_NATIVE);
0N/A }
0N/A if (f.isStrict()) {
0N/A env.error(f.getWhere(), "var.floatmodifier", f);
0N/A f.subModifiers(M_STRICTFP);
0N/A }
0N/A if (f.isTransient() && isInterface()) {
0N/A env.error(f.getWhere(), "transient.modifier", f);
0N/A f.subModifiers(M_TRANSIENT);
0N/A }
0N/A if (f.isVolatile() && (isInterface() || f.isFinal())) {
0N/A env.error(f.getWhere(), "volatile.modifier", f);
0N/A f.subModifiers(M_VOLATILE);
0N/A }
0N/A if (f.isFinal() && (f.getValue() == null) && isInterface()) {
0N/A env.error(f.getWhere(), "initializer.needed", f);
0N/A f.subModifiers(M_FINAL);
0N/A }
0N/A
0N/A if (f.getClassDefinition().isInterface() &&
0N/A (f.isPrivate() || f.isProtected())) {
0N/A env.error(f.getWhere(), "intf.modifier.field", f);
0N/A f.subModifiers(M_PRIVATE | M_PROTECTED);
0N/A f.addModifiers(M_PUBLIC);
0N/A }
0N/A }
0N/A // Do not check for repeated methods here: Types are not yet resolved.
0N/A if (!f.isInitializer()) {
0N/A for (MemberDefinition f2 = getFirstMatch(f.getName());
0N/A f2 != null; f2 = f2.getNextMatch()) {
0N/A if (f.isVariable() && f2.isVariable()) {
0N/A env.error(f.getWhere(), "var.multidef", f, f2);
0N/A return;
0N/A } else if (f.isInnerClass() && f2.isInnerClass() &&
0N/A !f.getInnerClass().isLocal() &&
0N/A !f2.getInnerClass().isLocal()) {
0N/A // Found a duplicate inner-class member.
0N/A // Duplicate local classes are detected in
0N/A // 'VarDeclarationStatement.checkDeclaration'.
0N/A env.error(f.getWhere(), "inner.class.multidef", f);
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A
0N/A super.addMember(env, f);
0N/A }
0N/A
0N/A /**
0N/A * Create an environment suitable for checking this class.
0N/A * Make sure the source and imports are set right.
0N/A * Make sure the environment contains no context information.
0N/A * (Actually, throw away env altogether and use toplevelEnv instead.)
0N/A */
0N/A public Environment setupEnv(Environment env) {
0N/A // In some cases, we go to some trouble to create the 'env' argument
0N/A // that is discarded. We should remove the 'env' argument entirely
0N/A // as well as the vestigial code that supports it. See comments on
0N/A // 'newEnvironment' in 'checkInternal' below.
0N/A return new Environment(toplevelEnv, this);
0N/A }
0N/A
0N/A /**
0N/A * A source class never reports deprecation, since the compiler
0N/A * allows access to deprecated features that are being compiled
0N/A * in the same job.
0N/A */
0N/A public boolean reportDeprecated(Environment env) {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * See if the source file of this class is right.
0N/A * @see ClassDefinition#noteUsedBy
0N/A */
0N/A public void noteUsedBy(ClassDefinition ref, long where, Environment env) {
0N/A // If this class is not public, watch for cross-file references.
0N/A super.noteUsedBy(ref, where, env);
0N/A ClassDefinition def = this;
0N/A while (def.isInnerClass()) {
0N/A def = def.getOuterClass();
0N/A }
0N/A if (def.isPublic()) {
0N/A return; // already checked
0N/A }
0N/A while (ref.isInnerClass()) {
0N/A ref = ref.getOuterClass();
0N/A }
0N/A if (def.getSource().equals(ref.getSource())) {
0N/A return; // intra-file reference
0N/A }
0N/A ((SourceClass)def).checkSourceFile(env, where);
0N/A }
0N/A
0N/A /**
0N/A * Check this class and all its fields.
0N/A */
0N/A public void check(Environment env) throws ClassNotFound {
0N/A if (tracing) env.dtEnter("SourceClass.check: " + getName());
0N/A if (isInsideLocal()) {
0N/A // An inaccessible class gets checked when the surrounding
0N/A // block is checked.
0N/A // QUERY: Should this case ever occur?
0N/A // What would invoke checking of a local class aside from
0N/A // checking the surrounding method body?
0N/A if (tracing) env.dtEvent("SourceClass.check: INSIDE LOCAL " +
0N/A getOuterClass().getName());
0N/A getOuterClass().check(env);
0N/A } else {
0N/A if (isInnerClass()) {
0N/A if (tracing) env.dtEvent("SourceClass.check: INNER CLASS " +
0N/A getOuterClass().getName());
0N/A // Make sure the outer is checked first.
0N/A ((SourceClass)getOuterClass()).maybeCheck(env);
0N/A }
0N/A Vset vset = new Vset();
0N/A Context ctx = null;
0N/A if (tracing)
0N/A env.dtEvent("SourceClass.check: CHECK INTERNAL " + getName());
0N/A vset = checkInternal(setupEnv(env), ctx, vset);
0N/A // drop vset here
0N/A }
0N/A if (tracing) env.dtExit("SourceClass.check: " + getName());
0N/A }
0N/A
0N/A private void maybeCheck(Environment env) throws ClassNotFound {
0N/A if (tracing) env.dtEvent("SourceClass.maybeCheck: " + getName());
0N/A // Check this class now, if it has not yet been checked.
0N/A // Cf. Main.compile(). Perhaps this code belongs there somehow.
0N/A ClassDeclaration c = getClassDeclaration();
0N/A if (c.getStatus() == CS_PARSED) {
0N/A // Set it first to avoid vicious circularity:
0N/A c.setDefinition(this, CS_CHECKED);
0N/A check(env);
0N/A }
0N/A }
0N/A
0N/A private Vset checkInternal(Environment env, Context ctx, Vset vset)
0N/A throws ClassNotFound {
0N/A Identifier nm = getClassDeclaration().getName();
0N/A if (env.verbose()) {
0N/A env.output("[checking class " + nm + "]");
0N/A }
0N/A
0N/A // Save context enclosing class for later access
0N/A // by 'ClassDefinition.resolveName.'
0N/A classContext = ctx;
0N/A
0N/A // At present, the call to 'newEnvironment' is not needed.
0N/A // The incoming environment to 'basicCheck' is always passed to
0N/A // 'setupEnv', which discards it completely. This is also the
0N/A // only call to 'newEnvironment', which is now apparently dead code.
0N/A basicCheck(Context.newEnvironment(env, ctx));
0N/A
0N/A // Validate access for all inner-class components
0N/A // of a qualified name, not just the last one, which
0N/A // is checked below. Yes, this is a dirty hack...
0N/A // Much of this code was cribbed from 'checkSupers'.
0N/A // Part of fix for 4094658.
0N/A ClassDeclaration sup = getSuperClass();
0N/A if (sup != null) {
0N/A long where = getWhere();
0N/A where = IdentifierToken.getWhere(superClassId, where);
0N/A env.resolveExtendsByName(where, this, sup.getName());
0N/A }
0N/A for (int i = 0 ; i < interfaces.length ; i++) {
0N/A ClassDeclaration intf = interfaces[i];
0N/A long where = getWhere();
0N/A // Error localization fails here if interfaces were
0N/A // elided during error recovery from an invalid one.
0N/A if (interfaceIds != null
0N/A && interfaceIds.length == interfaces.length) {
0N/A where = IdentifierToken.getWhere(interfaceIds[i], where);
0N/A }
0N/A env.resolveExtendsByName(where, this, intf.getName());
0N/A }
0N/A
0N/A // Does the name already exist in an imported package?
0N/A // See JLS 8.1 for the precise rules.
0N/A if (!isInnerClass() && !isInsideLocal()) {
0N/A // Discard package qualification for the import checks.
0N/A Identifier simpleName = nm.getName();
0N/A try {
0N/A // We want this to throw a ClassNotFound exception
0N/A Imports imports = toplevelEnv.getImports();
0N/A Identifier ID = imports.resolve(env, simpleName);
0N/A if (ID != getName())
0N/A env.error(where, "class.multidef.import", simpleName, ID);
0N/A } catch (AmbiguousClass e) {
0N/A // At least one of e.name1 and e.name2 must be different
0N/A Identifier ID = (e.name1 != getName()) ? e.name1 : e.name2;
0N/A env.error(where, "class.multidef.import", simpleName, ID);
0N/A } catch (ClassNotFound e) {
0N/A // we want this to happen
0N/A }
0N/A
0N/A // Make sure that no package with the same fully qualified
0N/A // name exists. This is required by JLS 7.1. We only need
0N/A // to perform this check for top level classes -- it isn't
0N/A // necessary for inner classes. (bug 4101529)
0N/A //
0N/A // This change has been backed out because, on WIN32, it
0N/A // failed to distinguish between java.awt.event and
0N/A // java.awt.Event when looking for a directory. We will
0N/A // add this back in later.
0N/A //
0N/A // try {
0N/A // if (env.getPackage(nm).exists()) {
0N/A // env.error(where, "class.package.conflict", nm);
0N/A // }
0N/A // } catch (java.io.IOException ee) {
0N/A // env.error(where, "io.exception.package", nm);
0N/A // }
0N/A
0N/A // Make sure it was defined in the right file
0N/A if (isPublic()) {
0N/A checkSourceFile(env, getWhere());
0N/A }
0N/A }
0N/A
0N/A vset = checkMembers(env, ctx, vset);
0N/A return vset;
0N/A }
0N/A
0N/A private boolean sourceFileChecked = false;
0N/A
0N/A /**
0N/A * See if the source file of this class is of the right name.
0N/A */
0N/A public void checkSourceFile(Environment env, long where) {
0N/A // one error per offending class is sufficient
0N/A if (sourceFileChecked) return;
0N/A sourceFileChecked = true;
0N/A
0N/A String fname = getName().getName() + ".java";
0N/A String src = ((ClassFile)getSource()).getName();
0N/A if (!src.equals(fname)) {
0N/A if (isPublic()) {
0N/A env.error(where, "public.class.file", this, fname);
0N/A } else {
0N/A env.error(where, "warn.package.class.file", this, src, fname);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Set true if superclass (but not necessarily superinterfaces) have
0N/A // been checked. If the superclass is still unresolved, then an error
0N/A // message should have been issued, and we assume that no further
0N/A // resolution is possible.
0N/A private boolean supersChecked = false;
0N/A
0N/A /**
0N/A * Overrides 'ClassDefinition.getSuperClass'.
0N/A */
0N/A
0N/A public ClassDeclaration getSuperClass(Environment env) {
0N/A if (tracing) env.dtEnter("SourceClass.getSuperClass: " + this);
0N/A // Superclass may fail to be set because of error recovery,
0N/A // so resolve types here only if 'checkSupers' has not yet
0N/A // completed its checks on the superclass.
0N/A // QUERY: Can we eliminate the need to resolve superclasses on demand?
0N/A // See comments in 'checkSupers' and in 'ClassDefinition.getInnerClass'.
0N/A if (superClass == null && superClassId != null && !supersChecked) {
0N/A resolveTypeStructure(env);
0N/A // We used to report an error here if the superclass was not
0N/A // resolved. Having moved the call to 'checkSupers' from 'basicCheck'
0N/A // into 'resolveTypeStructure', the errors reported here should have
0N/A // already been reported. Furthermore, error recovery can null out
0N/A // the superclass, which would cause a spurious error from the test here.
0N/A }
0N/A if (tracing) env.dtExit("SourceClass.getSuperClass: " + this);
0N/A return superClass;
0N/A }
0N/A
0N/A /**
0N/A * Check that all superclasses and superinterfaces are defined and
0N/A * well formed. Among other checks, verify that the inheritance
0N/A * graph is acyclic. Called from 'resolveTypeStructure'.
0N/A */
0N/A
0N/A private void checkSupers(Environment env) throws ClassNotFound {
0N/A
0N/A // *** DEBUG ***
0N/A supersCheckStarted = true;
0N/A
0N/A if (tracing) env.dtEnter("SourceClass.checkSupers: " + this);
0N/A
0N/A if (isInterface()) {
0N/A if (isFinal()) {
0N/A Identifier nm = getClassDeclaration().getName();
0N/A env.error(getWhere(), "final.intf", nm);
0N/A // Interfaces have no superclass. Superinterfaces
0N/A // are checked below, in code shared with the class case.
0N/A }
0N/A } else {
0N/A // Check superclass.
0N/A // Call to 'getSuperClass(env)' (note argument) attempts
0N/A // 'resolveTypeStructure' if superclass has not successfully
0N/A // been resolved. Since we have just now called 'resolveSupers'
0N/A // (see our call in 'resolveTypeStructure'), it is not clear
0N/A // that this can do any good. Why not 'getSuperClass()' here?
0N/A if (getSuperClass(env) != null) {
0N/A long where = getWhere();
0N/A where = IdentifierToken.getWhere(superClassId, where);
0N/A try {
0N/A ClassDefinition def =
0N/A getSuperClass().getClassDefinition(env);
0N/A // Resolve superclass and its ancestors.
0N/A def.resolveTypeStructure(env);
0N/A // Access to the superclass should be checked relative
0N/A // to the surrounding context, not as if the reference
0N/A // appeared within the class body. Changed 'canAccess'
0N/A // to 'extendsCanAccess' to fix 4087314.
0N/A if (!extendsCanAccess(env, getSuperClass())) {
0N/A env.error(where, "cant.access.class", getSuperClass());
0N/A // Might it be a better recovery to let the access go through?
0N/A superClass = null;
0N/A } else if (def.isFinal()) {
0N/A env.error(where, "super.is.final", getSuperClass());
0N/A // Might it be a better recovery to let the access go through?
0N/A superClass = null;
0N/A } else if (def.isInterface()) {
0N/A env.error(where, "super.is.intf", getSuperClass());
0N/A superClass = null;
0N/A } else if (superClassOf(env, getSuperClass())) {
0N/A env.error(where, "cyclic.super");
0N/A superClass = null;
0N/A } else {
0N/A def.noteUsedBy(this, where, env);
0N/A }
0N/A if (superClass == null) {
0N/A def = null;
0N/A } else {
0N/A // If we have a valid superclass, check its
0N/A // supers as well, and so on up to root class.
0N/A // Call to 'enclosingClassOf' will raise
0N/A // 'NullPointerException' if 'def' is null,
0N/A // so omit this check as error recovery.
0N/A ClassDefinition sup = def;
0N/A for (;;) {
0N/A if (enclosingClassOf(sup)) {
0N/A // Do we need a similar test for
0N/A // interfaces? See bugid 4038529.
0N/A env.error(where, "super.is.inner");
0N/A superClass = null;
0N/A break;
0N/A }
0N/A // Since we resolved the superclass and its
0N/A // ancestors above, we should not discover
0N/A // any unresolved classes on the superclass
0N/A // chain. It should thus be sufficient to
0N/A // call 'getSuperClass()' (no argument) here.
0N/A ClassDeclaration s = sup.getSuperClass(env);
0N/A if (s == null) {
0N/A // Superclass not resolved due to error.
0N/A break;
0N/A }
0N/A sup = s.getClassDefinition(env);
0N/A }
0N/A }
0N/A } catch (ClassNotFound e) {
0N/A // Error is detected in call to 'getClassDefinition'.
0N/A // The class may actually exist but be ambiguous.
0N/A // Call env.resolve(e.name) to see if it is.
0N/A // env.resolve(name) will definitely tell us if the
0N/A // class is ambiguous, but may not necessarily tell
0N/A // us if the class is not found.
0N/A // (part of solution for 4059855)
0N/A reportError: {
0N/A try {
0N/A env.resolve(e.name);
0N/A } catch (AmbiguousClass ee) {
0N/A env.error(where,
0N/A "ambig.class", ee.name1, ee.name2);
0N/A superClass = null;
0N/A break reportError;
0N/A } catch (ClassNotFound ee) {
0N/A // fall through
0N/A }
0N/A env.error(where, "super.not.found", e.name, this);
0N/A superClass = null;
0N/A } // The break exits this block
0N/A }
0N/A
0N/A } else {
0N/A // Superclass was null on entry, after call to
0N/A // 'resolveSupers'. This should normally not happen,
0N/A // as 'resolveSupers' sets 'superClass' to a non-null
0N/A // value for all named classes, except for one special
0N/A // case: 'java.lang.Object', which has no superclass.
0N/A if (isAnonymous()) {
0N/A // checker should have filled it in first
0N/A throw new CompilerError("anonymous super");
0N/A } else if (!getName().equals(idJavaLangObject)) {
0N/A throw new CompilerError("unresolved super");
0N/A }
0N/A }
0N/A }
0N/A
0N/A // At this point, if 'superClass' is null due to an error
0N/A // in the user program, a message should have been issued.
0N/A supersChecked = true;
0N/A
0N/A // Check interfaces
0N/A for (int i = 0 ; i < interfaces.length ; i++) {
0N/A ClassDeclaration intf = interfaces[i];
0N/A long where = getWhere();
0N/A if (interfaceIds != null
0N/A && interfaceIds.length == interfaces.length) {
0N/A where = IdentifierToken.getWhere(interfaceIds[i], where);
0N/A }
0N/A try {
0N/A ClassDefinition def = intf.getClassDefinition(env);
0N/A // Resolve superinterface and its ancestors.
0N/A def.resolveTypeStructure(env);
0N/A // Check superinterface access in the correct context.
0N/A // Changed 'canAccess' to 'extendsCanAccess' to fix 4087314.
0N/A if (!extendsCanAccess(env, intf)) {
0N/A env.error(where, "cant.access.class", intf);
0N/A } else if (!intf.getClassDefinition(env).isInterface()) {
0N/A env.error(where, "not.intf", intf);
0N/A } else if (isInterface() && implementedBy(env, intf)) {
0N/A env.error(where, "cyclic.intf", intf);
0N/A } else {
0N/A def.noteUsedBy(this, where, env);
0N/A // Interface is OK, leave it in the interface list.
0N/A continue;
0N/A }
0N/A } catch (ClassNotFound e) {
0N/A // The interface may actually exist but be ambiguous.
0N/A // Call env.resolve(e.name) to see if it is.
0N/A // env.resolve(name) will definitely tell us if the
0N/A // interface is ambiguous, but may not necessarily tell
0N/A // us if the interface is not found.
0N/A // (part of solution for 4059855)
0N/A reportError2: {
0N/A try {
0N/A env.resolve(e.name);
0N/A } catch (AmbiguousClass ee) {
0N/A env.error(where,
0N/A "ambig.class", ee.name1, ee.name2);
0N/A superClass = null;
0N/A break reportError2;
0N/A } catch (ClassNotFound ee) {
0N/A // fall through
0N/A }
0N/A env.error(where, "intf.not.found", e.name, this);
0N/A superClass = null;
0N/A } // The break exits this block
0N/A }
0N/A // Remove this interface from the list of interfaces
0N/A // as recovery from an error.
0N/A ClassDeclaration newInterfaces[] =
0N/A new ClassDeclaration[interfaces.length - 1];
0N/A System.arraycopy(interfaces, 0, newInterfaces, 0, i);
0N/A System.arraycopy(interfaces, i + 1, newInterfaces, i,
0N/A newInterfaces.length - i);
0N/A interfaces = newInterfaces;
0N/A --i;
0N/A }
0N/A if (tracing) env.dtExit("SourceClass.checkSupers: " + this);
0N/A }
0N/A
0N/A /**
0N/A * Check all of the members of this class.
0N/A * <p>
0N/A * Inner classes are checked in the following way. Any class which
0N/A * is immediately contained in a block (anonymous and local classes)
0N/A * is checked along with its containing method; see the
0N/A * SourceMember.check() method for more information. Member classes
0N/A * of this class are checked immediately after this class, unless this
0N/A * class is insideLocal(), in which case, they are checked with the
0N/A * rest of the members.
0N/A */
0N/A private Vset checkMembers(Environment env, Context ctx, Vset vset)
0N/A throws ClassNotFound {
0N/A
0N/A // bail out if there were any errors
0N/A if (getError()) {
0N/A return vset;
0N/A }
0N/A
0N/A // Make sure that all of our member classes have been
0N/A // basicCheck'ed before we check the rest of our members.
0N/A // If our member classes haven't been basicCheck'ed, then they
0N/A // may not have <init> methods. It is important that they
0N/A // have <init> methods so we can process NewInstanceExpressions
0N/A // correctly. This problem didn't occur before 1.2beta1.
0N/A // This is a fix for bug 4082816.
0N/A for (MemberDefinition f = getFirstMember();
0N/A f != null; f = f.getNextMember()) {
0N/A if (f.isInnerClass()) {
0N/A // System.out.println("Considering " + f + " in " + this);
0N/A SourceClass cdef = (SourceClass) f.getInnerClass();
0N/A if (cdef.isMember()) {
0N/A cdef.basicCheck(env);
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (isFinal() && isAbstract()) {
0N/A env.error(where, "final.abstract", this.getName().getName());
0N/A }
0N/A
0N/A // This class should be abstract if there are any abstract methods
0N/A // in our parent classes and interfaces which we do not override.
0N/A // There are odd cases when, even though we cannot access some
0N/A // abstract method from our superclass, that abstract method can
0N/A // still force this class to be abstract. See the discussion in
0N/A // bug id 1240831.
0N/A if (!isInterface() && !isAbstract() && mustBeAbstract(env)) {
0N/A // Set the class abstract.
0N/A modifiers |= M_ABSTRACT;
0N/A
0N/A // Tell the user which methods force this class to be abstract.
0N/A
0N/A // First list all of the "unimplementable" abstract methods.
0N/A Iterator iter = getPermanentlyAbstractMethods();
0N/A while (iter.hasNext()) {
0N/A MemberDefinition method = (MemberDefinition) iter.next();
0N/A // We couldn't override this method even if we
0N/A // wanted to. Try to make the error message
0N/A // as non-confusing as possible.
0N/A env.error(where, "abstract.class.cannot.override",
0N/A getClassDeclaration(), method,
0N/A method.getDefiningClassDeclaration());
0N/A }
0N/A
0N/A // Now list all of the traditional abstract methods.
0N/A iter = getMethods(env);
0N/A while (iter.hasNext()) {
0N/A // For each method, check if it is abstract. If it is,
0N/A // output an appropriate error message.
0N/A MemberDefinition method = (MemberDefinition) iter.next();
0N/A if (method.isAbstract()) {
0N/A env.error(where, "abstract.class",
0N/A getClassDeclaration(), method,
0N/A method.getDefiningClassDeclaration());
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Check the instance variables in a pre-pass before any constructors.
0N/A // This lets constructors "in-line" any initializers directly.
0N/A // It also lets us do some definite assignment checks on variables.
0N/A Context ctxInit = new Context(ctx);
0N/A Vset vsInst = vset.copy();
0N/A Vset vsClass = vset.copy();
0N/A
0N/A // Do definite assignment checking on blank finals.
0N/A // Other variables do not need such checks. The simple textual
0N/A // ordering constraints implemented by MemberDefinition.canReach()
0N/A // are necessary and sufficient for the other variables.
0N/A // Note that within non-static code, all statics are always
0N/A // definitely assigned, and vice-versa.
0N/A for (MemberDefinition f = getFirstMember();
0N/A f != null; f = f.getNextMember()) {
0N/A if (f.isVariable() && f.isBlankFinal()) {
0N/A // The following allocates a LocalMember object as a proxy
0N/A // to represent the field.
0N/A int number = ctxInit.declareFieldNumber(f);
0N/A if (f.isStatic()) {
0N/A vsClass = vsClass.addVarUnassigned(number);
0N/A vsInst = vsInst.addVar(number);
0N/A } else {
0N/A vsInst = vsInst.addVarUnassigned(number);
0N/A vsClass = vsClass.addVar(number);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // For instance variable checks, use a context with a "this" parameter.
0N/A Context ctxInst = new Context(ctxInit, this);
0N/A LocalMember thisArg = getThisArgument();
0N/A int thisNumber = ctxInst.declare(env, thisArg);
0N/A vsInst = vsInst.addVar(thisNumber);
0N/A
0N/A // Do all the initializers in order, checking the definite
0N/A // assignment of blank finals. Separate static from non-static.
0N/A for (MemberDefinition f = getFirstMember();
0N/A f != null; f = f.getNextMember()) {
0N/A try {
0N/A if (f.isVariable() || f.isInitializer()) {
0N/A if (f.isStatic()) {
0N/A vsClass = f.check(env, ctxInit, vsClass);
0N/A } else {
0N/A vsInst = f.check(env, ctxInst, vsInst);
0N/A }
0N/A }
0N/A } catch (ClassNotFound ee) {
0N/A env.error(f.getWhere(), "class.not.found", ee.name, this);
0N/A }
0N/A }
0N/A
0N/A checkBlankFinals(env, ctxInit, vsClass, true);
0N/A
0N/A // Check the rest of the field definitions.
0N/A // (Note: Re-checking a field is a no-op.)
0N/A for (MemberDefinition f = getFirstMember();
0N/A f != null; f = f.getNextMember()) {
0N/A try {
0N/A if (f.isConstructor()) {
0N/A // When checking a constructor, an explicit call to
0N/A // 'this(...)' makes all blank finals definitely assigned.
0N/A // See 'MethodExpression.checkValue'.
0N/A Vset vsCon = f.check(env, ctxInit, vsInst.copy());
0N/A // May issue multiple messages for the same variable!!
0N/A checkBlankFinals(env, ctxInit, vsCon, false);
0N/A // (drop vsCon here)
0N/A } else {
0N/A Vset vsFld = f.check(env, ctx, vset.copy());
0N/A // (drop vsFld here)
0N/A }
0N/A } catch (ClassNotFound ee) {
0N/A env.error(f.getWhere(), "class.not.found", ee.name, this);
0N/A }
0N/A }
0N/A
0N/A // Must mark class as checked before visiting inner classes,
0N/A // as they may in turn request checking of the current class
0N/A // as an outer class. Fix for bug id 4056774.
0N/A getClassDeclaration().setDefinition(this, CS_CHECKED);
0N/A
0N/A // Also check other classes in the same nest.
0N/A // All checking of this nest must be finished before any
0N/A // of its classes emit bytecode.
0N/A // Otherwise, the inner classes might not have a chance to
0N/A // add access or class literal fields to the outer class.
0N/A for (MemberDefinition f = getFirstMember();
0N/A f != null; f = f.getNextMember()) {
0N/A if (f.isInnerClass()) {
0N/A SourceClass cdef = (SourceClass) f.getInnerClass();
0N/A if (!cdef.isInsideLocal()) {
0N/A cdef.maybeCheck(env);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Note: Since inner classes cannot set up-level variables,
0N/A // the returned vset is always equal to the passed-in vset.
0N/A // Still, we'll return it for the sake of regularity.
0N/A return vset;
0N/A }
0N/A
0N/A /** Make sure all my blank finals exist now. */
0N/A
0N/A private void checkBlankFinals(Environment env, Context ctxInit, Vset vset,
0N/A boolean isStatic) {
0N/A for (int i = 0; i < ctxInit.getVarNumber(); i++) {
0N/A if (!vset.testVar(i)) {
0N/A MemberDefinition ff = ctxInit.getElement(i);
0N/A if (ff != null && ff.isBlankFinal()
0N/A && ff.isStatic() == isStatic
0N/A && ff.getClassDefinition() == this) {
0N/A env.error(ff.getWhere(),
0N/A "final.var.not.initialized", ff.getName());
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Check this class has its superclass and its interfaces. Also
0N/A * force it to have an <init> method (if it doesn't already have one)
0N/A * and to have all the abstract methods of its parents.
0N/A */
0N/A private boolean basicChecking = false;
0N/A private boolean basicCheckDone = false;
0N/A protected void basicCheck(Environment env) throws ClassNotFound {
0N/A
0N/A if (tracing) env.dtEnter("SourceClass.basicCheck: " + getName());
0N/A
0N/A super.basicCheck(env);
0N/A
0N/A if (basicChecking || basicCheckDone) {
0N/A if (tracing) env.dtExit("SourceClass.basicCheck: OK " + getName());
0N/A return;
0N/A }
0N/A
0N/A if (tracing) env.dtEvent("SourceClass.basicCheck: CHECKING " + getName());
0N/A
0N/A basicChecking = true;
0N/A
0N/A env = setupEnv(env);
0N/A
0N/A Imports imports = env.getImports();
0N/A if (imports != null) {
0N/A imports.resolve(env);
0N/A }
0N/A
0N/A resolveTypeStructure(env);
0N/A
0N/A // Check the existence of the superclass and all interfaces.
0N/A // Also responsible for breaking inheritance cycles. This call
0N/A // has been moved to 'resolveTypeStructure', just after the call
0N/A // to 'resolveSupers', as inheritance cycles must be broken before
0N/A // resolving types within the members. Fixes 4073739.
0N/A // checkSupers(env);
0N/A
0N/A if (!isInterface()) {
0N/A
0N/A // Add implicit <init> method, if necessary.
0N/A // QUERY: What keeps us from adding an implicit constructor
0N/A // when the user explicitly declares one? Is it truly guaranteed
0N/A // that the declaration for such an explicit constructor will have
0N/A // been processed by the time we arrive here? In general, 'basicCheck'
0N/A // is called very early, prior to the normal member checking phase.
0N/A if (!hasConstructor()) {
0N/A Node code = new CompoundStatement(getWhere(), new Statement[0]);
0N/A Type t = Type.tMethod(Type.tVoid);
0N/A
0N/A // Default constructors inherit the access modifiers of their
0N/A // class. For non-inner classes, this follows from JLS 8.6.7,
0N/A // as the only possible modifier is 'public'. For the sake of
0N/A // robustness in the presence of errors, we ignore any other
0N/A // modifiers. For inner classes, the rule needs to be extended
0N/A // in some way to account for the possibility of private and
0N/A // protected classes. We make the 'obvious' extension, however,
0N/A // the inner classes spec is silent on this issue, and a definitive
0N/A // resolution is needed. See bugid 4087421.
0N/A // WORKAROUND: A private constructor might need an access method,
0N/A // but it is not possible to create one due to a restriction in
0N/A // the verifier. (This is a known problem -- see 4015397.)
0N/A // We therefore do not inherit the 'private' modifier from the class,
0N/A // allowing the default constructor to be package private. This
0N/A // workaround can be observed via reflection, but is otherwise
0N/A // undetectable, as the constructor is always accessible within
0N/A // the class in which its containing (private) class appears.
0N/A int accessModifiers = getModifiers() &
0N/A (isInnerClass() ? (M_PUBLIC | M_PROTECTED) : M_PUBLIC);
0N/A env.makeMemberDefinition(env, getWhere(), this, null,
0N/A accessModifiers,
0N/A t, idInit, null, null, code);
0N/A }
0N/A }
0N/A
0N/A // Only do the inheritance/override checks if they are turned on.
0N/A // The idea here is that they will be done in javac, but not
0N/A // in javadoc. See the comment for turnOffChecks(), above.
0N/A if (doInheritanceChecks) {
0N/A
0N/A // Verify the compatibility of all inherited method definitions
0N/A // by collecting all of our inheritable methods.
0N/A collectInheritedMethods(env);
0N/A }
0N/A
0N/A basicChecking = false;
0N/A basicCheckDone = true;
0N/A if (tracing) env.dtExit("SourceClass.basicCheck: " + getName());
0N/A }
0N/A
0N/A /**
0N/A * Add a group of methods to this class as miranda methods.
0N/A *
0N/A * For a definition of Miranda methods, see the comment above the
0N/A * method addMirandaMethods() in the file
0N/A * sun/tools/java/ClassDeclaration.java
0N/A */
0N/A protected void addMirandaMethods(Environment env,
0N/A Iterator mirandas) {
0N/A
0N/A while(mirandas.hasNext()) {
0N/A MemberDefinition method =
0N/A (MemberDefinition)mirandas.next();
0N/A
0N/A addMember(method);
0N/A
0N/A //System.out.println("adding miranda method " + newMethod +
0N/A // " to " + this);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * <em>After parsing is complete</em>, resolve all names
0N/A * except those inside method bodies or initializers.
0N/A * In particular, this is the point at which we find out what
0N/A * kinds of variables and methods there are in the classes,
0N/A * and therefore what is each class's interface to the world.
0N/A * <p>
0N/A * Also perform certain other transformations, such as inserting
0N/A * "this$C" arguments into constructors, and reorganizing structure
0N/A * to flatten qualified member names.
0N/A * <p>
0N/A * Do not perform type-based or name-based consistency checks
0N/A * or normalizations (such as default nullary constructors),
0N/A * and do not attempt to compile code against this class,
0N/A * until after this phase.
0N/A */
0N/A
0N/A private boolean resolving = false;
0N/A
0N/A public void resolveTypeStructure(Environment env) {
0N/A
0N/A if (tracing)
0N/A env.dtEnter("SourceClass.resolveTypeStructure: " + getName());
0N/A
0N/A // Resolve immediately enclosing type, which in turn
0N/A // forces resolution of all enclosing type declarations.
0N/A ClassDefinition oc = getOuterClass();
0N/A if (oc != null && oc instanceof SourceClass
0N/A && !((SourceClass)oc).resolved) {
0N/A // Do the outer class first, always.
0N/A ((SourceClass)oc).resolveTypeStructure(env);
0N/A // (Note: this.resolved is probably true at this point.)
0N/A }
0N/A
0N/A // Punt if we've already resolved this class, or are currently
0N/A // in the process of doing so.
0N/A if (resolved || resolving) {
0N/A if (tracing)
0N/A env.dtExit("SourceClass.resolveTypeStructure: OK " + getName());
0N/A return;
0N/A }
0N/A
0N/A // Previously, 'resolved' was set here, and served to prevent
0N/A // duplicate resolutions here as well as its function in
0N/A // 'ClassDefinition.addMember'. Now, 'resolving' serves the
0N/A // former purpose, distinct from that of 'resolved'.
0N/A resolving = true;
0N/A
0N/A if (tracing)
0N/A env.dtEvent("SourceClass.resolveTypeStructure: RESOLVING " + getName());
0N/A
0N/A env = setupEnv(env);
0N/A
0N/A // Resolve superclass names to class declarations
0N/A // for the immediate superclass and superinterfaces.
0N/A resolveSupers(env);
0N/A
0N/A // Check all ancestor superclasses for various
0N/A // errors, verifying definition of all superclasses
0N/A // and superinterfaces. Also breaks inheritance cycles.
0N/A // Calls 'resolveTypeStructure' recursively for ancestors
0N/A // This call used to appear in 'basicCheck', but was not
0N/A // performed early enough. Most of the compiler will barf
0N/A // on inheritance cycles!
0N/A try {
0N/A checkSupers(env);
0N/A } catch (ClassNotFound ee) {
0N/A // Undefined classes should be reported by 'checkSupers'.
0N/A env.error(where, "class.not.found", ee.name, this);
0N/A }
0N/A
0N/A for (MemberDefinition
0N/A f = getFirstMember() ; f != null ; f = f.getNextMember()) {
0N/A if (f instanceof SourceMember)
0N/A ((SourceMember)f).resolveTypeStructure(env);
0N/A }
0N/A
0N/A resolving = false;
0N/A
0N/A // Mark class as resolved. If new members are subsequently
0N/A // added to the class, they will be resolved at that time.
0N/A // See 'ClassDefinition.addMember'. Previously, this variable was
0N/A // set prior to the calls to 'checkSupers' and 'resolveTypeStructure'
0N/A // (which may engender further calls to 'checkSupers'). This could
0N/A // lead to duplicate resolution of implicit constructors, as the call to
0N/A // 'basicCheck' from 'checkSupers' could add the constructor while
0N/A // its class is marked resolved, and thus would resolve the constructor,
0N/A // believing it to be a "late addition". It would then be resolved
0N/A // redundantly during the normal traversal of the members, which
0N/A // immediately follows in the code above.
0N/A resolved = true;
0N/A
0N/A // Now we have enough information to detect method repeats.
0N/A for (MemberDefinition
0N/A f = getFirstMember() ; f != null ; f = f.getNextMember()) {
0N/A if (f.isInitializer()) continue;
0N/A if (!f.isMethod()) continue;
0N/A for (MemberDefinition f2 = f; (f2 = f2.getNextMatch()) != null; ) {
0N/A if (!f2.isMethod()) continue;
0N/A if (f.getType().equals(f2.getType())) {
0N/A env.error(f.getWhere(), "meth.multidef", f);
0N/A continue;
0N/A }
0N/A if (f.getType().equalArguments(f2.getType())) {
0N/A env.error(f.getWhere(), "meth.redef.rettype", f, f2);
0N/A continue;
0N/A }
0N/A }
0N/A }
0N/A if (tracing)
0N/A env.dtExit("SourceClass.resolveTypeStructure: " + getName());
0N/A }
0N/A
0N/A protected void resolveSupers(Environment env) {
0N/A if (tracing)
0N/A env.dtEnter("SourceClass.resolveSupers: " + this);
0N/A // Find the super class
0N/A if (superClassId != null && superClass == null) {
0N/A superClass = resolveSuper(env, superClassId);
0N/A // Special-case java.lang.Object here (not in the parser).
0N/A // In all other cases, if we have a valid 'superClassId',
0N/A // we return with a valid and non-null 'superClass' value.
0N/A if (superClass == getClassDeclaration()
0N/A && getName().equals(idJavaLangObject)) {
0N/A superClass = null;
0N/A superClassId = null;
0N/A }
0N/A }
0N/A // Find interfaces
0N/A if (interfaceIds != null && interfaces == null) {
0N/A interfaces = new ClassDeclaration[interfaceIds.length];
0N/A for (int i = 0 ; i < interfaces.length ; i++) {
0N/A interfaces[i] = resolveSuper(env, interfaceIds[i]);
0N/A for (int j = 0; j < i; j++) {
0N/A if (interfaces[i] == interfaces[j]) {
0N/A Identifier id = interfaceIds[i].getName();
0N/A long where = interfaceIds[j].getWhere();
0N/A env.error(where, "intf.repeated", id);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A if (tracing)
0N/A env.dtExit("SourceClass.resolveSupers: " + this);
0N/A }
0N/A
0N/A private ClassDeclaration resolveSuper(Environment env, IdentifierToken t) {
0N/A Identifier name = t.getName();
0N/A if (tracing)
0N/A env.dtEnter("SourceClass.resolveSuper: " + name);
0N/A if (isInnerClass())
0N/A name = outerClass.resolveName(env, name);
0N/A else
0N/A name = env.resolveName(name);
0N/A ClassDeclaration result = env.getClassDeclaration(name);
0N/A // Result is never null, as a new 'ClassDeclaration' is
0N/A // created if one with the given name does not exist.
0N/A if (tracing) env.dtExit("SourceClass.resolveSuper: " + name);
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * During the type-checking of an outer method body or initializer,
0N/A * this routine is called to check a local class body
0N/A * in the proper context.
0N/A * @param sup the named super class or interface (if anonymous)
0N/A * @param args the actual arguments (if anonymous)
0N/A */
0N/A public Vset checkLocalClass(Environment env, Context ctx, Vset vset,
0N/A ClassDefinition sup,
0N/A Expression args[], Type argTypes[]
0N/A ) throws ClassNotFound {
0N/A env = setupEnv(env);
0N/A
0N/A if ((sup != null) != isAnonymous()) {
0N/A throw new CompilerError("resolveAnonymousStructure");
0N/A }
0N/A if (isAnonymous()) {
0N/A resolveAnonymousStructure(env, sup, args, argTypes);
0N/A }
0N/A
0N/A // Run the checks in the lexical context from the outer class.
0N/A vset = checkInternal(env, ctx, vset);
0N/A
0N/A // This is now done by 'checkInternal' via its call to 'checkMembers'.
0N/A // getClassDeclaration().setDefinition(this, CS_CHECKED);
0N/A
0N/A return vset;
0N/A }
0N/A
0N/A /**
0N/A * As with checkLocalClass, run the inline phase for a local class.
0N/A */
0N/A public void inlineLocalClass(Environment env) {
0N/A for (MemberDefinition
0N/A f = getFirstMember(); f != null; f = f.getNextMember()) {
0N/A if ((f.isVariable() || f.isInitializer()) && !f.isStatic()) {
0N/A continue; // inlined inside of constructors only
0N/A }
0N/A try {
0N/A ((SourceMember)f).inline(env);
0N/A } catch (ClassNotFound ee) {
0N/A env.error(f.getWhere(), "class.not.found", ee.name, this);
0N/A }
0N/A }
0N/A if (getReferencesFrozen() != null && !inlinedLocalClass) {
0N/A inlinedLocalClass = true;
0N/A // add more constructor arguments for uplevel references
0N/A for (MemberDefinition
0N/A f = getFirstMember(); f != null; f = f.getNextMember()) {
0N/A if (f.isConstructor()) {
0N/A //((SourceMember)f).addUplevelArguments(false);
0N/A ((SourceMember)f).addUplevelArguments();
0N/A }
0N/A }
0N/A }
0N/A }
0N/A private boolean inlinedLocalClass = false;
0N/A
0N/A /**
0N/A * Check a class which is inside a local class, but is not itself local.
0N/A */
0N/A public Vset checkInsideClass(Environment env, Context ctx, Vset vset)
0N/A throws ClassNotFound {
0N/A if (!isInsideLocal() || isLocal()) {
0N/A throw new CompilerError("checkInsideClass");
0N/A }
0N/A return checkInternal(env, ctx, vset);
0N/A }
0N/A
0N/A /**
0N/A * Just before checking an anonymous class, decide its true
0N/A * inheritance, and build its (sole, implicit) constructor.
0N/A */
0N/A private void resolveAnonymousStructure(Environment env,
0N/A ClassDefinition sup,
0N/A Expression args[], Type argTypes[]
0N/A ) throws ClassNotFound {
0N/A
0N/A if (tracing) env.dtEvent("SourceClass.resolveAnonymousStructure: " +
0N/A this + ", super " + sup);
0N/A
0N/A // Decide now on the superclass.
0N/A
0N/A // This check has been removed as part of the fix for 4055017.
0N/A // In the anonymous class created to hold the 'class$' method
0N/A // of an interface, 'superClassId' refers to 'java.lang.Object'.
0N/A /*---------------------*
0N/A if (!(superClass == null && superClassId.getName() == idNull)) {
0N/A throw new CompilerError("superclass "+superClass);
0N/A }
0N/A *---------------------*/
0N/A
0N/A if (sup.isInterface()) {
0N/A // allow an interface in the "super class" position
0N/A int ni = (interfaces == null) ? 0 : interfaces.length;
0N/A ClassDeclaration i1[] = new ClassDeclaration[1+ni];
0N/A if (ni > 0) {
0N/A System.arraycopy(interfaces, 0, i1, 1, ni);
0N/A if (interfaceIds != null && interfaceIds.length == ni) {
0N/A IdentifierToken id1[] = new IdentifierToken[1+ni];
0N/A System.arraycopy(interfaceIds, 0, id1, 1, ni);
0N/A id1[0] = new IdentifierToken(sup.getName());
0N/A }
0N/A }
0N/A i1[0] = sup.getClassDeclaration();
0N/A interfaces = i1;
0N/A
0N/A sup = toplevelEnv.getClassDefinition(idJavaLangObject);
0N/A }
0N/A superClass = sup.getClassDeclaration();
0N/A
0N/A if (hasConstructor()) {
0N/A throw new CompilerError("anonymous constructor");
0N/A }
0N/A
0N/A // Synthesize an appropriate constructor.
0N/A Type t = Type.tMethod(Type.tVoid, argTypes);
0N/A IdentifierToken names[] = new IdentifierToken[argTypes.length];
0N/A for (int i = 0; i < names.length; i++) {
0N/A names[i] = new IdentifierToken(args[i].getWhere(),
0N/A Identifier.lookup("$"+i));
0N/A }
0N/A int outerArg = (sup.isTopLevel() || sup.isLocal()) ? 0 : 1;
0N/A Expression superArgs[] = new Expression[-outerArg + args.length];
0N/A for (int i = outerArg ; i < args.length ; i++) {
0N/A superArgs[-outerArg + i] = new IdentifierExpression(names[i]);
0N/A }
0N/A long where = getWhere();
0N/A Expression superExp;
0N/A if (outerArg == 0) {
0N/A superExp = new SuperExpression(where);
0N/A } else {
0N/A superExp = new SuperExpression(where,
0N/A new IdentifierExpression(names[0]));
0N/A }
0N/A Expression superCall = new MethodExpression(where,
0N/A superExp, idInit,
0N/A superArgs);
0N/A Statement body[] = { new ExpressionStatement(where, superCall) };
0N/A Node code = new CompoundStatement(where, body);
0N/A int mod = M_SYNTHETIC; // ISSUE: make M_PRIVATE, with wrapper?
0N/A env.makeMemberDefinition(env, where, this, null,
0N/A mod, t, idInit, names, null, code);
0N/A }
0N/A
0N/A /**
0N/A * Convert class modifiers to a string for diagnostic purposes.
0N/A * Accepts modifiers applicable to inner classes and that appear
0N/A * in the InnerClasses attribute only, as well as those that may
0N/A * appear in the class modifier proper.
0N/A */
0N/A
0N/A private static int classModifierBits[] =
0N/A { ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, ACC_FINAL,
0N/A ACC_INTERFACE, ACC_ABSTRACT, ACC_SUPER, M_ANONYMOUS, M_LOCAL,
0N/A M_STRICTFP, ACC_STRICT};
0N/A
0N/A private static String classModifierNames[] =
0N/A { "PUBLIC", "PRIVATE", "PROTECTED", "STATIC", "FINAL",
0N/A "INTERFACE", "ABSTRACT", "SUPER", "ANONYMOUS", "LOCAL",
0N/A "STRICTFP", "STRICT"};
0N/A
0N/A static String classModifierString(int mods) {
0N/A String s = "";
0N/A for (int i = 0; i < classModifierBits.length; i++) {
0N/A if ((mods & classModifierBits[i]) != 0) {
0N/A s = s + " " + classModifierNames[i];
0N/A mods &= ~classModifierBits[i];
0N/A }
0N/A }
0N/A if (mods != 0) {
0N/A s = s + " ILLEGAL:" + Integer.toHexString(mods);
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A /**
0N/A * Find or create an access method for a private member,
0N/A * or return null if this is not possible.
0N/A */
0N/A public MemberDefinition getAccessMember(Environment env, Context ctx,
0N/A MemberDefinition field, boolean isSuper) {
0N/A return getAccessMember(env, ctx, field, false, isSuper);
0N/A }
0N/A
0N/A public MemberDefinition getUpdateMember(Environment env, Context ctx,
0N/A MemberDefinition field, boolean isSuper) {
0N/A if (!field.isVariable()) {
0N/A throw new CompilerError("method");
0N/A }
0N/A return getAccessMember(env, ctx, field, true, isSuper);
0N/A }
0N/A
0N/A private MemberDefinition getAccessMember(Environment env, Context ctx,
0N/A MemberDefinition field,
0N/A boolean isUpdate,
0N/A boolean isSuper) {
0N/A
0N/A // The 'isSuper' argument is really only meaningful when the
0N/A // target member is a method, in which case an 'invokespecial'
0N/A // is needed. For fields, 'getfield' and 'putfield' instructions
0N/A // are generated in either case, and 'isSuper' currently plays
0N/A // no essential role. Nonetheless, we maintain the distinction
0N/A // consistently for the time being.
0N/A
0N/A boolean isStatic = field.isStatic();
0N/A boolean isMethod = field.isMethod();
0N/A
0N/A // Find pre-existing access method.
0N/A // In the case of a field access method, we only look for the getter.
0N/A // A getter is always created whenever a setter is.
0N/A // QUERY: Why doesn't the 'MemberDefinition' object for the field
0N/A // itself just have fields for its getter and setter?
0N/A MemberDefinition af;
0N/A for (af = getFirstMember(); af != null; af = af.getNextMember()) {
0N/A if (af.getAccessMethodTarget() == field) {
0N/A if (isMethod && af.isSuperAccessMethod() == isSuper) {
0N/A break;
0N/A }
0N/A // Distinguish the getter and the setter by the number of
0N/A // arguments.
0N/A int nargs = af.getType().getArgumentTypes().length;
0N/A // This was (nargs == (isStatic ? 0 : 1) + (isUpdate ? 1 : 0))
0N/A // in order to find a setter as well as a getter. This caused
0N/A // allocation of multiple getters.
0N/A if (nargs == (isStatic ? 0 : 1)) {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (af != null) {
0N/A if (!isUpdate) {
0N/A return af;
0N/A } else {
0N/A MemberDefinition uf = af.getAccessUpdateMember();
0N/A if (uf != null) {
0N/A return uf;
0N/A }
0N/A }
0N/A } else if (isUpdate) {
0N/A // must find or create the getter before creating the setter
0N/A af = getAccessMember(env, ctx, field, false, isSuper);
0N/A }
0N/A
0N/A // If we arrive here, we are creating a new access member.
0N/A
0N/A Identifier anm;
0N/A Type dummyType = null;
0N/A
0N/A if (field.isConstructor()) {
0N/A // For a constructor, we use the same name as for all
0N/A // constructors ("<init>"), but add a distinguishing
0N/A // argument of an otherwise unused "dummy" type.
0N/A anm = idInit;
0N/A // Get the dummy class, creating it if necessary.
0N/A SourceClass outerMostClass = (SourceClass)getTopClass();
0N/A dummyType = outerMostClass.dummyArgumentType;
0N/A if (dummyType == null) {
0N/A // Create dummy class.
0N/A IdentifierToken sup =
0N/A new IdentifierToken(0, idJavaLangObject);
0N/A IdentifierToken interfaces[] = {};
0N/A IdentifierToken t = new IdentifierToken(0, idNull);
0N/A int mod = M_ANONYMOUS | M_STATIC | M_SYNTHETIC;
0N/A // If an interface has a public inner class, the dummy class for
0N/A // the constructor must always be accessible. Fix for 4221648.
0N/A if (outerMostClass.isInterface()) {
0N/A mod |= M_PUBLIC;
0N/A }
0N/A ClassDefinition dummyClass =
0N/A toplevelEnv.makeClassDefinition(toplevelEnv,
0N/A 0, t, null, mod,
0N/A sup, interfaces,
0N/A outerMostClass);
0N/A // Check the class.
0N/A // It is likely that a full check is not really necessary,
0N/A // but it is essential that the class be marked as parsed.
0N/A dummyClass.getClassDeclaration().setDefinition(dummyClass, CS_PARSED);
0N/A Expression argsX[] = {};
0N/A Type argTypesX[] = {};
0N/A try {
0N/A ClassDefinition supcls =
0N/A toplevelEnv.getClassDefinition(idJavaLangObject);
0N/A dummyClass.checkLocalClass(toplevelEnv, null,
0N/A new Vset(), supcls, argsX, argTypesX);
0N/A } catch (ClassNotFound ee) {};
0N/A // Get class type.
0N/A dummyType = dummyClass.getType();
0N/A outerMostClass.dummyArgumentType = dummyType;
0N/A }
0N/A } else {
0N/A // Otherwise, we use the name "access$N", for the
0N/A // smallest value of N >= 0 yielding an unused name.
0N/A for (int i = 0; ; i++) {
0N/A anm = Identifier.lookup(prefixAccess + i);
0N/A if (getFirstMatch(anm) == null) {
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A Type argTypes[];
0N/A Type t = field.getType();
0N/A
0N/A if (isStatic) {
0N/A if (!isMethod) {
0N/A if (!isUpdate) {
0N/A Type at[] = { };
0N/A argTypes = at;
0N/A t = Type.tMethod(t); // nullary getter
0N/A } else {
0N/A Type at[] = { t };
0N/A argTypes = at;
0N/A t = Type.tMethod(Type.tVoid, argTypes); // unary setter
0N/A }
0N/A } else {
0N/A // Since constructors are never static, we don't
0N/A // have to worry about a dummy argument here.
0N/A argTypes = t.getArgumentTypes();
0N/A }
0N/A } else {
0N/A // All access methods for non-static members get an explicit
0N/A // 'this' pointer as an extra argument, as the access methods
0N/A // themselves must be static. EXCEPTION: Access methods for
0N/A // constructors are non-static.
0N/A Type classType = this.getType();
0N/A if (!isMethod) {
0N/A if (!isUpdate) {
0N/A Type at[] = { classType };
0N/A argTypes = at;
0N/A t = Type.tMethod(t, argTypes); // nullary getter
0N/A } else {
0N/A Type at[] = { classType, t };
0N/A argTypes = at;
0N/A t = Type.tMethod(Type.tVoid, argTypes); // unary setter
0N/A }
0N/A } else {
0N/A // Target is a method, possibly a constructor.
0N/A Type at[] = t.getArgumentTypes();
0N/A int nargs = at.length;
0N/A if (field.isConstructor()) {
0N/A // Access method is a constructor.
0N/A // Requires a dummy argument.
0N/A MemberDefinition outerThisArg =
0N/A ((SourceMember)field).getOuterThisArg();
0N/A if (outerThisArg != null) {
0N/A // Outer instance link must be the first argument.
0N/A // The following is a sanity check that will catch
0N/A // most cases in which in this requirement is violated.
0N/A if (at[0] != outerThisArg.getType()) {
0N/A throw new CompilerError("misplaced outer this");
0N/A }
0N/A // Strip outer 'this' argument.
0N/A // It will be added back when the access method is checked.
0N/A argTypes = new Type[nargs];
0N/A argTypes[0] = dummyType;
0N/A for (int i = 1; i < nargs; i++) {
0N/A argTypes[i] = at[i];
0N/A }
0N/A } else {
0N/A // There is no outer instance.
0N/A argTypes = new Type[nargs+1];
0N/A argTypes[0] = dummyType;
0N/A for (int i = 0; i < nargs; i++) {
0N/A argTypes[i+1] = at[i];
0N/A }
0N/A }
0N/A } else {
0N/A // Access method is static.
0N/A // Requires an explicit 'this' argument.
0N/A argTypes = new Type[nargs+1];
0N/A argTypes[0] = classType;
0N/A for (int i = 0; i < nargs; i++) {
0N/A argTypes[i+1] = at[i];
0N/A }
0N/A }
0N/A t = Type.tMethod(t.getReturnType(), argTypes);
0N/A }
0N/A }
0N/A
0N/A int nlen = argTypes.length;
0N/A long where = field.getWhere();
0N/A IdentifierToken names[] = new IdentifierToken[nlen];
0N/A for (int i = 0; i < nlen; i++) {
0N/A names[i] = new IdentifierToken(where, Identifier.lookup("$"+i));
0N/A }
0N/A
0N/A Expression access = null;
0N/A Expression thisArg = null;
0N/A Expression args[] = null;
0N/A
0N/A if (isStatic) {
0N/A args = new Expression[nlen];
0N/A for (int i = 0 ; i < nlen ; i++) {
0N/A args[i] = new IdentifierExpression(names[i]);
0N/A }
0N/A } else {
0N/A if (field.isConstructor()) {
0N/A // Constructor access method is non-static, so
0N/A // 'this' works normally.
0N/A thisArg = new ThisExpression(where);
0N/A // Remove dummy argument, as it is not
0N/A // passed to the target method.
0N/A args = new Expression[nlen-1];
0N/A for (int i = 1 ; i < nlen ; i++) {
0N/A args[i-1] = new IdentifierExpression(names[i]);
0N/A }
0N/A } else {
0N/A // Non-constructor access method is static, so
0N/A // we use the first argument as 'this'.
0N/A thisArg = new IdentifierExpression(names[0]);
0N/A // Remove first argument.
0N/A args = new Expression[nlen-1];
0N/A for (int i = 1 ; i < nlen ; i++) {
0N/A args[i-1] = new IdentifierExpression(names[i]);
0N/A }
0N/A }
0N/A access = thisArg;
0N/A }
0N/A
0N/A if (!isMethod) {
0N/A access = new FieldExpression(where, access, field);
0N/A if (isUpdate) {
0N/A access = new AssignExpression(where, access, args[0]);
0N/A }
0N/A } else {
0N/A // If true, 'isSuper' forces a non-virtual call.
0N/A access = new MethodExpression(where, access, field, args, isSuper);
0N/A }
0N/A
0N/A Statement code;
0N/A if (t.getReturnType().isType(TC_VOID)) {
0N/A code = new ExpressionStatement(where, access);
0N/A } else {
0N/A code = new ReturnStatement(where, access);
0N/A }
0N/A Statement body[] = { code };
0N/A code = new CompoundStatement(where, body);
0N/A
0N/A // Access methods are now static (constructors excepted), and no longer final.
0N/A // This change was mandated by the interaction of the access method
0N/A // naming conventions and the restriction against overriding final
0N/A // methods.
0N/A int mod = M_SYNTHETIC;
0N/A if (!field.isConstructor()) {
0N/A mod |= M_STATIC;
0N/A }
0N/A
0N/A // Create the synthetic method within the class in which the referenced
0N/A // private member appears. The 'env' argument to 'makeMemberDefinition'
0N/A // is suspect because it represents the environment at the point at
0N/A // which a reference takes place, while it should represent the
0N/A // environment in which the definition of the synthetic method appears.
0N/A // We get away with this because 'env' is used only to access globals
0N/A // such as 'Environment.error', and also as an argument to
0N/A // 'resolveTypeStructure', which immediately discards it using
0N/A // 'setupEnv'. Apparently, the current definition of 'setupEnv'
0N/A // represents a design change that has not been thoroughly propagated.
0N/A // An access method is declared with same list of exceptions as its
0N/A // target. As the exceptions are simply listed by name, the correctness
0N/A // of this approach requires that the access method be checked
0N/A // (name-resolved) in the same context as its target method This
0N/A // should always be the case.
0N/A SourceMember newf = (SourceMember)
0N/A env.makeMemberDefinition(env, where, this,
0N/A null, mod, t, anm, names,
0N/A field.getExceptionIds(), code);
0N/A // Just to be safe, copy over the name-resolved exceptions from the
0N/A // target so that the context in which the access method is checked
0N/A // doesn't matter.
0N/A newf.setExceptions(field.getExceptions(env));
0N/A
0N/A newf.setAccessMethodTarget(field);
0N/A if (isUpdate) {
0N/A af.setAccessUpdateMember(newf);
0N/A }
0N/A newf.setIsSuperAccessMethod(isSuper);
0N/A
0N/A // The call to 'check' is not needed, as the access method will be
0N/A // checked by the containing class after it is added. This is the
0N/A // idiom followed in the implementation of class literals. (See
0N/A // 'FieldExpression.java'.) In any case, the context is wrong in the
0N/A // call below. The access method must be checked in the context in
0N/A // which it is declared, i.e., the class containing the referenced
0N/A // private member, not the (inner) class in which the original member
0N/A // reference occurs.
0N/A //
0N/A // try {
0N/A // newf.check(env, ctx, new Vset());
0N/A // } catch (ClassNotFound ee) {
0N/A // env.error(where, "class.not.found", ee.name, this);
0N/A // }
0N/A
0N/A // The comment above is inaccurate. While it is often the case
0N/A // that the containing class will check the access method, this is
0N/A // by no means guaranteed. In fact, an access method may be added
0N/A // after the checking of its class is complete. In this case, however,
0N/A // the context in which the class was checked will have been saved in
0N/A // the class definition object (by the fix for 4095716), allowing us
0N/A // to check the field now, and in the correct context.
0N/A // This fixes bug 4098093.
0N/A
0N/A Context checkContext = newf.getClassDefinition().getClassContext();
0N/A if (checkContext != null) {
0N/A //System.out.println("checking late addition: " + this);
0N/A try {
0N/A newf.check(env, checkContext, new Vset());
0N/A } catch (ClassNotFound ee) {
0N/A env.error(where, "class.not.found", ee.name, this);
0N/A }
0N/A }
0N/A
0N/A
0N/A //System.out.println("[Access member '" +
0N/A // newf + "' created for field '" +
0N/A // field +"' in class '" + this + "']");
0N/A
0N/A return newf;
0N/A }
0N/A
0N/A /**
0N/A * Find an inner class of 'this', chosen arbitrarily.
0N/A * Result is always an actual class, never an interface.
0N/A * Returns null if none found.
0N/A */
0N/A SourceClass findLookupContext() {
0N/A // Look for an immediate inner class.
0N/A for (MemberDefinition f = getFirstMember();
0N/A f != null;
0N/A f = f.getNextMember()) {
0N/A if (f.isInnerClass()) {
0N/A SourceClass ic = (SourceClass)f.getInnerClass();
0N/A if (!ic.isInterface()) {
0N/A return ic;
0N/A }
0N/A }
0N/A }
0N/A // Look for a class nested within an immediate inner interface.
0N/A // At this point, we have given up on finding a minimally-nested
0N/A // class (which would require a breadth-first traversal). It doesn't
0N/A // really matter which inner class we find.
0N/A for (MemberDefinition f = getFirstMember();
0N/A f != null;
0N/A f = f.getNextMember()) {
0N/A if (f.isInnerClass()) {
0N/A SourceClass lc =
0N/A ((SourceClass)f.getInnerClass()).findLookupContext();
0N/A if (lc != null) {
0N/A return lc;
0N/A }
0N/A }
0N/A }
0N/A // No inner classes.
0N/A return null;
0N/A }
0N/A
0N/A private MemberDefinition lookup = null;
0N/A
0N/A /**
0N/A * Get helper method for class literal lookup.
0N/A */
0N/A public MemberDefinition getClassLiteralLookup(long fwhere) {
0N/A
0N/A // If we have already created a lookup method, reuse it.
0N/A if (lookup != null) {
0N/A return lookup;
0N/A }
0N/A
0N/A // If the current class is a nested class, make sure we put the
0N/A // lookup method in the outermost class. Set 'lookup' for the
0N/A // intervening inner classes so we won't have to do the search
0N/A // again.
0N/A if (outerClass != null) {
0N/A lookup = outerClass.getClassLiteralLookup(fwhere);
0N/A return lookup;
0N/A }
0N/A
0N/A // If we arrive here, there was no existing 'class$' method.
0N/A
0N/A ClassDefinition c = this;
0N/A boolean needNewClass = false;
0N/A
0N/A if (isInterface()) {
0N/A // The top-level type is an interface. Try to find an existing
0N/A // inner class in which to create the helper method. Any will do.
0N/A c = findLookupContext();
0N/A if (c == null) {
0N/A // The interface has no inner classes. Create an anonymous
0N/A // inner class to hold the helper method, as an interface must
0N/A // not have any methods. The tests above for prior creation
0N/A // of a 'class$' method assure that only one such class is
0N/A // allocated for each outermost class containing a class
0N/A // literal embedded somewhere within. Part of fix for 4055017.
0N/A needNewClass = true;
0N/A IdentifierToken sup =
0N/A new IdentifierToken(fwhere, idJavaLangObject);
0N/A IdentifierToken interfaces[] = {};
0N/A IdentifierToken t = new IdentifierToken(fwhere, idNull);
0N/A int mod = M_PUBLIC | M_ANONYMOUS | M_STATIC | M_SYNTHETIC;
0N/A c = (SourceClass)
0N/A toplevelEnv.makeClassDefinition(toplevelEnv,
0N/A fwhere, t, null, mod,
0N/A sup, interfaces, this);
0N/A }
0N/A }
0N/A
0N/A
0N/A // The name of the class-getter stub is "class$"
0N/A Identifier idDClass = Identifier.lookup(prefixClass);
0N/A Type strarg[] = { Type.tString };
0N/A
0N/A // Some sanity checks of questionable value.
0N/A //
0N/A // This check became useless after matchMethod() was modified
0N/A // to not return synthetic methods.
0N/A //
0N/A //try {
0N/A // lookup = c.matchMethod(toplevelEnv, c, idDClass, strarg);
0N/A //} catch (ClassNotFound ee) {
0N/A // throw new CompilerError("unexpected missing class");
0N/A //} catch (AmbiguousMember ee) {
0N/A // throw new CompilerError("synthetic name clash");
0N/A //}
0N/A //if (lookup != null && lookup.getClassDefinition() == c) {
0N/A // // Error if method found was not inherited.
0N/A // throw new CompilerError("unexpected duplicate");
0N/A //}
0N/A // Some sanity checks of questionable value.
0N/A
0N/A /* // The helper function looks like this.
0N/A * // It simply maps a checked exception to an unchecked one.
0N/A * static Class class$(String class$) {
0N/A * try { return Class.forName(class$); }
0N/A * catch (ClassNotFoundException forName) {
0N/A * throw new NoClassDefFoundError(forName.getMessage());
0N/A * }
0N/A * }
0N/A */
0N/A long w = c.getWhere();
0N/A IdentifierToken arg = new IdentifierToken(w, idDClass);
0N/A Expression e = new IdentifierExpression(arg);
0N/A Expression a1[] = { e };
0N/A Identifier idForName = Identifier.lookup("forName");
0N/A e = new MethodExpression(w, new TypeExpression(w, Type.tClassDesc),
0N/A idForName, a1);
0N/A Statement body = new ReturnStatement(w, e);
0N/A // map the exceptions
0N/A Identifier idClassNotFound =
0N/A Identifier.lookup("java.lang.ClassNotFoundException");
0N/A Identifier idNoClassDefFound =
0N/A Identifier.lookup("java.lang.NoClassDefFoundError");
0N/A Type ctyp = Type.tClass(idClassNotFound);
0N/A Type exptyp = Type.tClass(idNoClassDefFound);
0N/A Identifier idGetMessage = Identifier.lookup("getMessage");
0N/A e = new IdentifierExpression(w, idForName);
0N/A e = new MethodExpression(w, e, idGetMessage, new Expression[0]);
0N/A Expression a2[] = { e };
0N/A e = new NewInstanceExpression(w, new TypeExpression(w, exptyp), a2);
0N/A Statement handler = new CatchStatement(w, new TypeExpression(w, ctyp),
0N/A new IdentifierToken(idForName),
0N/A new ThrowStatement(w, e));
0N/A Statement handlers[] = { handler };
0N/A body = new TryStatement(w, body, handlers);
0N/A
0N/A Type mtype = Type.tMethod(Type.tClassDesc, strarg);
0N/A IdentifierToken args[] = { arg };
0N/A
0N/A // Use default (package) access. If private, an access method would
0N/A // be needed in the event that the class literal belonged to an interface.
0N/A // Also, making it private tickles bug 4098316.
0N/A lookup = toplevelEnv.makeMemberDefinition(toplevelEnv, w,
0N/A c, null,
0N/A M_STATIC | M_SYNTHETIC,
0N/A mtype, idDClass,
0N/A args, null, body);
0N/A
0N/A // If a new class was created to contain the helper method,
0N/A // check it now.
0N/A if (needNewClass) {
0N/A if (c.getClassDeclaration().getStatus() == CS_CHECKED) {
0N/A throw new CompilerError("duplicate check");
0N/A }
0N/A c.getClassDeclaration().setDefinition(c, CS_PARSED);
0N/A Expression argsX[] = {};
0N/A Type argTypesX[] = {};
0N/A try {
0N/A ClassDefinition sup =
0N/A toplevelEnv.getClassDefinition(idJavaLangObject);
0N/A c.checkLocalClass(toplevelEnv, null,
0N/A new Vset(), sup, argsX, argTypesX);
0N/A } catch (ClassNotFound ee) {};
0N/A }
0N/A
0N/A return lookup;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * A list of active ongoing compilations. This list
0N/A * is used to stop two compilations from saving the
0N/A * same class.
0N/A */
0N/A private static Vector active = new Vector();
0N/A
0N/A /**
0N/A * Compile this class
0N/A */
0N/A public void compile(OutputStream out)
0N/A throws InterruptedException, IOException {
0N/A Environment env = toplevelEnv;
0N/A synchronized (active) {
0N/A while (active.contains(getName())) {
0N/A active.wait();
0N/A }
0N/A active.addElement(getName());
0N/A }
0N/A
0N/A try {
0N/A compileClass(env, out);
0N/A } catch (ClassNotFound e) {
0N/A throw new CompilerError(e);
0N/A } finally {
0N/A synchronized (active) {
0N/A active.removeElement(getName());
0N/A active.notifyAll();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Verify that the modifier bits included in 'required' are
0N/A * all present in 'mods', otherwise signal an internal error.
0N/A * Note that errors in the source program may corrupt the modifiers,
0N/A * thus we rely on the fact that 'CompilerError' exceptions are
0N/A * silently ignored after an error message has been issued.
0N/A */
0N/A private static void assertModifiers(int mods, int required) {
0N/A if ((mods & required) != required) {
0N/A throw new CompilerError("illegal class modifiers");
0N/A }
0N/A }
0N/A
0N/A protected void compileClass(Environment env, OutputStream out)
0N/A throws IOException, ClassNotFound {
0N/A Vector variables = new Vector();
0N/A Vector methods = new Vector();
0N/A Vector innerClasses = new Vector();
0N/A CompilerMember init = new CompilerMember(new MemberDefinition(getWhere(), this, M_STATIC, Type.tMethod(Type.tVoid), idClassInit, null, null), new Assembler());
0N/A Context ctx = new Context((Context)null, init.field);
0N/A
0N/A for (ClassDefinition def = this; def.isInnerClass(); def = def.getOuterClass()) {
0N/A innerClasses.addElement(def);
0N/A }
0N/A // Reverse the order, so that outer levels come first:
0N/A int ncsize = innerClasses.size();
0N/A for (int i = ncsize; --i >= 0; )
0N/A innerClasses.addElement(innerClasses.elementAt(i));
0N/A for (int i = ncsize; --i >= 0; )
0N/A innerClasses.removeElementAt(i);
0N/A
0N/A // System.out.println("compile class " + getName());
0N/A
0N/A boolean haveDeprecated = this.isDeprecated();
0N/A boolean haveSynthetic = this.isSynthetic();
0N/A boolean haveConstantValue = false;
0N/A boolean haveExceptions = false;
0N/A
0N/A // Generate code for all fields
0N/A for (SourceMember field = (SourceMember)getFirstMember();
0N/A field != null;
0N/A field = (SourceMember)field.getNextMember()) {
0N/A
0N/A //System.out.println("compile field " + field.getName());
0N/A
0N/A haveDeprecated |= field.isDeprecated();
0N/A haveSynthetic |= field.isSynthetic();
0N/A
0N/A try {
0N/A if (field.isMethod()) {
0N/A haveExceptions |=
0N/A (field.getExceptions(env).length > 0);
0N/A
0N/A if (field.isInitializer()) {
0N/A if (field.isStatic()) {
0N/A field.code(env, init.asm);
0N/A }
0N/A } else {
0N/A CompilerMember f =
0N/A new CompilerMember(field, new Assembler());
0N/A field.code(env, f.asm);
0N/A methods.addElement(f);
0N/A }
0N/A } else if (field.isInnerClass()) {
0N/A innerClasses.addElement(field.getInnerClass());
0N/A } else if (field.isVariable()) {
0N/A field.inline(env);
0N/A CompilerMember f = new CompilerMember(field, null);
0N/A variables.addElement(f);
0N/A if (field.isStatic()) {
0N/A field.codeInit(env, ctx, init.asm);
0N/A
0N/A }
0N/A haveConstantValue |=
0N/A (field.getInitialValue() != null);
0N/A }
0N/A } catch (CompilerError ee) {
0N/A ee.printStackTrace();
0N/A env.error(field, 0, "generic",
0N/A field.getClassDeclaration() + ":" + field +
0N/A "@" + ee.toString(), null, null);
0N/A }
0N/A }
0N/A if (!init.asm.empty()) {
0N/A init.asm.add(getWhere(), opc_return, true);
0N/A methods.addElement(init);
0N/A }
0N/A
0N/A // bail out if there were any errors
0N/A if (getNestError()) {
0N/A return;
0N/A }
0N/A
0N/A int nClassAttrs = 0;
0N/A
0N/A // Insert constants
0N/A if (methods.size() > 0) {
0N/A tab.put("Code");
0N/A }
0N/A if (haveConstantValue) {
0N/A tab.put("ConstantValue");
0N/A }
0N/A
0N/A String sourceFile = null;
0N/A if (env.debug_source()) {
0N/A sourceFile = ((ClassFile)getSource()).getName();
0N/A tab.put("SourceFile");
0N/A tab.put(sourceFile);
0N/A nClassAttrs += 1;
0N/A }
0N/A
0N/A if (haveExceptions) {
0N/A tab.put("Exceptions");
0N/A }
0N/A
0N/A if (env.debug_lines()) {
0N/A tab.put("LineNumberTable");
0N/A }
0N/A if (haveDeprecated) {
0N/A tab.put("Deprecated");
0N/A if (this.isDeprecated()) {
0N/A nClassAttrs += 1;
0N/A }
0N/A }
0N/A if (haveSynthetic) {
0N/A tab.put("Synthetic");
0N/A if (this.isSynthetic()) {
0N/A nClassAttrs += 1;
0N/A }
0N/A }
0N/A// JCOV
0N/A if (env.coverage()) {
0N/A nClassAttrs += 2; // AbsoluteSourcePath, TimeStamp
0N/A tab.put("AbsoluteSourcePath");
0N/A tab.put("TimeStamp");
0N/A tab.put("CoverageTable");
0N/A }
0N/A// end JCOV
0N/A if (env.debug_vars()) {
0N/A tab.put("LocalVariableTable");
0N/A }
0N/A if (innerClasses.size() > 0) {
0N/A tab.put("InnerClasses");
0N/A nClassAttrs += 1; // InnerClasses
0N/A }
0N/A
0N/A// JCOV
0N/A String absoluteSourcePath = "";
0N/A long timeStamp = 0;
0N/A
0N/A if (env.coverage()) {
0N/A absoluteSourcePath = getAbsoluteName();
0N/A timeStamp = System.currentTimeMillis();
0N/A tab.put(absoluteSourcePath);
0N/A }
0N/A// end JCOV
0N/A tab.put(getClassDeclaration());
0N/A if (getSuperClass() != null) {
0N/A tab.put(getSuperClass());
0N/A }
0N/A for (int i = 0 ; i < interfaces.length ; i++) {
0N/A tab.put(interfaces[i]);
0N/A }
0N/A
0N/A // Sort the methods in order to make sure both constant pool
0N/A // entries and methods are in a deterministic order from run
0N/A // to run (this allows comparing class files for a fixed point
0N/A // to validate the compiler)
0N/A CompilerMember[] ordered_methods =
0N/A new CompilerMember[methods.size()];
0N/A methods.copyInto(ordered_methods);
0N/A java.util.Arrays.sort(ordered_methods);
0N/A for (int i=0; i<methods.size(); i++)
0N/A methods.setElementAt(ordered_methods[i], i);
0N/A
0N/A // Optimize Code and Collect method constants
0N/A for (Enumeration e = methods.elements() ; e.hasMoreElements() ; ) {
0N/A CompilerMember f = (CompilerMember)e.nextElement();
0N/A try {
0N/A f.asm.optimize(env);
0N/A f.asm.collect(env, f.field, tab);
0N/A tab.put(f.name);
0N/A tab.put(f.sig);
0N/A ClassDeclaration exp[] = f.field.getExceptions(env);
0N/A for (int i = 0 ; i < exp.length ; i++) {
0N/A tab.put(exp[i]);
0N/A }
0N/A } catch (Exception ee) {
0N/A ee.printStackTrace();
0N/A env.error(f.field, -1, "generic", f.field.getName() + "@" + ee.toString(), null, null);
0N/A f.asm.listing(System.out);
0N/A }
0N/A }
0N/A
0N/A // Collect field constants
0N/A for (Enumeration e = variables.elements() ; e.hasMoreElements() ; ) {
0N/A CompilerMember f = (CompilerMember)e.nextElement();
0N/A tab.put(f.name);
0N/A tab.put(f.sig);
0N/A
0N/A Object val = f.field.getInitialValue();
0N/A if (val != null) {
0N/A tab.put((val instanceof String) ? new StringExpression(f.field.getWhere(), (String)val) : val);
0N/A }
0N/A }
0N/A
0N/A // Collect inner class constants
0N/A for (Enumeration e = innerClasses.elements();
0N/A e.hasMoreElements() ; ) {
0N/A ClassDefinition inner = (ClassDefinition)e.nextElement();
0N/A tab.put(inner.getClassDeclaration());
0N/A
0N/A // If the inner class is local, we do not need to add its
0N/A // outer class here -- the outer_class_info_index is zero.
0N/A if (!inner.isLocal()) {
0N/A ClassDefinition outer = inner.getOuterClass();
0N/A tab.put(outer.getClassDeclaration());
0N/A }
0N/A
0N/A // If the local name of the class is idNull, don't bother to
0N/A // add it to the constant pool. We won't need it.
0N/A Identifier inner_local_name = inner.getLocalName();
0N/A if (inner_local_name != idNull) {
0N/A tab.put(inner_local_name.toString());
0N/A }
0N/A }
0N/A
0N/A // Write header
0N/A DataOutputStream data = new DataOutputStream(out);
0N/A data.writeInt(JAVA_MAGIC);
0N/A data.writeShort(toplevelEnv.getMinorVersion());
0N/A data.writeShort(toplevelEnv.getMajorVersion());
0N/A tab.write(env, data);
0N/A
0N/A // Write class information
0N/A int cmods = getModifiers() & MM_CLASS;
0N/A
0N/A // Certain modifiers are implied:
0N/A // 1. Any interface (nested or not) is implicitly deemed to be abstract,
0N/A // whether it is explicitly marked so or not. (Java 1.0.)
0N/A // 2. A interface which is a member of a type is implicitly deemed to
0N/A // be static, whether it is explicitly marked so or not.
0N/A // 3a. A type which is a member of an interface is implicitly deemed
0N/A // to be public, whether it is explicitly marked so or not.
0N/A // 3b. A type which is a member of an interface is implicitly deemed
0N/A // to be static, whether it is explicitly marked so or not.
0N/A // All of these rules are implemented in 'BatchParser.beginClass',
0N/A // but the results are verified here.
0N/A
0N/A if (isInterface()) {
0N/A // Rule 1.
0N/A // The VM spec states that ACC_ABSTRACT must be set when
0N/A // ACC_INTERFACE is; this was not done by javac prior to 1.2,
0N/A // and the runtime compensates by setting it. Making sure
0N/A // it is set here will allow the runtime hack to eventually
0N/A // be removed. Rule 2 doesn't apply to transformed modifiers.
0N/A assertModifiers(cmods, ACC_ABSTRACT);
0N/A } else {
0N/A // Contrary to the JVM spec, we only set ACC_SUPER for classes,
0N/A // not interfaces. This is a workaround for a bug in IE3.0,
0N/A // which refuses interfaces with ACC_SUPER on.
0N/A cmods |= ACC_SUPER;
0N/A }
0N/A
0N/A // If this is a nested class, transform access modifiers.
0N/A if (outerClass != null) {
0N/A // If private, transform to default (package) access.
0N/A // If protected, transform to public.
0N/A // M_PRIVATE and M_PROTECTED are already masked off by MM_CLASS above.
0N/A // cmods &= ~(M_PRIVATE | M_PROTECTED);
0N/A if (isProtected()) cmods |= M_PUBLIC;
0N/A // Rule 3a. Note that Rule 3b doesn't apply to transformed modifiers.
0N/A if (outerClass.isInterface()) {
0N/A assertModifiers(cmods, M_PUBLIC);
0N/A }
0N/A }
0N/A
0N/A data.writeShort(cmods);
0N/A
0N/A if (env.dumpModifiers()) {
0N/A Identifier cn = getName();
0N/A Identifier nm =
0N/A Identifier.lookup(cn.getQualifier(), cn.getFlatName());
0N/A System.out.println();
0N/A System.out.println("CLASSFILE " + nm);
0N/A System.out.println("---" + classModifierString(cmods));
0N/A }
0N/A
0N/A data.writeShort(tab.index(getClassDeclaration()));
0N/A data.writeShort((getSuperClass() != null) ? tab.index(getSuperClass()) : 0);
0N/A data.writeShort(interfaces.length);
0N/A for (int i = 0 ; i < interfaces.length ; i++) {
0N/A data.writeShort(tab.index(interfaces[i]));
0N/A }
0N/A
0N/A // write variables
0N/A ByteArrayOutputStream buf = new ByteArrayOutputStream(256);
0N/A ByteArrayOutputStream attbuf = new ByteArrayOutputStream(256);
0N/A DataOutputStream databuf = new DataOutputStream(buf);
0N/A
0N/A data.writeShort(variables.size());
0N/A for (Enumeration e = variables.elements() ; e.hasMoreElements() ; ) {
0N/A CompilerMember f = (CompilerMember)e.nextElement();
0N/A Object val = f.field.getInitialValue();
0N/A
0N/A data.writeShort(f.field.getModifiers() & MM_FIELD);
0N/A data.writeShort(tab.index(f.name));
0N/A data.writeShort(tab.index(f.sig));
0N/A
0N/A int fieldAtts = (val != null ? 1 : 0);
0N/A boolean dep = f.field.isDeprecated();
0N/A boolean syn = f.field.isSynthetic();
0N/A fieldAtts += (dep ? 1 : 0) + (syn ? 1 : 0);
0N/A
0N/A data.writeShort(fieldAtts);
0N/A if (val != null) {
0N/A data.writeShort(tab.index("ConstantValue"));
0N/A data.writeInt(2);
0N/A data.writeShort(tab.index((val instanceof String) ? new StringExpression(f.field.getWhere(), (String)val) : val));
0N/A }
0N/A if (dep) {
0N/A data.writeShort(tab.index("Deprecated"));
0N/A data.writeInt(0);
0N/A }
0N/A if (syn) {
0N/A data.writeShort(tab.index("Synthetic"));
0N/A data.writeInt(0);
0N/A }
0N/A }
0N/A
0N/A // write methods
0N/A
0N/A data.writeShort(methods.size());
0N/A for (Enumeration e = methods.elements() ; e.hasMoreElements() ; ) {
0N/A CompilerMember f = (CompilerMember)e.nextElement();
0N/A
0N/A int xmods = f.field.getModifiers() & MM_METHOD;
0N/A // Transform floating point modifiers. M_STRICTFP
0N/A // of member + status of enclosing class turn into
0N/A // ACC_STRICT bit.
0N/A if (((xmods & M_STRICTFP)!=0) || ((cmods & M_STRICTFP)!=0)) {
0N/A xmods |= ACC_STRICT;
0N/A } else {
0N/A // Use the default
0N/A if (env.strictdefault()) {
0N/A xmods |= ACC_STRICT;
0N/A }
0N/A }
0N/A data.writeShort(xmods);
0N/A
0N/A data.writeShort(tab.index(f.name));
0N/A data.writeShort(tab.index(f.sig));
0N/A ClassDeclaration exp[] = f.field.getExceptions(env);
0N/A int methodAtts = ((exp.length > 0) ? 1 : 0);
0N/A boolean dep = f.field.isDeprecated();
0N/A boolean syn = f.field.isSynthetic();
0N/A methodAtts += (dep ? 1 : 0) + (syn ? 1 : 0);
0N/A
0N/A if (!f.asm.empty()) {
0N/A data.writeShort(methodAtts+1);
0N/A f.asm.write(env, databuf, f.field, tab);
0N/A int natts = 0;
0N/A if (env.debug_lines()) {
0N/A natts++;
0N/A }
0N/A// JCOV
0N/A if (env.coverage()) {
0N/A natts++;
0N/A }
0N/A// end JCOV
0N/A if (env.debug_vars()) {
0N/A natts++;
0N/A }
0N/A databuf.writeShort(natts);
0N/A
0N/A if (env.debug_lines()) {
0N/A f.asm.writeLineNumberTable(env, new DataOutputStream(attbuf), tab);
0N/A databuf.writeShort(tab.index("LineNumberTable"));
0N/A databuf.writeInt(attbuf.size());
0N/A attbuf.writeTo(buf);
0N/A attbuf.reset();
0N/A }
0N/A
0N/A//JCOV
0N/A if (env.coverage()) {
0N/A f.asm.writeCoverageTable(env, (ClassDefinition)this, new DataOutputStream(attbuf), tab, f.field.getWhere());
0N/A databuf.writeShort(tab.index("CoverageTable"));
0N/A databuf.writeInt(attbuf.size());
0N/A attbuf.writeTo(buf);
0N/A attbuf.reset();
0N/A }
0N/A// end JCOV
0N/A if (env.debug_vars()) {
0N/A f.asm.writeLocalVariableTable(env, f.field, new DataOutputStream(attbuf), tab);
0N/A databuf.writeShort(tab.index("LocalVariableTable"));
0N/A databuf.writeInt(attbuf.size());
0N/A attbuf.writeTo(buf);
0N/A attbuf.reset();
0N/A }
0N/A
0N/A data.writeShort(tab.index("Code"));
0N/A data.writeInt(buf.size());
0N/A buf.writeTo(data);
0N/A buf.reset();
0N/A } else {
0N/A//JCOV
0N/A if ((env.coverage()) && ((f.field.getModifiers() & M_NATIVE) > 0))
0N/A f.asm.addNativeToJcovTab(env, (ClassDefinition)this);
0N/A// end JCOV
0N/A data.writeShort(methodAtts);
0N/A }
0N/A
0N/A if (exp.length > 0) {
0N/A data.writeShort(tab.index("Exceptions"));
0N/A data.writeInt(2 + exp.length * 2);
0N/A data.writeShort(exp.length);
0N/A for (int i = 0 ; i < exp.length ; i++) {
0N/A data.writeShort(tab.index(exp[i]));
0N/A }
0N/A }
0N/A if (dep) {
0N/A data.writeShort(tab.index("Deprecated"));
0N/A data.writeInt(0);
0N/A }
0N/A if (syn) {
0N/A data.writeShort(tab.index("Synthetic"));
0N/A data.writeInt(0);
0N/A }
0N/A }
0N/A
0N/A // class attributes
0N/A data.writeShort(nClassAttrs);
0N/A
0N/A if (env.debug_source()) {
0N/A data.writeShort(tab.index("SourceFile"));
0N/A data.writeInt(2);
0N/A data.writeShort(tab.index(sourceFile));
0N/A }
0N/A
0N/A if (this.isDeprecated()) {
0N/A data.writeShort(tab.index("Deprecated"));
0N/A data.writeInt(0);
0N/A }
0N/A if (this.isSynthetic()) {
0N/A data.writeShort(tab.index("Synthetic"));
0N/A data.writeInt(0);
0N/A }
0N/A
0N/A// JCOV
0N/A if (env.coverage()) {
0N/A data.writeShort(tab.index("AbsoluteSourcePath"));
0N/A data.writeInt(2);
0N/A data.writeShort(tab.index(absoluteSourcePath));
0N/A data.writeShort(tab.index("TimeStamp"));
0N/A data.writeInt(8);
0N/A data.writeLong(timeStamp);
0N/A }
0N/A// end JCOV
0N/A
0N/A if (innerClasses.size() > 0) {
0N/A data.writeShort(tab.index("InnerClasses"));
0N/A data.writeInt(2 + 2*4*innerClasses.size());
0N/A data.writeShort(innerClasses.size());
0N/A for (Enumeration e = innerClasses.elements() ;
0N/A e.hasMoreElements() ; ) {
0N/A // For each inner class name transformation, we have a record
0N/A // with the following fields:
0N/A //
0N/A // u2 inner_class_info_index; // CONSTANT_Class_info index
0N/A // u2 outer_class_info_index; // CONSTANT_Class_info index
0N/A // u2 inner_name_index; // CONSTANT_Utf8_info index
0N/A // u2 inner_class_access_flags; // access_flags bitmask
0N/A //
0N/A // The spec states that outer_class_info_index is 0 iff
0N/A // the inner class is not a member of its enclosing class (i.e.
0N/A // it is a local or anonymous class). The spec also states
0N/A // that if a class is anonymous then inner_name_index should
0N/A // be 0.
0N/A //
0N/A // See also the initInnerClasses() method in BinaryClass.java.
0N/A
0N/A // Generate inner_class_info_index.
0N/A ClassDefinition inner = (ClassDefinition)e.nextElement();
0N/A data.writeShort(tab.index(inner.getClassDeclaration()));
0N/A
0N/A // Generate outer_class_info_index.
0N/A //
0N/A // Checking isLocal() should probably be enough here,
0N/A // but the check for isAnonymous is added for good
0N/A // measure.
0N/A if (inner.isLocal() || inner.isAnonymous()) {
0N/A data.writeShort(0);
0N/A } else {
0N/A // Query: what about if inner.isInsideLocal()?
0N/A // For now we continue to generate a nonzero
0N/A // outer_class_info_index.
0N/A ClassDefinition outer = inner.getOuterClass();
0N/A data.writeShort(tab.index(outer.getClassDeclaration()));
0N/A }
0N/A
0N/A // Generate inner_name_index.
0N/A Identifier inner_name = inner.getLocalName();
0N/A if (inner_name == idNull) {
0N/A if (!inner.isAnonymous()) {
0N/A throw new CompilerError("compileClass(), anonymous");
0N/A }
0N/A data.writeShort(0);
0N/A } else {
0N/A data.writeShort(tab.index(inner_name.toString()));
0N/A }
0N/A
0N/A // Generate inner_class_access_flags.
0N/A int imods = inner.getInnerClassMember().getModifiers()
0N/A & ACCM_INNERCLASS;
0N/A
0N/A // Certain modifiers are implied for nested types.
0N/A // See rules 1, 2, 3a, and 3b enumerated above.
0N/A // All of these rules are implemented in 'BatchParser.beginClass',
0N/A // but are verified here.
0N/A
0N/A if (inner.isInterface()) {
0N/A // Rules 1 and 2.
0N/A assertModifiers(imods, M_ABSTRACT | M_STATIC);
0N/A }
0N/A if (inner.getOuterClass().isInterface()) {
0N/A // Rules 3a and 3b.
0N/A imods &= ~(M_PRIVATE | M_PROTECTED); // error recovery
0N/A assertModifiers(imods, M_PUBLIC | M_STATIC);
0N/A }
0N/A
0N/A data.writeShort(imods);
0N/A
0N/A if (env.dumpModifiers()) {
0N/A Identifier fn = inner.getInnerClassMember().getName();
0N/A Identifier nm =
0N/A Identifier.lookup(fn.getQualifier(), fn.getFlatName());
0N/A System.out.println("INNERCLASS " + nm);
0N/A System.out.println("---" + classModifierString(imods));
0N/A }
0N/A
0N/A }
0N/A }
0N/A
0N/A // Cleanup
0N/A data.flush();
0N/A tab = null;
0N/A
0N/A// JCOV
0N/A // generate coverage data
0N/A if (env.covdata()) {
0N/A Assembler CovAsm = new Assembler();
0N/A CovAsm.GenVecJCov(env, (ClassDefinition)this, timeStamp);
0N/A }
0N/A// end JCOV
0N/A }
0N/A
0N/A /**
0N/A * Print out the dependencies for this class (-xdepend) option
0N/A */
0N/A
0N/A public void printClassDependencies(Environment env) {
0N/A
0N/A // Only do this if the -xdepend flag is on
0N/A if ( toplevelEnv.print_dependencies() ) {
0N/A
0N/A // Name of java source file this class was in (full path)
0N/A // e.g. /home/ohair/Test.java
0N/A String src = ((ClassFile)getSource()).getAbsoluteName();
0N/A
0N/A // Class name, fully qualified
0N/A // e.g. "java.lang.Object" or "FooBar" or "sun.tools.javac.Main"
0N/A // Inner class names must be mangled, as ordinary '.' qualification
0N/A // is used internally where the spec requires '$' separators.
0N/A // String className = getName().toString();
0N/A String className = Type.mangleInnerType(getName()).toString();
0N/A
0N/A // Line number where class starts in the src file
0N/A long startLine = getWhere() >> WHEREOFFSETBITS;
0N/A
0N/A // Line number where class ends in the src file (not used yet)
0N/A long endLine = getEndPosition() >> WHEREOFFSETBITS;
0N/A
0N/A // First line looks like:
0N/A // CLASS:src,startLine,endLine,className
0N/A System.out.println( "CLASS:"
0N/A + src + ","
0N/A + startLine + ","
0N/A + endLine + ","
0N/A + className);
0N/A
0N/A // For each class this class is dependent on:
0N/A // CLDEP:className1,className2
0N/A // where className1 is the name of the class we are in, and
0N/A // classname2 is the name of the class className1
0N/A // is dependent on.
0N/A for(Enumeration e = deps.elements(); e.hasMoreElements(); ) {
0N/A ClassDeclaration data = (ClassDeclaration) e.nextElement();
0N/A // Mangle name of class dependend on.
0N/A String depName =
0N/A Type.mangleInnerType(data.getName()).toString();
0N/A env.output("CLDEP:" + className + "," + depName);
0N/A }
0N/A }
0N/A }
0N/A}