Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/java/ClassDeclaration.java
38918 views
/*1* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.java;2627/**28* This class represents an Java class declaration. It refers29* to either a binary or source definition.30*31* ClassDefinitions are loaded on demand, this means that32* class declarations are late bound. The definition of the33* class is obtained in stages. The status field describes34* the state of the class definition:35*36* CS_UNDEFINED - the definition is not yet loaded37* CS_UNDECIDED - a binary definition is loaded, but it is38* still unclear if the source definition need to39* be loaded40* CS_BINARY - the binary class is loaded41* CS_PARSED - the class is loaded from the source file, the42* type information is available, but the class has43* not yet been compiled.44* CS_CHECKED - the class is loaded from the source file and has45* been type-checked.46* CS_COMPILED - the class has been type checked, compiled,47* and written out.48* CS_NOTFOUND - no class definition could be found49*50* WARNING: The contents of this source file are not part of any51* supported API. Code that depends on them does so at its own risk:52* they are subject to change or removal without notice.53*/5455public final56class ClassDeclaration implements Constants {57int status;58Type type;59ClassDefinition definition;6061/**62* Constructor63*/64public ClassDeclaration(Identifier name) {65this.type = Type.tClass(name);66}6768/**69* Get the status of the class70*/71public int getStatus() {72return status;73}7475/**76* Get the name of the class77*/78public Identifier getName() {79return type.getClassName();80}8182/**83* Get the type of the class84*/85public Type getType() {86return type;87}8889/**90* Check if the class is defined91*/92public boolean isDefined() {93switch (status) {94case CS_BINARY:95case CS_PARSED:96case CS_CHECKED:97case CS_COMPILED:98return true;99}100return false;101}102103/**104* Get the definition of this class. Returns null if105* the class is not yet defined.106*/107public ClassDefinition getClassDefinition() {108return definition;109}110111/**112* This is a flag for use by getClassDefinition(env). It is113* used to mark that a class has been successfully looked up114* by that method before.115*/116private boolean found = false;117118/**119* Get the definition of this class, if the class is not120* yet defined, load the definition. Loading a class may121* throw various exceptions.122*/123public ClassDefinition getClassDefinition(Environment env)124throws ClassNotFound {125if (tracing) env.dtEvent("getClassDefinition: " +126getName() + ", status " + getStatus());127128// The majority of calls to getClassDefinition() are duplicates.129// This check makes them fast. It also allows us to avoid130// duplicate, useless calls to basicCheck(). In the future it131// would be good to add an additional status value, CS_BASICCHECKED.132if (found) {133return definition;134}135136for(;;) {137switch (status) {138case CS_UNDEFINED:139case CS_UNDECIDED:140case CS_SOURCE:141env.loadDefinition(this);142break;143144case CS_BINARY:145case CS_PARSED:146//+FIX FOR BUGID 4056065147//definition.basicCheck(env);148if (!definition.isInsideLocal()) {149// Classes inside a block, including anonymous classes,150// are checked when their surrounding member is checked.151definition.basicCheck(env);152}153//-FIX FOR BUGID 4056065154found = true;155return definition;156157case CS_CHECKED:158case CS_COMPILED:159found = true;160return definition;161162default:163throw new ClassNotFound(getName());164}165}166}167168/**169* Get the definition of this class, if the class is not170* yet defined, load the definition. Loading a class may171* throw various exceptions. Perform no basicCheck() on this172* class.173*/174public ClassDefinition getClassDefinitionNoCheck(Environment env) throws ClassNotFound {175if (tracing) env.dtEvent("getClassDefinition: " +176getName() + ", status " + getStatus());177for(;;) {178switch (status) {179case CS_UNDEFINED:180case CS_UNDECIDED:181case CS_SOURCE:182env.loadDefinition(this);183break;184185case CS_BINARY:186case CS_PARSED:187case CS_CHECKED:188case CS_COMPILED:189return definition;190191default:192throw new ClassNotFound(getName());193}194}195}196197/**198* Set the class definition199*/200public void setDefinition(ClassDefinition definition, int status) {201202// Sanity checks.203204// The name of the definition should match that of the declaration.205if ((definition != null) && !getName().equals(definition.getName())) {206throw new CompilerError("setDefinition: name mismatch: " +207this + ", " + definition);208}209210// The status states can be considered ordered in the same211// manner as their numerical values. We expect classes to212// progress through a sequence of monotonically increasing213// states. NOTE: There are currently exceptions to this rule214// which are believed to be legitimate. In particular, a215// class may be checked more than once, though we believe that216// this is unnecessary and may be avoided.217/*-----------------*218if (status <= this.status) {219System.out.println("STATUS REGRESSION: " +220this + " FROM " + this.status + " TO " + status);221}222*------------------*/223224this.definition = definition;225this.status = status;226}227228/**229* Equality230*/231public boolean equals(Object obj) {232if (obj instanceof ClassDeclaration) {233return type.equals(((ClassDeclaration)obj).type);234}235return false;236}237238@Override239public int hashCode() {240return type.hashCode();241}242243/**244* toString245*/246public String toString() {247String name = getName().toString();248String type = "type ";249String nested = getName().isInner() ? "nested " : "";250if (getClassDefinition() != null) {251if (getClassDefinition().isInterface()) {252type = "interface ";253} else {254type = "class ";255}256if (!getClassDefinition().isTopLevel()) {257nested = "inner ";258if (getClassDefinition().isLocal()) {259nested = "local ";260if (!getClassDefinition().isAnonymous()) {261name = getClassDefinition().getLocalName() +262" (" + name + ")";263}264}265}266}267return nested + type + name;268}269}270271272