Author: dgeraskov
Date: 2009-03-16 10:01:22 -0400 (Mon, 16 Mar 2009)
New Revision: 14217
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/Utils.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-4028
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java 2009-03-16
12:51:31 UTC (rev 14216)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/collect/CollectEntityInfo.java 2009-03-16
14:01:22 UTC (rev 14217)
@@ -14,10 +14,11 @@
import java.util.Iterator;
import java.util.List;
+import org.eclipse.core.runtime.Assert;
import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
+import org.eclipse.jdt.core.dom.Annotation;
import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.FieldAccess;
@@ -45,6 +46,7 @@
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo;
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.JPAConst;
import org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType;
+import org.hibernate.eclipse.jdt.ui.internal.jpa.common.Utils;
/**
* Visitor to collect information about JPA entity.
@@ -65,7 +67,7 @@
public boolean visit(CompilationUnit node) {
entityInfo.setFullyQualifiedName(
node.getTypeRoot().findPrimaryType().getFullyQualifiedName());
- if (node.getProblems().length > 0) {
+ if (node.getProblems().length > 0) {//this includes warnings too
entityInfo.setCompilerProblemsFlag(true);
}
return true;
@@ -262,6 +264,7 @@
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof SimpleType) {
+ //TODO process interfaces
SimpleType st = (SimpleType)obj;
String fullyQualifiedName = st.getName().getFullyQualifiedName();
if (JPAConst.IMPORT_SERIALIZABLE.compareTo(fullyQualifiedName) == 0) {
@@ -314,12 +317,15 @@
return true;
}
// -) is it setter?
- if (node.getName().getIdentifier().startsWith("set")) { //$NON-NLS-1$
+ if (node.getName().getIdentifier().startsWith("set")
+ && node.parameters().size() == 1) { //$NON-NLS-1$
// setter - do not process it
return true;
}
// +) is it getter?
- if (!node.getName().getIdentifier().startsWith("get")) { //$NON-NLS-1$
+ if (!(node.getName().getIdentifier().startsWith("get")
+ || node.getName().getIdentifier().startsWith("is"))
+ || node.parameters().size() > 0) { //$NON-NLS-1$
// not the getter - do not process it
return true;
}
@@ -392,15 +398,42 @@
}
} else if (type.isArrayType()) {
ArrayType at = (ArrayType)type;
- ITypeBinding tb = at.resolveBinding();
+ Type componentType = at;
+ while (componentType.isArrayType()){
+ componentType = ((ArrayType)componentType).getComponentType();
+ }
+ ITypeBinding tb = componentType.resolveBinding();
+ if (tb != null) {
+ if (tb.getJavaElement() instanceof SourceType) {
+ String entityFullyQualifiedName = ""; //$NON-NLS-1$
+ SourceType sourceT = (SourceType)tb.getJavaElement();
+ try {
+ entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
+ } catch (JavaModelException e) {
+ HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException:
", e); //$NON-NLS-1$
+ }
+ entityInfo.addDependency(entityFullyQualifiedName);
+ Iterator itVarNames = list.iterator();
+ while (itVarNames.hasNext()) {
+ String name = (String)itVarNames.next();
+ entityInfo.addReference(name, entityFullyQualifiedName, RefType.ONE2MANY);
+ }
+ }
+ }
} else if (type.isParameterizedType()) {
ParameterizedType pt = (ParameterizedType)type;
Type typeP = (Type)pt.getType();
ITypeBinding tb = typeP.resolveBinding();
if (tb != null) {
- ITypeBinding[] interfaces = tb.getTypeDeclaration().getInterfaces();
+ ITypeBinding[] interfaces = Utils.getAllInterfaces(tb);
String fullyQualifiedNameTypeName = ""; //$NON-NLS-1$
- for (int i = 0; i < interfaces.length; i++) {
+ if (Utils.isImplementInterface(interfaces,
"java.util.Collection")){//$NON-NLS-1$
+ fullyQualifiedNameTypeName = "java.util.Collection";//$NON-NLS-1$
+ }
+ if (Utils.isImplementInterface(interfaces, "java.util.Map")){//$NON-NLS-1$
+ fullyQualifiedNameTypeName = "java.util.Map";//$NON-NLS-1$
+ }
+ /*for (int i = 0; i < interfaces.length; i++) {
if (interfaces[i].getJavaElement() instanceof BinaryType) {
BinaryType binaryT = (BinaryType)interfaces[i].getJavaElement();
String tmp = binaryT.getFullyQualifiedName('.');
@@ -409,7 +442,7 @@
break;
}
}
- }
+ }*/
if (fullyQualifiedNameTypeName.length() > 0) {
Iterator typeArgsIt = pt.typeArguments().iterator();
while (typeArgsIt.hasNext()) {
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/Utils.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/Utils.java 2009-03-16
12:51:31 UTC (rev 14216)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.jdt.ui/src/org/hibernate/eclipse/jdt/ui/internal/jpa/common/Utils.java 2009-03-16
14:01:22 UTC (rev 14217)
@@ -24,6 +24,8 @@
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.jdt.core.dom.ITypeBinding;
+import org.eclipse.jdt.core.dom.MethodDeclaration;
/**
* Compilation unit common functions
@@ -43,6 +45,15 @@
static public ICompilationUnit findCompilationUnit(IJavaProject javaProject,
String fullyQualifiedName) {
+ IType lwType = findType(javaProject, fullyQualifiedName);
+ if (lwType != null) {
+ return lwType.getCompilationUnit();
+ }
+ return null;
+ }
+
+ static public IType findType(IJavaProject javaProject,
+ String fullyQualifiedName) {
IType lwType = null;
try {
lwType = javaProject.findType(fullyQualifiedName);
@@ -50,11 +61,7 @@
// just ignore it!
//HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException:
", e); //$NON-NLS-1$
}
- ICompilationUnit resCompilationUnit = null;
- if (lwType != null) {
- resCompilationUnit = lwType.getCompilationUnit();
- }
- return resCompilationUnit;
+ return lwType;
}
static public ICompilationUnit findCompilationUnit(String fullyQualifiedName) {
@@ -154,4 +161,37 @@
}
return OwnerType.UNDEF;
}
+
+ public static boolean isImplementInterface(ITypeBinding[] interfaces, String
parentInterface){
+ for (int i = 0; i < interfaces.length; i++) {
+ ITypeBinding typeBinding = interfaces[i];
+ if (parentInterface.equals(typeBinding.getBinaryName())) return true;
+ if (isImplementInterface(typeBinding.getInterfaces(), parentInterface)) return true;
+ }
+ return false;
+ }
+
+ public static ITypeBinding[] getAllInterfaces(ITypeBinding tb){
+ ITypeBinding[] interfaces = tb.getInterfaces();
+ if (tb.isInterface()) {
+ ITypeBinding[] allInterfaces = new ITypeBinding[interfaces.length + 1];
+ System.arraycopy(interfaces, 0, allInterfaces, 0, interfaces.length);
+ allInterfaces[interfaces.length] = tb;
+ interfaces = allInterfaces;
+ }
+ return interfaces;
+ }
+
+ public static String getFieldNameByGetter(MethodDeclaration node){
+ if (node.parameters().size() != 0) return null;
+ String methodName = node.getName().getIdentifier();
+ if (methodName.startsWith("get") && methodName.length() > 3){
+ methodName = methodName.substring(3);
+ return Character.toLowerCase(methodName.charAt(0)) + methodName.substring(1);
+ } else if (methodName.startsWith("is") && methodName.length() >
2){
+ methodName = methodName.substring(2);
+ return Character.toLowerCase(methodName.charAt(0)) + methodName.substring(1);
+ }
+ return null;
+ }
}