1 | /* |
2 | * Copyright (c) 2001-2009, 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.impl; |
34 | |
35 | import java.io.*; |
36 | import java.util.*; |
37 | |
38 | import com.jeantessier.classreader.*; |
39 | |
40 | public class Method_info extends Feature_info implements com.jeantessier.classreader.Method_info { |
41 | private static final int ACC_SYNCHRONIZED = 0x0020; |
42 | private static final int ACC_BRIDGE = 0x0040; |
43 | private static final int ACC_VARARGS = 0x0080; |
44 | private static final int ACC_NATIVE = 0x0100; |
45 | private static final int ACC_ABSTRACT = 0x0400; |
46 | private static final int ACC_STRICT = 0x0800; |
47 | |
48 | public Method_info(Classfile classfile, DataInput in) throws IOException { |
49 | super(classfile, in); |
50 | } |
51 | |
52 | public String getFeatureType() { |
53 | return "method"; |
54 | } |
55 | |
56 | public boolean isSynchronized() { |
57 | return (getAccessFlag() & ACC_SYNCHRONIZED) != 0; |
58 | } |
59 | |
60 | public boolean isBridge() { |
61 | return (getAccessFlag() & ACC_BRIDGE) != 0; |
62 | } |
63 | |
64 | public boolean isVarargs() { |
65 | return (getAccessFlag() & ACC_VARARGS) != 0; |
66 | } |
67 | |
68 | public boolean isNative() { |
69 | return (getAccessFlag() & ACC_NATIVE) != 0; |
70 | } |
71 | |
72 | public boolean isAbstract() { |
73 | return (getAccessFlag() & ACC_ABSTRACT) != 0; |
74 | } |
75 | |
76 | public boolean isStrict() { |
77 | return (getAccessFlag() & ACC_STRICT) != 0; |
78 | } |
79 | |
80 | public boolean isConstructor() { |
81 | return getName().equals("<init>"); |
82 | } |
83 | |
84 | public boolean isStaticInitializer() { |
85 | return getName().equals("<clinit>"); |
86 | } |
87 | |
88 | public Collection<Class_info> getExceptions() { |
89 | Collection<Class_info> result = Collections.emptyList(); |
90 | |
91 | for (Attribute_info attribute : getAttributes()) { |
92 | if (attribute instanceof Exceptions_attribute) { |
93 | result = ((Exceptions_attribute) attribute).getExceptions(); |
94 | } |
95 | } |
96 | |
97 | return result; |
98 | } |
99 | |
100 | public String getSignature() { |
101 | StringBuffer result = new StringBuffer(); |
102 | |
103 | if (isConstructor()) { |
104 | result.append(getClassfile().getSimpleName()); |
105 | result.append(DescriptorHelper.getSignature(getDescriptor())); |
106 | } else if (isStaticInitializer()) { |
107 | result.append("static {}"); |
108 | } else { |
109 | result.append(getName()); |
110 | result.append(DescriptorHelper.getSignature(getDescriptor())); |
111 | } |
112 | |
113 | return result.toString(); |
114 | } |
115 | |
116 | public String getReturnType() { |
117 | return DescriptorHelper.getReturnType(getDescriptor()); |
118 | } |
119 | |
120 | public String getDeclaration() { |
121 | StringBuffer result = new StringBuffer(); |
122 | |
123 | if (isPublic()) result.append("public "); |
124 | if (isProtected()) result.append("protected "); |
125 | if (isPrivate()) result.append("private "); |
126 | if (isStatic() && !isStaticInitializer()) result.append("static "); |
127 | if (isFinal()) result.append("final "); |
128 | if (isSynchronized()) result.append("synchronized "); |
129 | if (isNative()) result.append("native "); |
130 | if (isAbstract()) result.append("abstract "); |
131 | |
132 | if (!isConstructor() && !isStaticInitializer()) { |
133 | result.append((getReturnType() != null) ? getReturnType() : "void").append(" "); |
134 | } |
135 | |
136 | result.append(getSignature()); |
137 | |
138 | if (getExceptions().size() != 0) { |
139 | result.append(" throws "); |
140 | Iterator i = getExceptions().iterator(); |
141 | while (i.hasNext()) { |
142 | result.append(i.next()); |
143 | if (i.hasNext()) { |
144 | result.append(", "); |
145 | } |
146 | } |
147 | } |
148 | |
149 | return result.toString(); |
150 | } |
151 | |
152 | public com.jeantessier.classreader.Code_attribute getCode() { |
153 | CodeFinder finder = new CodeFinder(); |
154 | accept(finder); |
155 | return finder.getCode(); |
156 | } |
157 | |
158 | public void accept(Visitor visitor) { |
159 | visitor.visitMethod_info(this); |
160 | } |
161 | } |