Author: scabanovich
Date: 2011-08-17 19:40:05 -0400 (Wed, 17 Aug 2011)
New Revision: 34023
Modified:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/java/impl/AnnotationDeclaration.java
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseJavaUtil.java
Log:
JBIDE-6517
https://issues.jboss.org/browse/JBIDE-6517
Modified:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/java/impl/AnnotationDeclaration.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/java/impl/AnnotationDeclaration.java 2011-08-17
23:19:12 UTC (rev 34022)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/java/impl/AnnotationDeclaration.java 2011-08-17
23:40:05 UTC (rev 34023)
@@ -11,14 +11,21 @@
package org.jboss.tools.common.java.impl;
import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IAnnotation;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IField;
+import org.eclipse.jdt.core.IImportDeclaration;
+import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMemberValuePair;
import org.eclipse.jdt.core.IType;
+import org.jboss.tools.common.CommonPlugin;
import org.jboss.tools.common.java.IAnnotationDeclaration;
import org.jboss.tools.common.java.IAnnotationType;
import org.jboss.tools.common.java.IJavaAnnotation;
import org.jboss.tools.common.java.impl.JavaAnnotation;
+import org.jboss.tools.common.util.EclipseJavaUtil;
/**
*
@@ -45,14 +52,14 @@
public IMemberValuePair[] getMemberValuePairs() {
return annotation.getMemberValuePairs();
}
-
+
public Object getMemberValue(String name) {
- if(name == null) name = "value";
+ if(name == null) name = "value"; //$NON-NLS-1$
IMemberValuePair[] pairs = getMemberValuePairs();
if(pairs != null) {
for (IMemberValuePair pair: pairs) {
if(name.equals(pair.getMemberName())) {
- return pair.getValue();
+ return resolveMemberValue(pair);
}
}
}
@@ -89,4 +96,70 @@
}
return null;
}
+
+ public Object resolveMemberValue(IMemberValuePair pair) {
+ Object value = pair.getValue();
+ int k = pair.getValueKind();
+ if(k == IMemberValuePair.K_QUALIFIED_NAME || k == IMemberValuePair.K_SIMPLE_NAME
+ || (value instanceof Object[] && k == IMemberValuePair.K_UNKNOWN)) {
+ IAnnotation a = getJavaAnnotation();
+ if(a != null && a.getAncestor(IJavaElement.COMPILATION_UNIT) instanceof
ICompilationUnit) {
+ value = validateNamedValue(value, a);
+ }
+ }
+ return value;
+ }
+
+ private Object validateNamedValue(Object value, IAnnotation a) {
+ if(value instanceof Object[]) {
+ Object[] vs = (Object[])value;
+ for (int i = 0; i < vs.length; i++) {
+ vs[i] = validateNamedValue(vs[i], a);
+ }
+ } else {
+ ICompilationUnit u = (ICompilationUnit)a.getAncestor(IJavaElement.COMPILATION_UNIT);
+ IType type = (IType)a.getAncestor(IJavaElement.TYPE);
+ try {
+ IImportDeclaration[] is = u.getImports();
+ String stringValue = value.toString();
+ int lastDot = stringValue.lastIndexOf('.');
+ String lastToken = stringValue.substring(lastDot + 1);
+ if(lastDot < 0) {
+ IField f = (a.getParent() == type) ? type.getField(lastToken) :
EclipseJavaUtil.findField(type, lastToken);
+ if(f != null && f.exists()) {
+ value = f.getDeclaringType().getFullyQualifiedName() + "." + lastToken;
+ } else {
+ String v = getFullName(is, lastToken);
+ if(v != null) {
+ value = v;
+ }
+ }
+ return value;
+ }
+ String prefix = stringValue.substring(0, lastDot);
+ String t = EclipseJavaUtil.resolveType(type, prefix);
+ if(t != null) {
+ IType q = EclipseJavaUtil.findType(type.getJavaProject(), t);
+ if(q != null && q.getField(lastToken).exists()) {
+ value = t + "." + lastToken;
+ }
+ }
+
+ } catch (CoreException e) {
+ CommonPlugin.getDefault().logError(e);
+ }
+ }
+
+ return value;
+ }
+
+ private String getFullName(IImportDeclaration[] is, String name) {
+ for (IImportDeclaration d: is) {
+ String n = d.getElementName();
+ if(n.equals(name) || n.endsWith("." + name)) {
+ return n;
+ }
+ }
+ return null;
+ }
}
\ No newline at end of file
Modified:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseJavaUtil.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseJavaUtil.java 2011-08-17
23:19:12 UTC (rev 34022)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseJavaUtil.java 2011-08-17
23:40:05 UTC (rev 34023)
@@ -13,10 +13,13 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import org.eclipse.core.resources.IProject;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IAnnotatable;
import org.eclipse.jdt.core.IAnnotation;
@@ -210,4 +213,46 @@
return null;
}
+ /**
+ * Finds field declared in the given type or its super types.
+ *
+ * @param type
+ * @param name
+ * @return
+ * @throws CoreException
+ */
+ public static IField findField(IType type, String name) throws CoreException {
+ return findField(type, name, new HashSet<IType>());
+ }
+ private static IField findField(IType type, String name, Set<IType> processed)
throws CoreException {
+ if(!type.exists() || processed.contains(type)) {
+ return null;
+ }
+ processed.add(type);
+ if(type.getField(name).exists()) {
+ return type.getField(name);
+ }
+ IField f = findField(type, type.getSuperclassName(), name, processed);
+ String[] is = type.getSuperInterfaceNames();
+ for (int i = 0; f == null && i < is.length; i++) {
+ f = findField(type, is[i], name, processed);
+ }
+ if(f == null) {
+ IType d = type.getDeclaringType();
+ if(d != null && d != type && d.exists()) {
+ f = findField(d, name);
+ }
+ }
+
+ return f;
+ }
+ private static IField findField(IType context, String typeName, String fieldName,
Set<IType> processed) throws CoreException {
+ typeName = resolveType(context, typeName);
+ if(typeName != null) {
+ IType s = findType(context.getJavaProject(), typeName);
+ return (s != null) ? findField(s, fieldName, processed) : null;
+ }
+ return null;
+ }
+
}
\ No newline at end of file