001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: public class Field_info extends Feature_info {
039: public static final int ACC_VOLATILE = 0x0040;
040: public static final int ACC_TRANSIENT = 0x0080;
041:
042: public Field_info(Classfile classfile, DataInputStream in)
043: throws IOException {
044: super (classfile, in);
045: }
046:
047: public String getFeatureType() {
048: return "field";
049: }
050:
051: public boolean isVolatile() {
052: return (getAccessFlag() & ACC_VOLATILE) != 0;
053: }
054:
055: public boolean isTransient() {
056: return (getAccessFlag() & ACC_TRANSIENT) != 0;
057: }
058:
059: public String getType() {
060: return SignatureHelper.getType(getDescriptor());
061: }
062:
063: public String getDeclaration() {
064: StringBuffer result = new StringBuffer();
065:
066: if (isPublic())
067: result.append("public ");
068: if (isProtected())
069: result.append("protected ");
070: if (isPrivate())
071: result.append("private ");
072: if (isStatic())
073: result.append("static ");
074: if (isFinal())
075: result.append("final ");
076: if (isVolatile())
077: result.append("volatile ");
078: if (isTransient())
079: result.append("transient ");
080:
081: result.append(getType()).append(" ");
082: result.append(getName());
083:
084: return result.toString();
085: }
086:
087: public String getFullDeclaration() {
088: String result = getDeclaration();
089:
090: if (getConstantValue() != null) {
091: if (getConstantValue().getRawValue() instanceof String_info) {
092: result += " = \"" + getConstantValue().getRawValue()
093: + "\"";
094: } else {
095: result += " = " + getConstantValue().getRawValue();
096: }
097: }
098:
099: return result;
100: }
101:
102: public String getSignature() {
103: return getName();
104: }
105:
106: public ConstantValue_attribute getConstantValue() {
107: ConstantValue_attribute result = null;
108:
109: Iterator i = getAttributes().iterator();
110: while (result == null && i.hasNext()) {
111: Object temp = i.next();
112: if (temp instanceof ConstantValue_attribute) {
113: result = (ConstantValue_attribute) temp;
114: }
115: }
116:
117: return result;
118: }
119:
120: public void accept(Visitor visitor) {
121: visitor.visitField_info(this);
122: }
123: }
|