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