Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/java/BinaryMember.java
38918 views
/*1* Copyright (c) 1994, 2003, 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;2627import sun.tools.tree.*;28import java.util.Vector;29import java.util.Hashtable;30import java.io.IOException;31import java.io.DataInputStream;32import java.io.ByteArrayInputStream;3334/**35* This class represents a binary member36*37* WARNING: The contents of this source file are not part of any38* supported API. Code that depends on them does so at its own risk:39* they are subject to change or removal without notice.40*/41public final42class BinaryMember extends MemberDefinition {43Expression value;44BinaryAttribute atts;4546/**47* Constructor48*/49public BinaryMember(ClassDefinition clazz, int modifiers, Type type,50Identifier name, BinaryAttribute atts) {51super(0, clazz, modifiers, type, name, null, null);52this.atts = atts;5354// Was it compiled as deprecated?55if (getAttribute(idDeprecated) != null) {56this.modifiers |= M_DEPRECATED;57}5859// Was it synthesized by the compiler?60if (getAttribute(idSynthetic) != null) {61this.modifiers |= M_SYNTHETIC;62}63}6465/**66* Constructor for an inner class.67*/68public BinaryMember(ClassDefinition innerClass) {69super(innerClass);70}7172/**73* Inline allowed (currently only allowed for the constructor of Object).74*/75public boolean isInlineable(Environment env, boolean fromFinal) {76// It is possible for 'getSuperClass()' to return null due to error77// recovery from cyclic inheritace. Can this cause a problem here?78return isConstructor() && (getClassDefinition().getSuperClass() == null);79}8081/**82* Get arguments83*/84public Vector getArguments() {85if (isConstructor() && (getClassDefinition().getSuperClass() == null)) {86Vector v = new Vector();87v.addElement(new LocalMember(0, getClassDefinition(), 0,88getClassDefinition().getType(), idThis));89return v;90}91return null;92}9394/**95* Get exceptions96*/97public ClassDeclaration[] getExceptions(Environment env) {98if ((!isMethod()) || (exp != null)) {99return exp;100}101byte data[] = getAttribute(idExceptions);102if (data == null) {103return new ClassDeclaration[0];104}105106try {107BinaryConstantPool cpool = ((BinaryClass)getClassDefinition()).getConstants();108DataInputStream in = new DataInputStream(new ByteArrayInputStream(data));109// JVM 4.7.5 Exceptions_attribute.number_of_exceptions110int n = in.readUnsignedShort();111exp = new ClassDeclaration[n];112for (int i = 0 ; i < n ; i++) {113// JVM 4.7.5 Exceptions_attribute.exception_index_table[]114exp[i] = cpool.getDeclaration(env, in.readUnsignedShort());115}116return exp;117} catch (IOException e) {118throw new CompilerError(e);119}120}121122/**123* Get documentation124*/125public String getDocumentation() {126if (documentation != null) {127return documentation;128}129byte data[] = getAttribute(idDocumentation);130if (data == null) {131return null;132}133try {134return documentation = new DataInputStream(new ByteArrayInputStream(data)).readUTF();135} catch (IOException e) {136throw new CompilerError(e);137}138}139140/**141* Check if constant: Will it inline away to a constant?142* This override is needed to solve bug 4128266. It is also143* integral to the solution of 4119776.144*/145private boolean isConstantCache = false;146private boolean isConstantCached = false;147public boolean isConstant() {148if (!isConstantCached) {149isConstantCache = isFinal()150&& isVariable()151&& getAttribute(idConstantValue) != null;152isConstantCached = true;153}154return isConstantCache;155}156157/**158* Get the value159*/160public Node getValue(Environment env) {161if (isMethod()) {162return null;163}164if (!isFinal()) {165return null;166}167if (getValue() != null) {168return (Expression)getValue();169}170byte data[] = getAttribute(idConstantValue);171if (data == null) {172return null;173}174175try {176BinaryConstantPool cpool = ((BinaryClass)getClassDefinition()).getConstants();177// JVM 4.7.3 ConstantValue.constantvalue_index178Object obj = cpool.getValue(new DataInputStream(new ByteArrayInputStream(data)).readUnsignedShort());179switch (getType().getTypeCode()) {180case TC_BOOLEAN:181setValue(new BooleanExpression(0, ((Number)obj).intValue() != 0));182break;183case TC_BYTE:184case TC_SHORT:185case TC_CHAR:186case TC_INT:187setValue(new IntExpression(0, ((Number)obj).intValue()));188break;189case TC_LONG:190setValue(new LongExpression(0, ((Number)obj).longValue()));191break;192case TC_FLOAT:193setValue(new FloatExpression(0, ((Number)obj).floatValue()));194break;195case TC_DOUBLE:196setValue(new DoubleExpression(0, ((Number)obj).doubleValue()));197break;198case TC_CLASS:199setValue(new StringExpression(0, (String)cpool.getValue(((Number)obj).intValue())));200break;201}202return (Expression)getValue();203} catch (IOException e) {204throw new CompilerError(e);205}206}207208/**209* Get a field attribute210*/211public byte[] getAttribute(Identifier name) {212for (BinaryAttribute att = atts ; att != null ; att = att.next) {213if (att.name.equals(name)) {214return att.data;215}216}217return null;218}219220public boolean deleteAttribute(Identifier name) {221BinaryAttribute walker = null, next = null;222223boolean succeed = false;224225while (atts.name.equals(name)) {226atts = atts.next;227succeed = true;228}229for (walker = atts; walker != null; walker = next) {230next = walker.next;231if (next != null) {232if (next.name.equals(name)) {233walker.next = next.next;234next = next.next;235succeed = true;236}237}238}239for (walker = atts; walker != null; walker = walker.next) {240if (walker.name.equals(name)) {241throw new InternalError("Found attribute " + name);242}243}244245return succeed;246}247248249250/*251* Add an attribute to a field252*/253public void addAttribute(Identifier name, byte data[], Environment env) {254this.atts = new BinaryAttribute(name, data, this.atts);255// Make sure that the new attribute is in the constant pool256((BinaryClass)(this.clazz)).cpool.indexString(name.toString(), env);257}258259}260261262