1 | // |
2 | // (c) Copyright, Moebius Solutions, 2002, 2003 |
3 | // |
4 | // All Rights Reserved |
5 | // |
6 | // This material may be reproduced by or for the U. S. Government |
7 | // pursuant to the copyright license under the clause at |
8 | // DFARS 252.227-7013 (OCT 1988). |
9 | // |
10 | package com.moesol.generator; |
11 | |
12 | import java.io.*; |
13 | import com.moesol.generator.core.*; |
14 | import com.moesol.generator.visitor.*; |
15 | |
16 | /** |
17 | * Encapsulate a Method or Constructor |
18 | */ |
19 | public abstract class Callable { |
20 | /** |
21 | * See Method getDeclaringClass |
22 | * @return |
23 | * */ |
24 | public abstract Class getDeclaringClass(); |
25 | /** |
26 | * See Method getReturnType |
27 | * @return |
28 | */ |
29 | public abstract Class getReturnType(); |
30 | /** |
31 | * See Method getParameterTypes |
32 | * @return |
33 | */ |
34 | public abstract Class[] getParameterTypes(); |
35 | /** |
36 | * See Method getExceptionTypes |
37 | * @return |
38 | */ |
39 | public abstract Class[] getExceptionTypes(); |
40 | /** |
41 | * See Method getName |
42 | * @return |
43 | */ |
44 | public abstract String getName(); |
45 | /** |
46 | * See Method getModifiers |
47 | * @return |
48 | */ |
49 | public abstract int getModifiers(); |
50 | /** |
51 | * Determine if the declaring class has other methods |
52 | * with the same name, but different parameters. |
53 | * @return |
54 | */ |
55 | public abstract boolean isOverloaded(); |
56 | /** |
57 | * Determine if the method is a Java implementation method |
58 | * (if the name contains $ then return true). |
59 | * @return |
60 | */ |
61 | public abstract boolean isImpl(); |
62 | |
63 | /** Tell getKeyString to include the package name */ |
64 | public static final int PACKAGE = (1 << 0); |
65 | /** Tell getKeyString to include the class name */ |
66 | public static final int CLASS = (1 << 1); |
67 | /** Tell getKeyString to include the method name */ |
68 | public static final int METHOD = (1 << 2); |
69 | /** Tell getKeyString to include the parameters */ |
70 | public static final int PARAMETERS = (1 << 3); |
71 | /** Tell getKeyString to include the wildcard */ |
72 | public static final int WILD = (1 << 4); |
73 | |
74 | /** |
75 | * Build a lookup string based on which bits are set in <code>parts</code>. |
76 | * |
77 | * @param parts parts to include in result. |
78 | * @return built string |
79 | * @throws ApplyException |
80 | */ |
81 | public String getKeyString(int parts) throws ApplyException { |
82 | StringBuffer result = new StringBuffer(); |
83 | if ((parts & PACKAGE) != 0) { |
84 | result.append(getDeclaringClass().getName()); |
85 | result.append("."); |
86 | } else if ((parts & CLASS) != 0) { |
87 | result.append(ClassGenerator.stripPackageName(getDeclaringClass().getName())); |
88 | result.append("."); |
89 | } |
90 | if ((parts & METHOD) != 0) { |
91 | result.append(getName()); |
92 | } |
93 | if ((parts & WILD) != 0) { |
94 | result.append('*'); |
95 | } |
96 | if ((parts & PARAMETERS) != 0) { |
97 | result.append("("); |
98 | result.append(getParamSignature()); |
99 | result.append(")"); |
100 | } |
101 | return result.toString(); |
102 | } |
103 | /** |
104 | * @return The parameters java signature. For example, |
105 | * "(char[],offset,len)". |
106 | * @throws ApplyException |
107 | */ |
108 | private String getParamSignature() throws ApplyException { |
109 | final StringWriter sw = new StringWriter(); |
110 | ParamTypeVisitor v = new JavaSignatureParamsVisitor(sw); |
111 | VisitClass.visitParamTypes(getParameterTypes(), v); |
112 | return sw.toString(); |
113 | } |
114 | } |