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
|
/*
* Copyright (C) 2009-2011 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.eclipse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
import org.eclipse.jdt.internal.compiler.ast.Clinit;
import org.eclipse.jdt.internal.compiler.ast.Expression;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Literal;
import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
public class Eclipse {
private static final Annotation[] EMPTY_ANNOTATIONS_ARRAY = new Annotation[0];
/**
* Eclipse's Parser class is instrumented to not attempt to fill in the body of any method or initializer
* or field initialization if this flag is set. Set it on the flag field of
* any method, field, or initializer you create!
*/
public static final int ECLIPSE_DO_NOT_TOUCH_FLAG = ASTNode.Bit24;
private Eclipse() {
//Prevent instantiation
}
/**
* For 'speed' reasons, Eclipse works a lot with char arrays. I have my doubts this was a fruitful exercise,
* but we need to deal with it. This turns [[java][lang][String]] into "java.lang.String".
*/
public static String toQualifiedName(char[][] typeName) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (char[] c : typeName) {
sb.append(first ? "" : ".").append(c);
first = false;
}
return sb.toString();
}
public static char[][] fromQualifiedName(String typeName) {
String[] split = typeName.split("\\.");
char[][] result = new char[split.length][];
for (int i = 0; i < split.length; i++) {
result[i] = split[i].toCharArray();
}
return result;
}
public static long pos(ASTNode node) {
return ((long) node.sourceStart << 32) | (node.sourceEnd & 0xFFFFFFFFL);
}
public static long[] poss(ASTNode node, int repeat) {
long p = ((long) node.sourceStart << 32) | (node.sourceEnd & 0xFFFFFFFFL);
long[] out = new long[repeat];
Arrays.fill(out, p);
return out;
}
/**
* Checks if an eclipse-style array-of-array-of-characters to represent a fully qualified name ('foo.bar.baz'), matches a plain
* string containing the same fully qualified name with dots in the string.
*/
public static boolean nameEquals(char[][] typeName, String string) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (char[] elem : typeName) {
if (first) first = false;
else sb.append('.');
sb.append(elem);
}
return string.contentEquals(sb);
}
public static boolean hasClinit(TypeDeclaration parent) {
if (parent.methods == null) return false;
for (AbstractMethodDeclaration method : parent.methods) {
if (method instanceof Clinit) return true;
}
return false;
}
/**
* Searches the given field node for annotations and returns each one that matches the provided regular expression pattern.
*
* Only the simple name is checked - the package and any containing class are ignored.
*/
public static Annotation[] findAnnotations(FieldDeclaration field, Pattern namePattern) {
List<Annotation> result = new ArrayList<Annotation>();
if (field.annotations == null) return EMPTY_ANNOTATIONS_ARRAY;
for (Annotation annotation : field.annotations) {
TypeReference typeRef = annotation.type;
if (typeRef != null && typeRef.getTypeName() != null) {
char[][] typeName = typeRef.getTypeName();
String suspect = new String(typeName[typeName.length - 1]);
if (namePattern.matcher(suspect).matches()) {
result.add(annotation);
}
}
}
return result.toArray(EMPTY_ANNOTATIONS_ARRAY);
}
/** Matches any of the 8 primitive names, such as {@code boolean}. */
private static final Pattern PRIMITIVE_TYPE_NAME_PATTERN = Pattern.compile(
"^(boolean|byte|short|int|long|float|double|char)$");
/**
* Checks if the given type reference represents a primitive type.
*/
public static boolean isPrimitive(TypeReference ref) {
if (ref.dimensions() > 0) return false;
return PRIMITIVE_TYPE_NAME_PATTERN.matcher(toQualifiedName(ref.getTypeName())).matches();
}
/**
* Returns the actual value of the given Literal or Literal-like node.
*/
public static Object calculateValue(Expression e) {
if (e instanceof Literal) {
((Literal)e).computeConstant();
switch (e.constant.typeID()) {
case TypeIds.T_int: return e.constant.intValue();
case TypeIds.T_byte: return e.constant.byteValue();
case TypeIds.T_short: return e.constant.shortValue();
case TypeIds.T_char: return e.constant.charValue();
case TypeIds.T_float: return e.constant.floatValue();
case TypeIds.T_double: return e.constant.doubleValue();
case TypeIds.T_boolean: return e.constant.booleanValue();
case TypeIds.T_long: return e.constant.longValue();
case TypeIds.T_JavaLangString: return e.constant.stringValue();
default: return null;
}
} else if (e instanceof ClassLiteralAccess) {
return Eclipse.toQualifiedName(((ClassLiteralAccess)e).type.getTypeName());
} else if (e instanceof SingleNameReference) {
return new String(((SingleNameReference)e).token);
} else if (e instanceof QualifiedNameReference) {
String qName = Eclipse.toQualifiedName(((QualifiedNameReference)e).tokens);
int idx = qName.lastIndexOf('.');
return idx == -1 ? qName : qName.substring(idx+1);
}
return null;
}
}
|