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 Method_info extends Feature_info {
039: public static final int ACC_SYNCHRONIZED = 0x0020;
040: public static final int ACC_NATIVE = 0x0100;
041: public static final int ACC_ABSTRACT = 0x0400;
042: public static final int ACC_STRICT = 0x0800;
043:
044: public Method_info(Classfile classfile, DataInputStream in)
045: throws IOException {
046: super (classfile, in);
047: }
048:
049: public String getFeatureType() {
050: return "method";
051: }
052:
053: public boolean isSynchronized() {
054: return (getAccessFlag() & ACC_SYNCHRONIZED) != 0;
055: }
056:
057: public boolean isNative() {
058: return (getAccessFlag() & ACC_NATIVE) != 0;
059: }
060:
061: public boolean isAbstract() {
062: return (getAccessFlag() & ACC_ABSTRACT) != 0;
063: }
064:
065: public boolean isStrict() {
066: return (getAccessFlag() & ACC_STRICT) != 0;
067: }
068:
069: public boolean isConstructor() {
070: return getName().equals("<init>");
071: }
072:
073: public boolean isStaticInitializer() {
074: return getName().equals("<clinit>");
075: }
076:
077: public Collection getExceptions() {
078: Collection result = Collections.emptyList();
079:
080: for (Attribute_info attribute : getAttributes()) {
081: if (attribute instanceof Exceptions_attribute) {
082: result = ((Exceptions_attribute) attribute)
083: .getExceptions();
084: }
085: }
086:
087: return result;
088: }
089:
090: public String getSignature() {
091: StringBuffer result = new StringBuffer();
092:
093: if (isConstructor()) {
094: result.append(getClassfile().getSimpleName());
095: result
096: .append(SignatureHelper
097: .getSignature(getDescriptor()));
098: } else if (isStaticInitializer()) {
099: result.append("static {}");
100: } else {
101: result.append(getName());
102: result
103: .append(SignatureHelper
104: .getSignature(getDescriptor()));
105: }
106:
107: return result.toString();
108: }
109:
110: public String getReturnType() {
111: return SignatureHelper.getReturnType(getDescriptor());
112: }
113:
114: public String getDeclaration() {
115: StringBuffer result = new StringBuffer();
116:
117: if (isPublic())
118: result.append("public ");
119: if (isProtected())
120: result.append("protected ");
121: if (isPrivate())
122: result.append("private ");
123: if (isStatic())
124: result.append("static ");
125: if (isFinal())
126: result.append("final ");
127: if (isSynchronized())
128: result.append("synchronized ");
129: if (isNative())
130: result.append("native ");
131: if (isAbstract())
132: result.append("abstract ");
133:
134: if (!getName().equals("<init>")
135: && !getName().equals("<clinit>")) {
136: result.append(
137: (getReturnType() != null) ? getReturnType()
138: : "void").append(" ");
139: }
140:
141: result.append(getSignature());
142:
143: if (getExceptions().size() != 0) {
144: result.append(" throws ");
145: Iterator i = getExceptions().iterator();
146: while (i.hasNext()) {
147: result.append(i.next());
148: if (i.hasNext()) {
149: result.append(", ");
150: }
151: }
152: }
153:
154: return result.toString();
155: }
156:
157: public Code_attribute getCode() {
158: Code_attribute result = null;
159:
160: Iterator i = getAttributes().iterator();
161: while (result == null && i.hasNext()) {
162: Object temp = i.next();
163: if (temp instanceof Code_attribute) {
164: result = (Code_attribute) temp;
165: }
166: }
167:
168: return result;
169: }
170:
171: public void accept(Visitor visitor) {
172: visitor.visitMethod_info(this);
173: }
174: }
|