[jbosstools-commits] JBoss Tools SVN: r43229 - in trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common: util and 1 other directory.

jbosstools-commits at lists.jboss.org jbosstools-commits at lists.jboss.org
Fri Aug 24 17:51:12 EDT 2012


Author: scabanovich
Date: 2012-08-24 17:51:12 -0400 (Fri, 24 Aug 2012)
New Revision: 43229

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
   trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/TypeResolutionCache.java
Log:
JBIDE-12490
https://issues.jboss.org/browse/JBIDE-12490


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	2012-08-24 20:21:29 UTC (rev 43228)
+++ trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/java/impl/AnnotationDeclaration.java	2012-08-24 21:51:12 UTC (rev 43229)
@@ -140,7 +140,7 @@
 				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;
+						value = f.getDeclaringType().getFullyQualifiedName() + "." + lastToken; //$NON-NLS-1$
 					} else {
 						String v = getFullName(type, is, lastToken);
 						if(v != null) {
@@ -150,13 +150,17 @@
 					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;
+				String t = prefix;
+				IType q = EclipseJavaUtil.findType(type.getJavaProject(), prefix);
+				if(q == null) {
+					t = EclipseJavaUtil.resolveType(type, prefix);
+					if(t != null) {
+						q = EclipseJavaUtil.findType(type.getJavaProject(), t);
 					}
 				}
+				if(q != null && q.getField(lastToken).exists()) {
+					value = t + "." + lastToken; //$NON-NLS-1$
+				}
 				
 			} catch (CoreException e) {
 				CommonPlugin.getDefault().logError(e);
@@ -169,16 +173,16 @@
 	private String getFullName(IType type, IImportDeclaration[] is, String name) throws CoreException {
 		for (IImportDeclaration d: is) {
 			String n = d.getElementName();
-			if(n.equals(name) || n.endsWith("." + name)) {
+			if(n.equals(name) || n.endsWith("." + name)) { //$NON-NLS-1$
 				return n;
 			}
-			if(Flags.isStatic(d.getFlags()) && n.endsWith(".*")) {
+			if(Flags.isStatic(d.getFlags()) && n.endsWith(".*")) { //$NON-NLS-1$
 				String typename = n.substring(0, n.length() - 2);
 				IType t = EclipseJavaUtil.findType(type.getJavaProject(), typename);
 				if(t != null && t.exists()) {
 					IField f = EclipseJavaUtil.findField(t, name);
 					if(f != null) {
-						return f.getDeclaringType().getFullyQualifiedName() + "." + name;
+						return f.getDeclaringType().getFullyQualifiedName() + "." + name; //$NON-NLS-1$
 					}
 				}
 				

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	2012-08-24 20:21:29 UTC (rev 43228)
+++ trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/EclipseJavaUtil.java	2012-08-24 21:51:12 UTC (rev 43229)
@@ -66,6 +66,8 @@
 	public static String resolveTypeAsString(IType type, String typeName) {
 		if(type == null || typeName == null) return null;
 		typeName = new String(Signature.toCharArray(typeName.toCharArray()));
+		int i = typeName.indexOf(Signature.C_GENERIC_START);
+		if(i > 0) typeName = typeName.substring(0, i);
 		return resolveType(type, typeName);
 	}
 

Modified: trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/TypeResolutionCache.java
===================================================================
--- trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/TypeResolutionCache.java	2012-08-24 20:21:29 UTC (rev 43228)
+++ trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/util/TypeResolutionCache.java	2012-08-24 21:51:12 UTC (rev 43229)
@@ -10,10 +10,19 @@
   ******************************************************************************/
 package org.jboss.tools.common.util;
 
+import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IImportDeclaration;
 import org.eclipse.jdt.core.IJavaProject;
 import org.eclipse.jdt.core.IType;
 import org.eclipse.jdt.core.JavaModelException;
@@ -29,16 +38,125 @@
 	static class Resolved {
 		IType type;
 		Map<String, String> types = new Hashtable<String, String>();
+		List<String> classImports = new ArrayList<String>();
+		List<String> packageImports = new ArrayList<String>();
 		Resolved(IType type) {
 			this.type = type;
+			readImports();
 		}
 		
 		void setType(IType type) {
 			this.type = type;
 			types.clear();
+
+			readImports();
 		}
+	
+		void readImports() {
+			ICompilationUnit unit = type.getCompilationUnit();
+			if(unit == null) return;
+			IImportDeclaration[] ds = null;
+			try {
+				ds = unit.getImports();
+			} catch (JavaModelException e) {
+				CommonPlugin.getDefault().logError(e);
+				ds = new IImportDeclaration[0];
+			}
+			IResource r = unit.getResource();
+			
+			if(r instanceof IFile && r.exists()) {
+				List<String> newClassImports = new ArrayList<String>();
+				List<String> newPackageImports = new ArrayList<String>();
+				//add local package
+				newPackageImports.add(type.getPackageFragment().getElementName() + "."); //$NON-NLS-1$
+				for (IImportDeclaration d: ds) {
+					String q = d.getElementName();
+					if(q.endsWith(".*")) { //$NON-NLS-1$
+						newPackageImports.add( q = q.substring(0, q.length() - 1));
+					} else {
+						newClassImports.add(q);
+					}
+				}
+				classImports = newClassImports;
+				packageImports = newPackageImports;
+			}
+		}
+		
+		public String resolveInImports(String typeName) {
+			if(typeName.indexOf(".") >= 0) { //$NON-NLS-1$
+				try {
+					IType q = EclipseJavaUtil.findType(type.getJavaProject(), typeName);
+					if(q != null) {
+						types.put(typeName, typeName);
+						return typeName;
+					}
+				} catch (JavaModelException e) {
+					CommonPlugin.getDefault().logError(e);
+				}
+				//too difficult
+				return null; 
+			}
+			for (String imp: classImports) {
+				if(imp.endsWith("." + typeName)) { //$NON-NLS-1$
+					types.put(typeName, imp);
+					return imp;
+				}
+			}
+			for (String imp: packageImports) {
+				String result = imp + typeName;
+				try {
+					IType q = EclipseJavaUtil.findType(type.getJavaProject(), result);
+					if(q != null) {
+						types.put(typeName, result);
+						return result;
+					}
+				} catch (JavaModelException e) {
+					CommonPlugin.getDefault().logError(e);
+				}
+			}
+			String pr = "java.lang." + typeName; //$NON-NLS-1$
+			if(primitive.contains(pr)) {
+				types.put(typeName, pr);
+				return pr;
+			}
+
+			if(type.getTypeParameter(typeName).exists()) {
+				types.put(typeName, typeName);
+				return typeName;
+			}
+			return null;
+		}
 	}
 
+	static Set<String> primitive = new HashSet<String>();
+	static {
+		primitive.add("void"); //$NON-NLS-1$
+		primitive.add("int"); //$NON-NLS-1$
+		primitive.add("char"); //$NON-NLS-1$
+		primitive.add("boolean"); //$NON-NLS-1$
+		primitive.add("long"); //$NON-NLS-1$
+		primitive.add("short"); //$NON-NLS-1$
+		primitive.add("double"); //$NON-NLS-1$
+		primitive.add("float"); //$NON-NLS-1$
+		primitive.add("java.lang.Object"); //$NON-NLS-1$
+		primitive.add("java.lang.Number"); //$NON-NLS-1$
+		primitive.add("java.lang.Integer"); //$NON-NLS-1$
+		primitive.add("java.lang.Character"); //$NON-NLS-1$
+		primitive.add("java.lang.Boolean"); //$NON-NLS-1$
+		primitive.add("java.lang.Long"); //$NON-NLS-1$
+		primitive.add("java.lang.Short"); //$NON-NLS-1$
+		primitive.add("java.lang.Double"); //$NON-NLS-1$
+		primitive.add("java.lang.Float"); //$NON-NLS-1$
+		primitive.add("java.lang.String"); //$NON-NLS-1$
+		primitive.add("java.lang.StringBuffer"); //$NON-NLS-1$
+		primitive.add("java.lang.Class"); //$NON-NLS-1$
+		primitive.add("java.lang.Deprecated"); //$NON-NLS-1$
+		primitive.add("java.lang.SuppressWarnings"); //$NON-NLS-1$
+		primitive.add("java.lang.Throwable"); //$NON-NLS-1$
+		primitive.add("java.lang.Exception"); //$NON-NLS-1$
+		primitive.add("java.lang.RuntimeException"); //$NON-NLS-1$
+		primitive.add("java.lang.Override"); //$NON-NLS-1$
+	}
 	static String NULL = ";;;"; //$NON-NLS-1$
 	Map<String,Resolved> resolved = new Hashtable<String, Resolved>();
 
@@ -46,8 +164,8 @@
 	
 	public String resolveType(IType type, String typeName) {
 		if(type == null) return null;
-		if(type.isBinary() || typeName == null) return typeName;
-		
+		if(type.isBinary() || typeName == null || primitive.contains(typeName)) return typeName;
+
 		String n = getKey(type);
 		Resolved r = resolved.get(n);
 		if(r == null) {
@@ -62,8 +180,15 @@
 			return (result == NULL) ? null : result;
 		}
 
+		result = r.resolveInImports(typeName);
+		if(result != null) {
+			return result;
+		}
+		
 		result = __resolveType(type, typeName);
 		
+		System.out.println(typeName + "---" + result);
+		
 		r.types.put(typeName, result == null ? NULL : result);
 		return result;
 



More information about the jbosstools-commits mailing list