Repository navigation
Expand file tree
/
Copy pathJavaDecl.java
More file actions
285 lines (241 loc) · 7.45 KB
/
Copy pathJavaDecl.java
File metadata and controls
285 lines (241 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
* Copyright (c) 2010, ReportMill Software. All rights reserved.
*/
package javakit.resolver;
import snap.util.StringUtils;
import java.lang.reflect.Modifier;
/**
* A class to represent a declaration of a Java Class, Method, Field or Constructor.
*/
public class JavaDecl implements Comparable<JavaDecl> {
// The Resolver that produced this decl
protected Resolver _resolver;
// A unique identifier
protected String _id;
// The type
protected DeclType _type;
// The name of this declaration
protected String _name;
// The simple name of this declaration
protected String _simpleName;
// The full name of this declaration
private String _fullName;
// The type this decl evaluates to when referenced
protected JavaType _evalType;
// Constants for type
public enum DeclType { Class, Field, Constructor, Method, Package, Module, VarDecl, ParamType, TypeVar, GenArrayType, Word }
/**
* Constructor.
*/
protected JavaDecl(Resolver aResolver, DeclType aType)
{
// Set Resolver, Type
_resolver = aResolver;
_type = aType; assert (aType != null);
}
/**
* Returns the id.
*/
public String getId()
{
if (_id != null) return _id;
return _id = createId();
}
/**
* Creates the id.
*/
protected String createId() { return null; }
/**
* Returns the type.
*/
public DeclType getType() { return _type; }
/**
* Returns the name.
*/
public String getName() { return _name; }
/**
* Returns the simple name.
*/
public String getSimpleName() { return _simpleName; }
/**
* Returns the full name.
*/
public String getFullName()
{
// If already set, just return
if (_fullName != null) return _fullName;
// Get full name
String fullName = _name;
if (this instanceof JavaMethod || this instanceof JavaField) {
JavaMember member = (JavaMember) this;
String className = member.getDeclaringClassName();
fullName = className + '.' + fullName;
}
// Set and return
return _fullName = fullName;
}
/**
* Returns the JavaType this decl evaluates to when referenced.
*/
public JavaType getEvalType() { return _evalType; }
/**
* Returns the type of the most basic class associated with this type.
*/
public JavaClass getEvalClass()
{
JavaType evalType = getEvalType();
return evalType != null ? evalType.getEvalClass() : null;
}
/**
* Returns the type name for class this decl evaluates to when referenced.
*/
public String getEvalClassName()
{
JavaClass evalClass = getEvalClass();
return evalClass != null ? evalClass.getName() : null;
}
/**
* Returns the full name, with parameter type names appended if executable.
*/
public String getSimpleNameWithParameterTypes()
{
String simpleName = getSimpleName();
// If Executable, add parameter types string
if (this instanceof JavaExecutable)
simpleName += ((JavaExecutable) this).getParameterTypesString(true);
// Return
return simpleName;
}
/**
* Returns the full name, with parameter type names appended if executable.
*/
public String getFullNameWithParameterTypes()
{
String fullName = getFullName();
// If Executable, add parameter types string
if (this instanceof JavaExecutable)
fullName += ((JavaExecutable) this).getParameterTypesString(false);
// Return
return fullName;
}
/**
* Returns the full name, with parameter type simple-names appended if executable.
*/
public String getFullNameWithSimpleParameterTypes()
{
String fullName = getFullName();
// If Executable, add parameter types string
if (this instanceof JavaExecutable)
fullName += ((JavaExecutable) this).getParameterTypesString(true);
// Return
return fullName;
}
/**
* Returns the full declaration string, including modifiers and return type (method/field).
*/
public String getDeclarationString()
{
String declString = getSimpleName();
if (this instanceof JavaExecutable)
declString += ((JavaExecutable) this).getParametersString();
// If method or field, prefix return type
if (this instanceof JavaMethod || this instanceof JavaField) {
JavaType returnType = getEvalType();
String returnTypeName = returnType.getSimpleName();
declString = returnTypeName + " " + declString;
}
// If Class or Member, prefix mod string
if (this instanceof JavaClass || this instanceof JavaMember) {
int mods = this instanceof JavaClass ? ((JavaClass) this).getModifiers() : ((JavaMember) this).getModifiers();
String modifierStr = Modifier.toString(mods);
if (!modifierStr.isEmpty())
declString = modifierStr + " " + declString;
}
// Return
return declString;
}
/**
* Returns a string representation of suggestion.
*/
public String getSuggestionString()
{
return getSimpleNameWithParameterTypes();
}
/**
* Returns the string to use when inserting this suggestion into code.
*/
public String getReplaceString()
{
return getSimpleNameWithParameterTypes();
}
/**
* Returns a JavaDecl for given object.
*/
public JavaClass getJavaClassForClass(Class<?> aClass)
{
return _resolver.getJavaClassForClass(aClass);
}
/**
* Returns whether given declaration collides with this declaration.
*/
public boolean matches(JavaDecl aDecl)
{
return aDecl == this;
}
/**
* Standard compareTo implementation.
*/
public int compareTo(JavaDecl aDecl)
{
// Compare type order
int typeOrder1 = getType().ordinal();
int typeOrder2 = aDecl.getType().ordinal();
if (typeOrder1 != typeOrder2)
return typeOrder1 - typeOrder2;
// Compare full names
String fullName1 = getFullNameWithParameterTypes();
String fullName2 = aDecl.getFullNameWithParameterTypes();
return fullName1.compareTo(fullName2);
}
/**
* Standard equals implementation.
*/
@Override
public boolean equals(Object anObj)
{
if (anObj == this) return true;
JavaDecl other = anObj instanceof JavaDecl ? (JavaDecl) anObj : null;
if (other == null) return false;
String id = getId();
String otherId = other.getId();
return id.equals(otherId);
}
/**
* Standard hashcode implementation.
*/
public int hashCode()
{
String id = getId();
if (id == null)
return System.identityHashCode(this);
return id.hashCode();
}
/**
* Standard toString implementation.
*/
public String toString()
{
String className = getClass().getSimpleName();
String propStrings = toStringProps();
return className + " { " + propStrings + " }";
}
/**
* Standard toStringProps implementation.
*/
public String toStringProps()
{
StringBuffer sb = new StringBuffer();
StringUtils.appendProp(sb,"Id", getId());
return sb.toString();
}
}