|
Lines 10-16
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jst.ws.internal.jaxws.ui.views; |
11 |
package org.eclipse.jst.ws.internal.jaxws.ui.views; |
| 12 |
|
12 |
|
| 13 |
import java.lang.reflect.Method; |
|
|
| 14 |
import java.util.ArrayList; |
13 |
import java.util.ArrayList; |
| 15 |
import java.util.Collections; |
14 |
import java.util.Collections; |
| 16 |
import java.util.Iterator; |
15 |
import java.util.Iterator; |
|
Lines 27-36
Link Here
|
| 27 |
import org.eclipse.jdt.core.IAnnotation; |
26 |
import org.eclipse.jdt.core.IAnnotation; |
| 28 |
import org.eclipse.jdt.core.ICompilationUnit; |
27 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 29 |
import org.eclipse.jdt.core.IJavaElement; |
28 |
import org.eclipse.jdt.core.IJavaElement; |
|
|
29 |
import org.eclipse.jdt.core.IJavaProject; |
| 30 |
import org.eclipse.jdt.core.ILocalVariable; |
30 |
import org.eclipse.jdt.core.ILocalVariable; |
| 31 |
import org.eclipse.jdt.core.IMemberValuePair; |
31 |
import org.eclipse.jdt.core.IMemberValuePair; |
| 32 |
import org.eclipse.jdt.core.IMethod; |
32 |
import org.eclipse.jdt.core.IMethod; |
|
|
33 |
import org.eclipse.jdt.core.IType; |
| 33 |
import org.eclipse.jdt.core.JavaModelException; |
34 |
import org.eclipse.jdt.core.JavaModelException; |
|
|
35 |
import org.eclipse.jdt.core.Signature; |
| 34 |
import org.eclipse.jdt.core.dom.AST; |
36 |
import org.eclipse.jdt.core.dom.AST; |
| 35 |
import org.eclipse.jdt.core.dom.ASTNode; |
37 |
import org.eclipse.jdt.core.dom.ASTNode; |
| 36 |
import org.eclipse.jdt.core.dom.Annotation; |
38 |
import org.eclipse.jdt.core.dom.Annotation; |
|
Lines 53-58
Link Here
|
| 53 |
import org.eclipse.jst.ws.annotations.core.AnnotationsManager; |
55 |
import org.eclipse.jst.ws.annotations.core.AnnotationsManager; |
| 54 |
import org.eclipse.jst.ws.annotations.core.initialization.IAnnotationAttributeInitializer; |
56 |
import org.eclipse.jst.ws.annotations.core.initialization.IAnnotationAttributeInitializer; |
| 55 |
import org.eclipse.jst.ws.annotations.core.utils.AnnotationUtils; |
57 |
import org.eclipse.jst.ws.annotations.core.utils.AnnotationUtils; |
|
|
58 |
import org.eclipse.jst.ws.internal.annotations.core.utils.SignatureUtils; |
| 56 |
import org.eclipse.jst.ws.internal.jaxws.ui.JAXWSUIMessages; |
59 |
import org.eclipse.jst.ws.internal.jaxws.ui.JAXWSUIMessages; |
| 57 |
import org.eclipse.jst.ws.internal.jaxws.ui.JAXWSUIPlugin; |
60 |
import org.eclipse.jst.ws.internal.jaxws.ui.JAXWSUIPlugin; |
| 58 |
import org.eclipse.ltk.core.refactoring.Change; |
61 |
import org.eclipse.ltk.core.refactoring.Change; |
|
Lines 63-68
Link Here
|
| 63 |
import org.eclipse.text.edits.MultiTextEdit; |
66 |
import org.eclipse.text.edits.MultiTextEdit; |
| 64 |
|
67 |
|
| 65 |
public class AnnotationsValuesEditingSupport extends EditingSupport { |
68 |
public class AnnotationsValuesEditingSupport extends EditingSupport { |
|
|
69 |
|
| 66 |
private AnnotationsView annotationsView; |
70 |
private AnnotationsView annotationsView; |
| 67 |
private TreeViewer treeViewer; |
71 |
private TreeViewer treeViewer; |
| 68 |
|
72 |
|
|
Lines 85-181
Link Here
|
| 85 |
|
89 |
|
| 86 |
@Override |
90 |
@Override |
| 87 |
protected boolean canEdit(Object element) { |
91 |
protected boolean canEdit(Object element) { |
| 88 |
if (element instanceof Method) { |
92 |
if (element instanceof IMethod) { |
| 89 |
Method method = (Method)element; |
93 |
IMethod method = (IMethod) element; |
| 90 |
return (Boolean) getValue(method.getDeclaringClass()); |
94 |
return (Boolean) getValue(method.getDeclaringType()); |
| 91 |
} |
95 |
} |
| 92 |
return true; |
96 |
return true; |
| 93 |
} |
97 |
} |
| 94 |
|
98 |
|
| 95 |
@Override |
99 |
@Override |
| 96 |
protected CellEditor getCellEditor(Object element) { |
100 |
protected CellEditor getCellEditor(Object element) { |
| 97 |
if (element instanceof Class) { |
101 |
if (element instanceof IType) { |
| 98 |
return checkboxCellEditor; |
102 |
return checkboxCellEditor; |
| 99 |
} |
103 |
} |
| 100 |
if (element instanceof Method) { |
104 |
if (element instanceof IMethod) { |
| 101 |
Method method = (Method) element; |
105 |
try { |
| 102 |
final Class<?> returnType = method.getReturnType(); |
106 |
IMethod method = (IMethod) element; |
| 103 |
if (returnType.isEnum()) { |
107 |
|
| 104 |
Object[] enumConstants = returnType.getEnumConstants(); |
108 |
IType enumType = SignatureUtils.getEnumReturnType(method); |
| 105 |
String[] values = new String[enumConstants.length]; |
109 |
if (enumType != null) { |
| 106 |
for (int i = 0; i < enumConstants.length; i++) { |
110 |
comboBoxCellEditor.setItems(SignatureUtils.getEnumConstantsNames(enumType)); |
| 107 |
values[i] = enumConstants[i].toString(); |
111 |
return comboBoxCellEditor; |
| 108 |
} |
112 |
} |
| 109 |
comboBoxCellEditor.setItems(values); |
113 |
|
| 110 |
return comboBoxCellEditor; |
114 |
final String returnType = method.getReturnType(); |
| 111 |
} |
115 |
|
| 112 |
if (returnType.equals(Boolean.TYPE)) { |
116 |
if (SignatureUtils.isBoolean(returnType)) { |
| 113 |
return checkboxCellEditor; |
117 |
return checkboxCellEditor; |
| 114 |
} |
118 |
} |
| 115 |
|
119 |
|
| 116 |
if (returnType.equals(Class.class)) { |
120 |
if (SignatureUtils.isClass(returnType)) { |
| 117 |
return classDialogCellEditor; |
121 |
return classDialogCellEditor; |
| 118 |
} |
122 |
} |
| 119 |
|
123 |
|
| 120 |
if (returnType.isArray()) { |
124 |
if (SignatureUtils.isArray(returnType)) { |
| 121 |
annotationArrayCellEditor.setMethod(method); |
125 |
annotationArrayCellEditor.setMethod(method); |
| 122 |
return annotationArrayCellEditor; |
126 |
return annotationArrayCellEditor; |
| 123 |
} |
127 |
} |
| 124 |
if (returnType.isPrimitive()) { |
128 |
if (SignatureUtils.isPrimitive(returnType)) { |
| 125 |
textCellEditor.setValidator(new ICellEditorValidator() { |
129 |
textCellEditor.setValidator(new ICellEditorValidator() { |
| 126 |
public String isValid(Object value) { |
130 |
public String isValid(Object value) { |
| 127 |
try { |
131 |
try { |
| 128 |
if (returnType.equals(Byte.TYPE)) { |
132 |
if (returnType.charAt(0) == Signature.C_BYTE) { |
| 129 |
Byte.parseByte((String) value); |
133 |
Byte.parseByte((String) value); |
| 130 |
} |
134 |
} |
| 131 |
if (returnType.equals(Short.TYPE)) { |
135 |
if (returnType.charAt(0) == Signature.C_SHORT) { |
| 132 |
Short.parseShort((String) value); |
136 |
Short.parseShort((String) value); |
| 133 |
} |
137 |
} |
| 134 |
if (returnType.equals(Integer.TYPE)) { |
138 |
if (returnType.charAt(0) == Signature.C_INT) { |
| 135 |
Integer.parseInt((String) value); |
139 |
Integer.parseInt((String) value); |
| 136 |
} |
140 |
} |
| 137 |
if (returnType.equals(Long.TYPE)) { |
141 |
if (returnType.charAt(0) == Signature.C_LONG) { |
| 138 |
Long.parseLong((String) value); |
142 |
Long.parseLong((String) value); |
| 139 |
} |
143 |
} |
| 140 |
if (returnType.equals(Float.TYPE)) { |
144 |
if (returnType.charAt(0) == Signature.C_FLOAT) { |
| 141 |
Float.parseFloat((String) value); |
145 |
Float.parseFloat((String) value); |
| 142 |
} |
146 |
} |
| 143 |
if (returnType.equals(Double.TYPE)) { |
147 |
if (returnType.charAt(0) == Signature.C_DOUBLE) { |
| 144 |
Double.parseDouble((String) value); |
148 |
Double.parseDouble((String) value); |
|
|
149 |
} |
| 150 |
} catch (NumberFormatException nfe) { |
| 151 |
return JAXWSUIMessages.ANNOTATION_EDITING_SUPPORT_NOT_VALID_MESSAGE_PREFIX + value; |
| 145 |
} |
152 |
} |
| 146 |
} catch (NumberFormatException nfe) { |
153 |
return null; |
| 147 |
return JAXWSUIMessages.ANNOTATION_EDITING_SUPPORT_NOT_VALID_MESSAGE_PREFIX |
|
|
| 148 |
+ returnType.getSimpleName(); |
| 149 |
} |
154 |
} |
| 150 |
return null; |
155 |
}); |
| 151 |
} |
156 |
return textCellEditor; |
| 152 |
}); |
157 |
} |
| 153 |
return textCellEditor; |
158 |
return textCellEditor; |
|
|
159 |
} catch (JavaModelException jme) { |
| 160 |
JAXWSUIPlugin.log(jme.getStatus()); |
| 154 |
} |
161 |
} |
| 155 |
return textCellEditor; |
|
|
| 156 |
} |
162 |
} |
| 157 |
return checkboxCellEditor; |
163 |
return checkboxCellEditor; |
| 158 |
} |
164 |
} |
| 159 |
|
165 |
|
| 160 |
@Override |
166 |
@Override |
| 161 |
protected Object getValue(Object element) { |
167 |
protected Object getValue(Object element) { |
| 162 |
if (element instanceof Class) { |
168 |
if (element instanceof IType) { |
| 163 |
return getValueForClass((Class<?>) element); |
169 |
return getValueForClass((IType) element); |
| 164 |
} |
170 |
} |
| 165 |
if (element instanceof Method) { |
171 |
if (element instanceof IMethod) { |
| 166 |
return getValueForMethod((Method) element); |
172 |
return getValueForMethod((IMethod) element); |
| 167 |
} |
173 |
} |
| 168 |
return null; |
174 |
return null; |
| 169 |
} |
175 |
} |
| 170 |
|
176 |
|
| 171 |
private Object getValueForClass(Class<?> aClass) { |
177 |
private Object getValueForClass(IType type) { |
| 172 |
if (treeViewer.getInput() instanceof IAnnotatable) { |
178 |
if (treeViewer.getInput() instanceof IAnnotatable) { |
| 173 |
return getValueForClass(aClass, (IAnnotatable) treeViewer.getInput()); |
179 |
return getValueForClass(type, (IAnnotatable) treeViewer.getInput()); |
| 174 |
} |
180 |
} |
| 175 |
return Boolean.FALSE; |
181 |
return Boolean.FALSE; |
| 176 |
} |
182 |
} |
| 177 |
|
183 |
|
| 178 |
private Object getValueForClass(Class<?> aClass, IAnnotatable annotatedElement) { |
184 |
private Object getValueForClass(IType type, IAnnotatable annotatedElement) { |
| 179 |
if (annotatedElement instanceof ILocalVariable) { |
185 |
if (annotatedElement instanceof ILocalVariable) { |
| 180 |
ILocalVariable localVariable = getLocalVariable(annotatedElement); |
186 |
ILocalVariable localVariable = getLocalVariable(annotatedElement); |
| 181 |
if (localVariable != null) { |
187 |
if (localVariable != null) { |
|
Lines 187-194
Link Here
|
| 187 |
for (IAnnotation annotation : annotations) { |
193 |
for (IAnnotation annotation : annotations) { |
| 188 |
String annotationName = annotation.getElementName(); |
194 |
String annotationName = annotation.getElementName(); |
| 189 |
if (AnnotationUtils.isAnnotationPresent((IJavaElement)annotatedElement, annotationName) |
195 |
if (AnnotationUtils.isAnnotationPresent((IJavaElement)annotatedElement, annotationName) |
| 190 |
&& (annotationName.equals(aClass.getSimpleName()) |
196 |
&& (annotationName.equals(type.getElementName()) |
| 191 |
|| annotationName.equals(aClass.getCanonicalName()))) { |
197 |
|| annotationName.equals(type.getFullyQualifiedName()))) { |
| 192 |
return Boolean.TRUE; |
198 |
return Boolean.TRUE; |
| 193 |
} |
199 |
} |
| 194 |
} |
200 |
} |
|
Lines 198-204
Link Here
|
| 198 |
return Boolean.FALSE; |
204 |
return Boolean.FALSE; |
| 199 |
} |
205 |
} |
| 200 |
|
206 |
|
| 201 |
private Object getValueForMethod(Method method) { |
207 |
private Object getValueForMethod(IMethod method) { |
| 202 |
Object value = null; |
208 |
Object value = null; |
| 203 |
try { |
209 |
try { |
| 204 |
if (treeViewer.getInput() instanceof IAnnotatable) { |
210 |
if (treeViewer.getInput() instanceof IAnnotatable) { |
|
Lines 210-259
Link Here
|
| 210 |
return value; |
216 |
return value; |
| 211 |
} |
217 |
} |
| 212 |
|
218 |
|
| 213 |
private Object getValueForMethod(Method method, IAnnotatable annotatedElement) throws JavaModelException { |
219 |
private Object getValueForMethod(IMethod method, IAnnotatable annotatedElement) throws JavaModelException { |
| 214 |
if (annotatedElement instanceof ILocalVariable) { |
220 |
if (annotatedElement instanceof ILocalVariable) { |
| 215 |
ILocalVariable localVariable = getLocalVariable(annotatedElement); |
221 |
ILocalVariable localVariable = getLocalVariable(annotatedElement); |
| 216 |
if (localVariable != null) { |
222 |
if (localVariable != null) { |
| 217 |
annotatedElement = localVariable; |
223 |
annotatedElement = localVariable; |
| 218 |
} |
224 |
} |
| 219 |
} |
225 |
} |
| 220 |
Class<?> returnType = method.getReturnType(); |
226 |
String returnType = method.getReturnType(); |
| 221 |
IAnnotation[] annotations = annotatedElement.getAnnotations(); |
227 |
IAnnotation[] annotations = annotatedElement.getAnnotations(); |
| 222 |
for (IAnnotation annotation : annotations) { |
228 |
for (IAnnotation annotation : annotations) { |
| 223 |
Class<?> declaringClass = method.getDeclaringClass(); |
229 |
IType declaringType = method.getDeclaringType(); |
|
|
230 |
|
| 224 |
String annotationName = annotation.getElementName(); |
231 |
String annotationName = annotation.getElementName(); |
| 225 |
if (annotationName.equals(declaringClass.getSimpleName()) |
232 |
if (annotationName.equals(declaringType.getElementName()) |
| 226 |
|| annotationName.equals(declaringClass.getCanonicalName())) { |
233 |
|| annotationName.equals(declaringType.getFullyQualifiedName())) { |
| 227 |
IMemberValuePair[] memberValuePairs = annotation.getMemberValuePairs(); |
234 |
IMemberValuePair[] memberValuePairs = annotation.getMemberValuePairs(); |
| 228 |
for (IMemberValuePair memberValuePair : memberValuePairs) { |
235 |
for (IMemberValuePair memberValuePair : memberValuePairs) { |
| 229 |
if (memberValuePair.getMemberName().equals(method.getName())) { |
236 |
if (memberValuePair.getMemberName().equals(method.getElementName())) { |
| 230 |
if (returnType.equals(String.class)) { |
237 |
if (SignatureUtils.isString(returnType)) { |
| 231 |
return memberValuePair.getValue(); |
238 |
return memberValuePair.getValue(); |
| 232 |
} |
239 |
} |
| 233 |
|
240 |
|
| 234 |
if (returnType.isEnum()) { |
241 |
IType enumType = SignatureUtils.getEnumReturnType(method); |
|
|
242 |
if (enumType != null) { |
| 235 |
String enumValue = memberValuePair.getValue().toString(); |
243 |
String enumValue = memberValuePair.getValue().toString(); |
| 236 |
String literal = enumValue.substring(enumValue.lastIndexOf(".") + 1); //$NON-NLS-1$ |
244 |
String literal = enumValue.substring(enumValue.lastIndexOf(".") + 1); //$NON-NLS-1$ |
| 237 |
Object[] enumConstants = method.getReturnType().getEnumConstants(); |
245 |
String[] enumConstants = SignatureUtils.getEnumConstantsNames(enumType); |
| 238 |
for (int i = 0; i < enumConstants.length; i++) { |
246 |
for (int i = 0; i < enumConstants.length; i++) { |
| 239 |
if (enumConstants[i].toString().equals(literal)) { |
247 |
if (enumConstants[i].equals(literal)) { |
| 240 |
return i; |
248 |
return i; |
| 241 |
} |
249 |
} |
| 242 |
} |
250 |
} |
| 243 |
} |
251 |
} |
| 244 |
|
252 |
if (SignatureUtils.isClass(returnType)) { |
| 245 |
if (returnType.equals(Class.class)) { |
|
|
| 246 |
return memberValuePair.getValue(); |
253 |
return memberValuePair.getValue(); |
| 247 |
} |
254 |
} |
| 248 |
|
255 |
|
| 249 |
if (returnType.equals(Boolean.TYPE)) { |
256 |
if (SignatureUtils.isBoolean(returnType)) { |
| 250 |
return memberValuePair.getValue(); |
257 |
return memberValuePair.getValue(); |
| 251 |
} |
258 |
} |
| 252 |
|
259 |
|
| 253 |
if (returnType.isPrimitive()) { |
260 |
if (SignatureUtils.isPrimitive(returnType)) { |
| 254 |
return ""; //$NON-NLS-1$ |
261 |
return ""; //$NON-NLS-1$ |
| 255 |
} |
262 |
} |
| 256 |
if (returnType.isArray()) { |
263 |
if (SignatureUtils.isArray(returnType)) { |
| 257 |
if (memberValuePair.getValueKind() == IMemberValuePair.K_CLASS) { |
264 |
if (memberValuePair.getValueKind() == IMemberValuePair.K_CLASS) { |
| 258 |
Object[] arrayValues = (Object[])memberValuePair.getValue(); |
265 |
Object[] arrayValues = (Object[])memberValuePair.getValue(); |
| 259 |
for (int i = 0; i < arrayValues.length; i++) { |
266 |
for (int i = 0; i < arrayValues.length; i++) { |
|
Lines 266-293
Link Here
|
| 266 |
} |
273 |
} |
| 267 |
} |
274 |
} |
| 268 |
} |
275 |
} |
| 269 |
return getDefaultValueForMethod(returnType); |
276 |
return getDefaultValueForMethod(method); |
| 270 |
} |
277 |
} |
| 271 |
} |
278 |
} |
| 272 |
return null; |
279 |
return null; |
| 273 |
} |
280 |
} |
| 274 |
|
281 |
|
| 275 |
private Object getDefaultValueForMethod(Class<?> returnType) { |
282 |
private Object getDefaultValueForMethod(IMethod method) throws JavaModelException { |
| 276 |
if (returnType.equals(String.class)) { |
283 |
String returnType = method.getReturnType(); |
|
|
284 |
|
| 285 |
if (SignatureUtils.isString(returnType)) { |
| 277 |
return ""; //$NON-NLS-1$ |
286 |
return ""; //$NON-NLS-1$ |
| 278 |
} |
287 |
} |
| 279 |
if (returnType.equals(Boolean.TYPE)) { |
288 |
|
|
|
289 |
if (SignatureUtils.isBoolean(returnType)) { |
| 280 |
return Boolean.FALSE; |
290 |
return Boolean.FALSE; |
| 281 |
} |
291 |
} |
| 282 |
if (returnType.isEnum()) { |
292 |
|
|
|
293 |
if (SignatureUtils.isEnum(method)) { |
| 283 |
return -1; |
294 |
return -1; |
| 284 |
} |
295 |
} |
| 285 |
if (returnType.isPrimitive()) { |
296 |
|
|
|
297 |
if (SignatureUtils.isPrimitive(returnType)) { |
| 286 |
return ""; //$NON-NLS-1$ |
298 |
return ""; //$NON-NLS-1$ |
| 287 |
} |
299 |
} |
| 288 |
if (returnType.isArray()) { |
300 |
|
|
|
301 |
if (SignatureUtils.isArray(returnType)) { |
| 289 |
return new Object[] {}; |
302 |
return new Object[] {}; |
| 290 |
} |
303 |
} |
|
|
304 |
|
| 291 |
return null; |
305 |
return null; |
| 292 |
} |
306 |
} |
| 293 |
|
307 |
|
|
Lines 310-350
Link Here
|
| 310 |
} |
324 |
} |
| 311 |
|
325 |
|
| 312 |
try { |
326 |
try { |
| 313 |
if (element instanceof Class && ((Class<?>) element).isAnnotation()) { |
327 |
if (element instanceof IType && ((IType) element).isAnnotation()) { |
| 314 |
@SuppressWarnings("unchecked") |
328 |
setValueForClass((IType) element, (Boolean) value); |
| 315 |
Class<? extends java.lang.annotation.Annotation> annotationClass = |
|
|
| 316 |
(Class<? extends java.lang.annotation.Annotation>) element; |
| 317 |
if (annotationClass != null) { |
| 318 |
setValueForClass(annotationClass, (Boolean) value); |
| 319 |
} |
| 320 |
} |
329 |
} |
| 321 |
if (element instanceof Method) { |
330 |
|
| 322 |
setValueForMethod((Method) element, value); |
331 |
if (element instanceof IMethod) { |
|
|
332 |
setValueForMethod((IMethod) element, value); |
| 323 |
} |
333 |
} |
| 324 |
} catch (CoreException ce) { |
334 |
} catch (CoreException ce) { |
| 325 |
JAXWSUIPlugin.log(ce.getStatus()); |
335 |
JAXWSUIPlugin.log(ce.getStatus()); |
| 326 |
} |
336 |
} |
| 327 |
} |
337 |
} |
| 328 |
|
338 |
|
| 329 |
private void setValueForClass(Class<? extends java.lang.annotation.Annotation> annotationClass, |
339 |
private void setValueForClass(IType type, Boolean annotate) throws CoreException { |
| 330 |
Boolean annotate) throws CoreException { |
|
|
| 331 |
Object viewerInput = treeViewer.getInput(); |
340 |
Object viewerInput = treeViewer.getInput(); |
| 332 |
|
341 |
|
| 333 |
IAnnotationAttributeInitializer annotationAttributeInitializer = |
342 |
IAnnotationAttributeInitializer annotationAttributeInitializer = |
| 334 |
AnnotationsManager.getAnnotationDefinitionForClass(annotationClass).getAnnotationAttributeInitializer(); |
343 |
AnnotationsManager.getAnnotationDefinitionForClass(type.getFullyQualifiedName()).getAnnotationAttributeInitializer(); |
| 335 |
|
344 |
|
| 336 |
if (viewerInput instanceof IJavaElement) { |
345 |
if (viewerInput instanceof IJavaElement) { |
| 337 |
setValueForClass(annotationClass, annotate, (IJavaElement) viewerInput, annotationAttributeInitializer); |
346 |
setValueForClass(type, annotate, (IJavaElement) viewerInput, annotationAttributeInitializer); |
| 338 |
} |
347 |
} |
| 339 |
} |
348 |
} |
| 340 |
|
349 |
|
| 341 |
private Annotation getAnnotation(AST ast, Class<? extends java.lang.annotation.Annotation> annotationClass, |
350 |
private Annotation getAnnotation(AST ast, IType type, List<MemberValuePair> memberValuePairs) throws JavaModelException { |
| 342 |
List<MemberValuePair> memberValuePairs) { |
|
|
| 343 |
|
| 344 |
Annotation annotation = null; |
351 |
Annotation annotation = null; |
| 345 |
int numberOfDeclaredMethods = annotationClass.getDeclaredMethods().length; |
352 |
int numberOfDeclaredMethods = type.getMethods().length; |
| 346 |
if (numberOfDeclaredMethods == 0) { |
353 |
if (numberOfDeclaredMethods == 0) { |
| 347 |
annotation = AnnotationsCore.createMarkerAnnotation(ast, annotationClass.getSimpleName()); |
354 |
annotation = AnnotationsCore.createMarkerAnnotation(ast, type.getElementName()); |
| 348 |
} else if (numberOfDeclaredMethods == 1) { |
355 |
} else if (numberOfDeclaredMethods == 1) { |
| 349 |
Expression value = null; |
356 |
Expression value = null; |
| 350 |
if (memberValuePairs != null && memberValuePairs.size() == 1) { |
357 |
if (memberValuePairs != null && memberValuePairs.size() == 1) { |
|
Lines 354-381
Link Here
|
| 354 |
} |
361 |
} |
| 355 |
} |
362 |
} |
| 356 |
if (value != null) { |
363 |
if (value != null) { |
| 357 |
annotation = AnnotationsCore.createSingleMemberAnnotation(ast, annotationClass.getSimpleName(), value); |
364 |
annotation = AnnotationsCore.createSingleMemberAnnotation(ast, type.getElementName(), value); |
| 358 |
} else { |
365 |
} else { |
| 359 |
annotation = AnnotationsCore.createNormalAnnotation(ast, annotationClass.getSimpleName(), memberValuePairs); |
366 |
annotation = AnnotationsCore.createNormalAnnotation(ast, type.getElementName(), memberValuePairs); |
| 360 |
} |
367 |
} |
| 361 |
} else if (numberOfDeclaredMethods > 1) { |
368 |
} else if (numberOfDeclaredMethods > 1) { |
| 362 |
annotation = AnnotationsCore.createNormalAnnotation(ast, annotationClass.getSimpleName(), memberValuePairs); |
369 |
annotation = AnnotationsCore.createNormalAnnotation(ast, type.getElementName(), memberValuePairs); |
| 363 |
} |
370 |
} |
| 364 |
|
371 |
|
| 365 |
return annotation; |
372 |
return annotation; |
| 366 |
} |
373 |
} |
| 367 |
|
374 |
|
| 368 |
private void setValueForClass(Class<? extends java.lang.annotation.Annotation> annotationClass, |
375 |
private void setValueForClass(IType type, Boolean annotate, IJavaElement javaElement, |
| 369 |
Boolean annotate, IJavaElement javaElement, IAnnotationAttributeInitializer annotationAttributeInitializer) |
376 |
IAnnotationAttributeInitializer annotationAttributeInitializer) throws CoreException { |
| 370 |
throws CoreException { |
|
|
| 371 |
ICompilationUnit source = AnnotationUtils.getCompilationUnitFromJavaElement(javaElement); |
377 |
ICompilationUnit source = AnnotationUtils.getCompilationUnitFromJavaElement(javaElement); |
| 372 |
CompilationUnit compilationUnit = SharedASTProvider.getAST(source, SharedASTProvider.WAIT_YES, null); |
378 |
CompilationUnit compilationUnit = SharedASTProvider.getAST(source, SharedASTProvider.WAIT_YES, null); |
| 373 |
AST ast = compilationUnit.getAST(); |
379 |
AST ast = compilationUnit.getAST(); |
| 374 |
|
380 |
|
| 375 |
List<MemberValuePair> memberValuePairs = getMemberValuePairs(annotationAttributeInitializer, javaElement, |
381 |
List<MemberValuePair> memberValuePairs = getMemberValuePairs(annotationAttributeInitializer, javaElement, |
| 376 |
ast, annotationClass); |
382 |
ast, type); |
| 377 |
|
383 |
|
| 378 |
Annotation annotation = getAnnotation(ast, annotationClass, memberValuePairs); |
384 |
Annotation annotation = getAnnotation(ast, type, memberValuePairs); |
| 379 |
|
385 |
|
| 380 |
TextFileChange change = new TextFileChange("Add/Remove Annotation", (IFile) source.getResource()); //$NON-NLS-1$ |
386 |
TextFileChange change = new TextFileChange("Add/Remove Annotation", (IFile) source.getResource()); //$NON-NLS-1$ |
| 381 |
MultiTextEdit multiTextEdit = new MultiTextEdit(); |
387 |
MultiTextEdit multiTextEdit = new MultiTextEdit(); |
|
Lines 387-393
Link Here
|
| 387 |
|| javaElement.getElementType() == IJavaElement.FIELD |
393 |
|| javaElement.getElementType() == IJavaElement.FIELD |
| 388 |
|| javaElement.getElementType() == IJavaElement.METHOD |
394 |
|| javaElement.getElementType() == IJavaElement.METHOD |
| 389 |
|| javaElement.getElementType() == IJavaElement.LOCAL_VARIABLE) { |
395 |
|| javaElement.getElementType() == IJavaElement.LOCAL_VARIABLE) { |
| 390 |
change.addEdit(AnnotationUtils.createAddImportTextEdit(javaElement, annotationClass.getCanonicalName())); |
396 |
change.addEdit(AnnotationUtils.createAddImportTextEdit(javaElement, type.getFullyQualifiedName())); |
| 391 |
change.addEdit(AnnotationUtils.createAddAnnotationTextEdit(javaElement, annotation)); |
397 |
change.addEdit(AnnotationUtils.createAddAnnotationTextEdit(javaElement, annotation)); |
| 392 |
} |
398 |
} |
| 393 |
} else { |
399 |
} else { |
|
Lines 396-420
Link Here
|
| 396 |
|| javaElement.getElementType() == IJavaElement.FIELD |
402 |
|| javaElement.getElementType() == IJavaElement.FIELD |
| 397 |
|| javaElement.getElementType() == IJavaElement.METHOD |
403 |
|| javaElement.getElementType() == IJavaElement.METHOD |
| 398 |
|| javaElement.getElementType() == IJavaElement.LOCAL_VARIABLE) { |
404 |
|| javaElement.getElementType() == IJavaElement.LOCAL_VARIABLE) { |
| 399 |
change.addEdit(AnnotationUtils.createRemoveImportTextEdit(javaElement, annotationClass.getCanonicalName())); |
405 |
change.addEdit(AnnotationUtils.createRemoveImportTextEdit(javaElement, type.getFullyQualifiedName())); |
| 400 |
change.addEdit(AnnotationUtils.createRemoveAnnotationTextEdit(javaElement, annotation)); |
406 |
change.addEdit(AnnotationUtils.createRemoveAnnotationTextEdit(javaElement, annotation)); |
| 401 |
} |
407 |
} |
| 402 |
} |
408 |
} |
| 403 |
executeChange(new NullProgressMonitor(), change); |
409 |
executeChange(new NullProgressMonitor(), change); |
| 404 |
} |
410 |
} |
| 405 |
|
411 |
|
| 406 |
private List<MemberValuePair> getMemberValuePairs( |
412 |
private List<MemberValuePair> getMemberValuePairs(IAnnotationAttributeInitializer annotationAttributeInitializer, |
| 407 |
IAnnotationAttributeInitializer annotationAttributeInitializer, IJavaElement javaElement, AST ast, |
413 |
IJavaElement javaElement, AST ast, IType type) { |
| 408 |
Class<?extends java.lang.annotation.Annotation> annotationClass) { |
|
|
| 409 |
if (annotationAttributeInitializer != null) { |
414 |
if (annotationAttributeInitializer != null) { |
| 410 |
return annotationAttributeInitializer.getMemberValuePairs(javaElement, ast, annotationClass); |
415 |
List<MemberValuePair> memberValuePairs = annotationAttributeInitializer.getMemberValuePairs(javaElement, ast, type); |
|
|
416 |
if (memberValuePairs.size() > 0) { |
| 417 |
return memberValuePairs; |
| 418 |
} else { |
| 419 |
return annotationAttributeInitializer.getMemberValuePairs(javaElement, ast, |
| 420 |
AnnotationsManager.getAnnotationDefinitionForType(type).getAnnotationClass()); |
| 421 |
} |
| 411 |
} |
422 |
} |
| 412 |
return Collections.emptyList(); |
423 |
return Collections.emptyList(); |
| 413 |
} |
424 |
} |
| 414 |
|
425 |
|
| 415 |
|
426 |
private void setValueForMethod(IMethod method, Object value) throws CoreException { |
| 416 |
private void setValueForMethod(Method method, Object value) throws CoreException { |
427 |
if (value instanceof String) { |
| 417 |
if (((Boolean) getValue(method.getDeclaringClass())).booleanValue()) { |
428 |
Object currentValue = getValue(method); |
|
|
429 |
if (currentValue != null && currentValue instanceof String) { |
| 430 |
if (((String) value).equals(currentValue)) { |
| 431 |
return; |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
if (((Boolean) getValue(method.getDeclaringType())).booleanValue()) { |
| 418 |
Object viewerInput = treeViewer.getInput(); |
436 |
Object viewerInput = treeViewer.getInput(); |
| 419 |
if (viewerInput instanceof IAnnotatable) { |
437 |
if (viewerInput instanceof IAnnotatable) { |
| 420 |
setValueForMethod(method, value, (IJavaElement) viewerInput); |
438 |
setValueForMethod(method, value, (IJavaElement) viewerInput); |
|
Lines 422-428
Link Here
|
| 422 |
} |
440 |
} |
| 423 |
} |
441 |
} |
| 424 |
|
442 |
|
| 425 |
private void setValueForMethod(Method method, Object value, IJavaElement javaElement) throws CoreException { |
443 |
private void setValueForMethod(IMethod method, Object value, IJavaElement javaElement) throws CoreException { |
| 426 |
ICompilationUnit source = AnnotationUtils.getCompilationUnitFromJavaElement(javaElement); |
444 |
ICompilationUnit source = AnnotationUtils.getCompilationUnitFromJavaElement(javaElement); |
| 427 |
CompilationUnit compilationUnit = SharedASTProvider.getAST(source, SharedASTProvider.WAIT_YES, null); |
445 |
CompilationUnit compilationUnit = SharedASTProvider.getAST(source, SharedASTProvider.WAIT_YES, null); |
| 428 |
AST ast = compilationUnit.getAST(); |
446 |
AST ast = compilationUnit.getAST(); |
|
Lines 435-448
Link Here
|
| 435 |
for (Annotation annotation : annotations) { |
453 |
for (Annotation annotation : annotations) { |
| 436 |
if (annotation instanceof NormalAnnotation) { |
454 |
if (annotation instanceof NormalAnnotation) { |
| 437 |
NormalAnnotation normalAnnotation = (NormalAnnotation) annotation; |
455 |
NormalAnnotation normalAnnotation = (NormalAnnotation) annotation; |
| 438 |
Class<?> declaringClass = method.getDeclaringClass(); |
456 |
IType declaringType = method.getDeclaringType(); |
| 439 |
String annotationName = normalAnnotation.getTypeName().getFullyQualifiedName(); |
457 |
String annotationName = normalAnnotation.getTypeName().getFullyQualifiedName(); |
| 440 |
if (annotationName.equals(declaringClass.getSimpleName()) || annotationName.equals(declaringClass.getCanonicalName())) { |
458 |
if (annotationName.equals(declaringType.getElementName()) || annotationName.equals(declaringType.getFullyQualifiedName())) { |
| 441 |
@SuppressWarnings("unchecked") |
459 |
@SuppressWarnings("unchecked") |
| 442 |
List<MemberValuePair> memberValuePairs = normalAnnotation.values(); |
460 |
List<MemberValuePair> memberValuePairs = normalAnnotation.values(); |
| 443 |
boolean hasMemberValuePair = false; |
461 |
boolean hasMemberValuePair = false; |
| 444 |
for (MemberValuePair memberValuePair : memberValuePairs) { |
462 |
for (MemberValuePair memberValuePair : memberValuePairs) { |
| 445 |
if (memberValuePair.getName().getIdentifier().equals(method.getName())) { |
463 |
if (memberValuePair.getName().getIdentifier().equals(method.getElementName())) { |
| 446 |
ASTNode memberValue = getMemberValuePairValue(ast, method, value); |
464 |
ASTNode memberValue = getMemberValuePairValue(ast, method, value); |
| 447 |
if (memberValue != null) { |
465 |
if (memberValue != null) { |
| 448 |
change.addEdit(AnnotationUtils.createUpdateMemberValuePairTextEdit(memberValuePair, memberValue)); |
466 |
change.addEdit(AnnotationUtils.createUpdateMemberValuePairTextEdit(memberValuePair, memberValue)); |
|
Lines 461-469
Link Here
|
| 461 |
} |
479 |
} |
| 462 |
} else if (annotation instanceof SingleMemberAnnotation) { |
480 |
} else if (annotation instanceof SingleMemberAnnotation) { |
| 463 |
SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation; |
481 |
SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation; |
| 464 |
Class<?> declaringClass = method.getDeclaringClass(); |
482 |
IType declaringType = method.getDeclaringType(); |
| 465 |
String annotationName = singleMemberAnnotation.getTypeName().getFullyQualifiedName(); |
483 |
String annotationName = singleMemberAnnotation.getTypeName().getFullyQualifiedName(); |
| 466 |
if (annotationName.equals(declaringClass.getSimpleName()) || annotationName.equals(declaringClass.getCanonicalName())) { |
484 |
if (annotationName.equals(declaringType.getElementName()) || annotationName.equals(declaringType.getFullyQualifiedName())) { |
| 467 |
MemberValuePair memberValuePair = getMemberValuePair(ast, method, value); |
485 |
MemberValuePair memberValuePair = getMemberValuePair(ast, method, value); |
| 468 |
if (memberValuePair != null) { |
486 |
if (memberValuePair != null) { |
| 469 |
change.addEdit(AnnotationUtils.createUpdateSingleMemberAnnotationTextEdit(singleMemberAnnotation, memberValuePair.getValue())); |
487 |
change.addEdit(AnnotationUtils.createUpdateSingleMemberAnnotationTextEdit(singleMemberAnnotation, memberValuePair.getValue())); |
|
Lines 477-552
Link Here
|
| 477 |
executeChange(new NullProgressMonitor(), change); |
495 |
executeChange(new NullProgressMonitor(), change); |
| 478 |
} |
496 |
} |
| 479 |
|
497 |
|
| 480 |
private ASTNode getMemberValuePairValue(AST ast, Method method, Object value) { |
498 |
private MemberValuePair getMemberValuePair(AST ast, IMethod method, Object value) throws JavaModelException { |
| 481 |
Class<?> returnType = method.getReturnType(); |
499 |
String returnType = method.getReturnType(); |
| 482 |
if (returnType.equals(String.class)) { |
500 |
if (SignatureUtils.isString(returnType)) { |
| 483 |
return AnnotationsCore.createStringLiteral(ast, value.toString()); |
501 |
return AnnotationsCore.createStringMemberValuePair(ast, method.getElementName(), (String) value); |
| 484 |
} |
502 |
} |
| 485 |
if (returnType.equals(Boolean.TYPE)) { |
503 |
if (SignatureUtils.isBoolean(returnType)) { |
| 486 |
return AnnotationsCore.createBooleanLiteral(ast, ((Boolean) value).booleanValue()); |
504 |
return AnnotationsCore.createBooleanMemberValuePair(ast, method.getElementName(), (Boolean) value); |
|
|
505 |
} |
| 506 |
|
| 507 |
if (SignatureUtils.isPrimitive(returnType)) { |
| 508 |
if (returnType.charAt(0) == Signature.C_BYTE |
| 509 |
|| returnType.charAt(0) == Signature.C_SHORT |
| 510 |
|| returnType.charAt(0) == Signature.C_INT |
| 511 |
|| returnType.charAt(0) == Signature.C_LONG |
| 512 |
|| returnType.charAt(0) == Signature.C_FLOAT |
| 513 |
|| returnType.charAt(0) == Signature.C_DOUBLE) { |
| 514 |
return AnnotationsCore.createNumberMemberValuePair(ast, method.getElementName(), value.toString()); |
| 515 |
} |
| 487 |
} |
516 |
} |
| 488 |
if (returnType.isPrimitive() |
517 |
|
| 489 |
&& (returnType.equals(Byte.TYPE) || returnType.equals(Short.TYPE) |
518 |
if (SignatureUtils.isArray(returnType)) { |
| 490 |
|| returnType.equals(Integer.TYPE) || returnType.equals(Long.TYPE) |
519 |
IType componentType = getComponentType(method); |
| 491 |
|| returnType.equals(Float.TYPE) || returnType.equals(Double.TYPE))) { |
520 |
if (componentType != null) { |
| 492 |
return AnnotationsCore.createNumberLiteral(ast, value.toString()); |
521 |
if (componentType.isAnnotation()) { |
| 493 |
} |
522 |
return createArrayMemberValuePair(ast, method, (Object[]) value); |
| 494 |
if (returnType.isArray()) { |
523 |
} else { |
| 495 |
if (method.getReturnType().getComponentType().isAnnotation()) { |
524 |
return AnnotationsCore.createArrayMemberValuePair(ast, method.getElementName(), (Object[]) value); |
| 496 |
return createArrayValueLiteral(ast, method, (Object[]) value); |
525 |
} |
| 497 |
} else { |
|
|
| 498 |
return AnnotationsCore.createArrayValueLiteral(ast, (Object[]) value); |
| 499 |
} |
526 |
} |
| 500 |
} |
527 |
} |
| 501 |
|
528 |
|
| 502 |
if (returnType.equals(Class.class)) { |
529 |
if (SignatureUtils.isClass(returnType)) { |
| 503 |
return AnnotationsCore.createTypeLiteral(ast, value.toString()); |
530 |
return AnnotationsCore.createTypeMemberValuePair(ast, method.getElementName(), value.toString()); |
| 504 |
} |
531 |
} |
| 505 |
|
532 |
|
| 506 |
if (returnType.isEnum()) { |
533 |
IType enumType = SignatureUtils.getEnumReturnType(method); |
|
|
534 |
if (enumType != null) { |
| 507 |
int selected = ((Integer) value).intValue(); |
535 |
int selected = ((Integer) value).intValue(); |
| 508 |
if (selected != -1) { |
536 |
if (selected != -1) { |
| 509 |
return AnnotationsCore.createEnumLiteral(ast, method.getDeclaringClass().getCanonicalName(), |
537 |
if (enumType.isMember()) { |
| 510 |
method.getReturnType().getEnumConstants()[selected]); |
538 |
return AnnotationsCore.createEnumMemberValuePair(ast, enumType.getDeclaringType().getFullyQualifiedName(), |
|
|
539 |
method.getElementName(), SignatureUtils.getEnumConstants(enumType)[selected]); |
| 540 |
} else { |
| 541 |
return AnnotationsCore.createEnumMemberValuePair(ast, enumType.getFullyQualifiedName(), |
| 542 |
method.getElementName(), SignatureUtils.getEnumConstants(enumType)[selected]); |
| 543 |
} |
| 511 |
} |
544 |
} |
| 512 |
} |
545 |
} |
| 513 |
return null; |
546 |
return null; |
| 514 |
} |
547 |
} |
| 515 |
|
548 |
|
| 516 |
private MemberValuePair getMemberValuePair(AST ast, Method method, Object value) { |
549 |
private ASTNode getMemberValuePairValue(AST ast, IMethod method, Object value) throws JavaModelException { |
| 517 |
Class<?> returnType = method.getReturnType(); |
550 |
String returnType = method.getReturnType(); |
| 518 |
if (returnType.equals(String.class)) { |
551 |
if (SignatureUtils.isString(returnType)) { |
| 519 |
return AnnotationsCore.createStringMemberValuePair(ast, method.getName(), (String) value); |
552 |
return AnnotationsCore.createStringLiteral(ast, value.toString()); |
| 520 |
} |
553 |
} |
| 521 |
if (returnType.equals(Boolean.TYPE)) { |
554 |
|
| 522 |
return AnnotationsCore.createBooleanMemberValuePair(ast, method.getName(), (Boolean) value); |
555 |
if (SignatureUtils.isBoolean(returnType)) { |
| 523 |
} |
556 |
return AnnotationsCore.createBooleanLiteral(ast, ((Boolean) value).booleanValue()); |
| 524 |
if (returnType.isPrimitive() |
557 |
} |
| 525 |
&& (returnType.equals(Byte.TYPE) || returnType.equals(Short.TYPE) |
558 |
|
| 526 |
|| returnType.equals(Integer.TYPE) || returnType.equals(Long.TYPE) |
559 |
if (SignatureUtils.isPrimitive(returnType)) { |
| 527 |
|| returnType.equals(Float.TYPE) || returnType.equals(Double.TYPE))) { |
560 |
if (returnType.charAt(0) == Signature.C_BYTE |
| 528 |
return AnnotationsCore.createNumberMemberValuePair(ast, method.getName(), value.toString()); |
561 |
|| returnType.charAt(0) == Signature.C_SHORT |
| 529 |
} |
562 |
|| returnType.charAt(0) == Signature.C_INT |
| 530 |
if (returnType.isArray()) { |
563 |
|| returnType.charAt(0) == Signature.C_LONG |
| 531 |
if (method.getReturnType().getComponentType().isAnnotation()) { |
564 |
|| returnType.charAt(0) == Signature.C_FLOAT |
| 532 |
return createArrayMemberValuePair(ast, method, (Object[]) value); |
565 |
|| returnType.charAt(0) == Signature.C_DOUBLE) { |
| 533 |
} else { |
566 |
return AnnotationsCore.createNumberLiteral(ast, value.toString()); |
| 534 |
return AnnotationsCore.createArrayMemberValuePair(ast, method.getName(), (Object[]) value); |
567 |
} |
|
|
568 |
} |
| 569 |
|
| 570 |
if (SignatureUtils.isArray(returnType)) { |
| 571 |
IType componentType = getComponentType(method); |
| 572 |
if (componentType != null) { |
| 573 |
if (componentType.isAnnotation()) { |
| 574 |
return createArrayValueLiteral(ast, method, (Object[]) value); |
| 575 |
} else { |
| 576 |
return AnnotationsCore.createArrayValueLiteral(ast, (Object[]) value); |
| 577 |
} |
| 535 |
} |
578 |
} |
| 536 |
} |
579 |
} |
| 537 |
|
580 |
|
| 538 |
if (returnType.equals(Class.class)) { |
581 |
if (SignatureUtils.isClass(returnType)) { |
| 539 |
return AnnotationsCore.createTypeMemberValuePair(ast, method.getName(), value.toString()); |
582 |
return AnnotationsCore.createTypeLiteral(ast, value.toString()); |
| 540 |
} |
583 |
} |
| 541 |
|
584 |
|
| 542 |
if (returnType.isEnum()) { |
585 |
|
|
|
586 |
IType enumType = SignatureUtils.getEnumReturnType(method); |
| 587 |
if (enumType != null) { |
| 543 |
int selected = ((Integer) value).intValue(); |
588 |
int selected = ((Integer) value).intValue(); |
| 544 |
if (selected != -1) { |
589 |
if (selected != -1) { |
| 545 |
return AnnotationsCore.createEnumMemberValuePair(ast, |
590 |
if (enumType.isMember()) { |
| 546 |
method.getDeclaringClass().getCanonicalName(), method.getName(), method.getReturnType() |
591 |
return AnnotationsCore.createEnumLiteral(ast, enumType.getDeclaringType().getFullyQualifiedName(), |
| 547 |
.getEnumConstants()[selected]); |
592 |
SignatureUtils.getEnumConstants(enumType)[selected]); |
|
|
593 |
} else { |
| 594 |
return AnnotationsCore.createEnumLiteral(ast, enumType.getFullyQualifiedName(), |
| 595 |
SignatureUtils.getEnumConstants(enumType)[selected]); |
| 596 |
} |
| 548 |
} |
597 |
} |
| 549 |
} |
598 |
} |
|
|
599 |
|
| 550 |
return null; |
600 |
return null; |
| 551 |
} |
601 |
} |
| 552 |
|
602 |
|
|
Lines 578-642
Link Here
|
| 578 |
annotationsView.refreshLabels(); |
628 |
annotationsView.refreshLabels(); |
| 579 |
} |
629 |
} |
| 580 |
|
630 |
|
| 581 |
private MemberValuePair createArrayMemberValuePair(AST ast, Method method, Object[] values) { |
631 |
private MemberValuePair createArrayMemberValuePair(AST ast, IMethod method, Object[] values) throws JavaModelException { |
| 582 |
return AnnotationsCore.createMemberValuePair(ast, method.getName(), createArrayValueLiteral(ast, |
632 |
return AnnotationsCore.createMemberValuePair(ast, method.getElementName(), createArrayValueLiteral(ast, |
| 583 |
method, values)); |
633 |
method, values)); |
| 584 |
} |
634 |
} |
| 585 |
|
635 |
|
| 586 |
@SuppressWarnings("unchecked") |
636 |
private IType getComponentType(IMethod method) throws JavaModelException { |
| 587 |
private ArrayInitializer createArrayValueLiteral(AST ast, Method method, Object[] values) { |
637 |
String returnType = method.getReturnType(); |
|
|
638 |
if (SignatureUtils.isArray(returnType)) { |
| 639 |
String elementType = Signature.getElementType(returnType); |
| 640 |
IType declaringType = method.getDeclaringType(); |
| 641 |
IJavaProject javaProject = declaringType.getJavaProject(); |
| 642 |
if (javaProject != null) { |
| 643 |
return javaProject.findType(Signature.toString(elementType)); |
| 644 |
} |
| 645 |
} |
| 646 |
return null; |
| 647 |
} |
| 648 |
|
| 649 |
private ArrayInitializer createArrayValueLiteral(AST ast, IMethod method, Object[] values) throws JavaModelException { |
| 588 |
ArrayInitializer arrayInitializer = ast.newArrayInitializer(); |
650 |
ArrayInitializer arrayInitializer = ast.newArrayInitializer(); |
| 589 |
for (Object value : values) { |
651 |
for (Object value : values) { |
| 590 |
if (value instanceof List) { |
652 |
if (value instanceof List<?>) { |
| 591 |
Class<? extends java.lang.annotation.Annotation> annotationClass = |
653 |
IType componentType = getComponentType(method); |
| 592 |
(Class<? extends java.lang.annotation.Annotation>) method.getReturnType().getComponentType(); |
654 |
if (componentType != null) { |
| 593 |
|
655 |
|
| 594 |
List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>(); |
656 |
List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>(); |
| 595 |
|
657 |
@SuppressWarnings("unchecked") |
| 596 |
List<Map<String, Object>> valuesList = (List<Map<String, Object>>) value; |
658 |
List<Map<String, Object>> valuesList = (List<Map<String, Object>>) value; |
| 597 |
Iterator<Map<String, Object>> valuesIterator = valuesList.iterator(); |
659 |
Iterator<Map<String, Object>> valuesIterator = valuesList.iterator(); |
| 598 |
while (valuesIterator.hasNext()) { |
660 |
while (valuesIterator.hasNext()) { |
| 599 |
Map<String, Object> annotationMap = valuesIterator.next(); |
661 |
Map<String, Object> annotationMap = valuesIterator.next(); |
| 600 |
Set<Entry<String, Object>> entrySet = annotationMap.entrySet(); |
662 |
Set<Entry<String, Object>> entrySet = annotationMap.entrySet(); |
| 601 |
Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator(); |
663 |
Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator(); |
| 602 |
while (iterator.hasNext()) { |
664 |
while (iterator.hasNext()) { |
| 603 |
Map.Entry<java.lang.String, Object> entry = iterator.next(); |
665 |
Map.Entry<java.lang.String, Object> entry = iterator.next(); |
| 604 |
String memberName = entry.getKey(); |
666 |
String memberName = entry.getKey(); |
| 605 |
try { |
667 |
try { |
| 606 |
Method annotationMethod = annotationClass.getMethod(memberName, new Class[0]); |
668 |
IMethod annotationMethod = componentType.getMethod(memberName, new String[] {}); |
| 607 |
if (annotationMethod != null) { |
669 |
if (annotationMethod != null) { |
| 608 |
Object memberValue = entry.getValue(); |
670 |
Object memberValue = entry.getValue(); |
| 609 |
Class<?> returnType = annotationMethod.getReturnType(); |
671 |
String returnType = annotationMethod.getReturnType(); |
| 610 |
if (returnType.equals(String.class)) { |
672 |
if (SignatureUtils.isString(returnType)) { |
| 611 |
memberValuePairs.add(AnnotationsCore.createStringMemberValuePair(ast, memberName, |
673 |
memberValuePairs.add(AnnotationsCore.createStringMemberValuePair(ast, memberName, |
| 612 |
memberValue.toString())); |
674 |
memberValue.toString())); |
| 613 |
} |
|
|
| 614 |
if (returnType.equals(Boolean.TYPE)) { |
| 615 |
memberValuePairs.add(AnnotationsCore.createBooleanMemberValuePair(ast, memberName, |
| 616 |
(Boolean) memberValue)); |
| 617 |
} |
| 618 |
if (returnType.equals(Class.class)) { |
| 619 |
String className = memberValue.toString(); |
| 620 |
if (className.endsWith(".class")) { |
| 621 |
className = className.substring(0, className.lastIndexOf(".")); |
| 622 |
} |
675 |
} |
| 623 |
memberValuePairs.add(AnnotationsCore.createMemberValuePair(ast, memberName, |
676 |
if (SignatureUtils.isBoolean(returnType)) { |
| 624 |
AnnotationsCore.createTypeLiteral(ast, className))); |
677 |
memberValuePairs.add(AnnotationsCore.createBooleanMemberValuePair(ast, memberName, |
|
|
678 |
(Boolean) memberValue)); |
| 679 |
} |
| 680 |
if (SignatureUtils.isClass(returnType)) { |
| 681 |
String className = memberValue.toString(); |
| 682 |
if (className.endsWith(".class")) { |
| 683 |
className = className.substring(0, className.lastIndexOf(".")); |
| 684 |
} |
| 685 |
memberValuePairs.add(AnnotationsCore.createMemberValuePair(ast, memberName, |
| 686 |
AnnotationsCore.createTypeLiteral(ast, className))); |
| 687 |
} |
| 688 |
// if (returnType.isPrimitive()) { |
| 689 |
// memberValuePairs.add(getNumberMemberValuePair(ast, memberName, memberValue)); |
| 690 |
// } |
| 625 |
} |
691 |
} |
| 626 |
// if (returnType.isPrimitive()) { |
|
|
| 627 |
// memberValuePairs.add(getNumberMemberValuePair(ast, memberName, memberValue)); |
| 628 |
// } |
| 629 |
} |
| 630 |
|
692 |
|
| 631 |
} catch (SecurityException se) { |
693 |
} catch (SecurityException se) { |
| 632 |
AnnotationsCorePlugin.log(se); |
694 |
AnnotationsCorePlugin.log(se); |
| 633 |
} catch (NoSuchMethodException nsme) { |
695 |
} |
| 634 |
AnnotationsCorePlugin.log(nsme); |
|
|
| 635 |
} |
696 |
} |
| 636 |
} |
697 |
} |
|
|
698 |
arrayInitializer.expressions().add(AnnotationsCore.createNormalAnnotation(ast, componentType.getFullyQualifiedName(), |
| 699 |
memberValuePairs)); |
| 637 |
} |
700 |
} |
| 638 |
arrayInitializer.expressions().add(AnnotationsCore.createNormalAnnotation(ast, annotationClass.getCanonicalName(), |
|
|
| 639 |
memberValuePairs)); |
| 640 |
} |
701 |
} |
| 641 |
} |
702 |
} |
| 642 |
return arrayInitializer; |
703 |
return arrayInitializer; |