KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > Method_info


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.classreader;
34
35 import java.io.*;
36 import java.util.*;
37
38 public class Method_info extends Feature_info {
39     public static final int ACC_SYNCHRONIZED = 0x0020;
40     public static final int ACC_NATIVE = 0x0100;
41     public static final int ACC_ABSTRACT = 0x0400;
42     public static final int ACC_STRICT = 0x0800;
43
44     public Method_info(Classfile classfile, DataInputStream in) throws IOException {
45         super(classfile, in);
46     }
47
48     public String JavaDoc getFeatureType() {
49         return "method";
50     }
51
52     public boolean isSynchronized() {
53         return (getAccessFlag() & ACC_SYNCHRONIZED) != 0;
54     }
55
56     public boolean isNative() {
57         return (getAccessFlag() & ACC_NATIVE) != 0;
58     }
59
60     public boolean isAbstract() {
61         return (getAccessFlag() & ACC_ABSTRACT) != 0;
62     }
63
64     public boolean isStrict() {
65         return (getAccessFlag() & ACC_STRICT) != 0;
66     }
67
68     public boolean isConstructor() {
69         return getName().equals("<init>");
70     }
71
72     public boolean isStaticInitializer() {
73         return getName().equals("<clinit>");
74     }
75
76     public Collection getExceptions() {
77         Collection result = Collections.EMPTY_LIST;
78
79         Iterator i = getAttributes().iterator();
80         while (i.hasNext()) {
81             Object JavaDoc obj = i.next();
82             if (obj instanceof Exceptions_attribute) {
83                 result = ((Exceptions_attribute) obj).getExceptions();
84             }
85         }
86
87         return result;
88     }
89
90     public String JavaDoc getSignature() {
91         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
92
93         if (isConstructor()) {
94             result.append(getClassfile().getClassName().substring(getClassfile().getClassName().lastIndexOf(".") + 1));
95             result.append(SignatureHelper.getSignature(getDescriptor()));
96         } else if (isStaticInitializer()) {
97             result.append("static {}");
98         } else {
99             result.append(getName());
100             result.append(SignatureHelper.getSignature(getDescriptor()));
101         }
102
103         return result.toString();
104     }
105
106     public String JavaDoc getReturnType() {
107         return SignatureHelper.getReturnType(getDescriptor());
108     }
109
110     public String JavaDoc getDeclaration() {
111         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
112
113         if (isPublic()) result.append("public ");
114         if (isProtected()) result.append("protected ");
115         if (isPrivate()) result.append("private ");
116         if (isStatic()) result.append("static ");
117         if (isFinal()) result.append("final ");
118         if (isSynchronized()) result.append("synchronized ");
119         if (isNative()) result.append("native ");
120         if (isAbstract()) result.append("abstract ");
121
122         if (!getName().equals("<init>") && !getName().equals("<clinit>")) {
123             result.append((getReturnType() != null) ? getReturnType() : "void").append(" ");
124         }
125
126         result.append(getSignature());
127
128         if (getExceptions().size() != 0) {
129             result.append(" throws ");
130             Iterator i = getExceptions().iterator();
131             while (i.hasNext()) {
132                 result.append(i.next());
133                 if (i.hasNext()) {
134                     result.append(", ");
135                 }
136             }
137         }
138
139         return result.toString();
140     }
141
142     public void accept(Visitor visitor) {
143         visitor.visitMethod_info(this);
144     }
145 }
146
Popular Tags