Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpfullbright2011-08-16 15:55:57 -0400
committerpfullbright2011-08-16 15:55:57 -0400
commit97ad59a0f924d4eb66498f516cf07012d5290f37 (patch)
treeddbb89012df0614bf145532192cb3282f25ef92a
parent57241c4c8abbbcd876ca5b91846cb73e2a8348a4 (diff)
downloadwebtools.dali-97ad59a0f924d4eb66498f516cf07012d5290f37.tar.gz
webtools.dali-97ad59a0f924d4eb66498f516cf07012d5290f37.tar.xz
webtools.dali-97ad59a0f924d4eb66498f516cf07012d5290f37.zip
changed JDTTools.typeNamedImplementsInterfaceNamed(..) to typeIsSubType(..) and added tests
-rw-r--r--common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/JDTTools.java86
-rw-r--r--common/tests/org.eclipse.jpt.common.core.tests/src/org/eclipse/jpt/common/core/tests/internal/utility/jdt/JDTToolsTests.java51
-rw-r--r--common/tests/org.eclipse.jpt.common.core.tests/src/org/eclipse/jpt/common/core/tests/internal/utility/jdt/JptCommonCoreUtilityJdtTests.java1
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/context/persistence/EclipseLinkPersistenceUnit.java8
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/java/JavaEclipseLinkConverterClassConverter.java2
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkConverterClassConverter.java2
-rw-r--r--jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkCustomizer.java2
7 files changed, 106 insertions, 46 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/JDTTools.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/JDTTools.java
index c91dec73c6..fe1f94bbb0 100644
--- a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/JDTTools.java
+++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/JDTTools.java
@@ -11,7 +11,6 @@ package org.eclipse.jpt.common.core.internal.utility;
import java.io.FileNotFoundException;
import java.util.ArrayList;
-
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
@@ -24,10 +23,13 @@ import org.eclipse.jpt.common.core.JptCommonCorePlugin;
import org.eclipse.jpt.common.utility.Filter;
import org.eclipse.jpt.common.utility.internal.ArrayTools;
import org.eclipse.jpt.common.utility.internal.ClassName;
+import org.eclipse.jpt.common.utility.internal.NotNullFilter;
import org.eclipse.jpt.common.utility.internal.ReflectionTools;
import org.eclipse.jpt.common.utility.internal.iterables.ArrayIterable;
+import org.eclipse.jpt.common.utility.internal.iterables.CompositeIterable;
import org.eclipse.jpt.common.utility.internal.iterables.EmptyIterable;
import org.eclipse.jpt.common.utility.internal.iterables.FilteringIterable;
+import org.eclipse.jpt.common.utility.internal.iterables.SingleElementIterable;
/**
* Convenience methods for dealing with JDT core
@@ -68,70 +70,76 @@ public final class JDTTools {
private static final IJavaElement[] EMPTY_JAVA_ELEMENT_ARRAY = new IJavaElement[0];
/**
- * Climb the specified type's inheritance hierarchy looking for the
- * specified interface.
+ * Climb the specified type's inheritance hierarchy looking for the specified interface.
*/
- public static boolean typeNamedImplementsInterfaceNamed(IJavaProject javaProject, String typeName, String interfaceName) {
+ public static boolean typeIsSubType(IJavaProject javaProject, String potentialSubType, String potentialSuperType) {
try {
- return typeImplementsInterface(javaProject, javaProject.findType(typeName), javaProject.findType(interfaceName));
+ return typeIsSubType(javaProject, javaProject.findType(potentialSubType), javaProject.findType(potentialSuperType));
} catch (JavaModelException ex) {
JptCommonCorePlugin.log(ex);
return false;
}
}
- private static boolean typeImplementsInterfaceNamed(IJavaProject javaProject, IType type, String interfaceName) throws JavaModelException {
- return typeImplementsInterface(javaProject, type, javaProject.findType(interfaceName));
+ private static boolean typeIsSubType(IJavaProject javaProject, IType potentialSubType, String potentialSuperType) throws JavaModelException {
+ return typeIsSubType(javaProject, potentialSubType, javaProject.findType(potentialSuperType));
}
- private static boolean typeImplementsInterface(IJavaProject javaProject, IType type, IType interfase) throws JavaModelException {
- if ((type == null) || (interfase == null)) {
+ private static boolean typeIsSubType(IJavaProject javaProject, IType potentialSubType, IType potentialSuperType) throws JavaModelException {
+ if ((potentialSubType == null) || (potentialSuperType == null)) {
return false;
}
-
- String interfaceName = interfase.getFullyQualifiedName();
- for (String superInterfaceName : resolveSuperInterfaceNames(type)) {
- if (superInterfaceName.equals(interfaceName)) {
+
+ // short cut if potential supertype is "java.lang.Object"
+ if (Object.class.getName().equals(potentialSuperType)) {
+ return true;
+ }
+
+ String potentialSuperTypeName = potentialSuperType.getFullyQualifiedName();
+
+ for (String superTypeName : getResolvedSuperTypeNames(potentialSubType)) {
+ if (superTypeName.equals(potentialSuperTypeName)) {
return true;
}
- // recurse into super interface
- if (typeImplementsInterface(javaProject, javaProject.findType(superInterfaceName), interfase)) {
+
+ // recurse into super type
+ if (typeIsSubType(javaProject, javaProject.findType(superTypeName), potentialSuperType)) {
return true;
}
}
-
- if (type.getSuperclassName() == null) {
- return false;
- }
- // recurse into superclass
- return typeImplementsInterface(javaProject, javaProject.findType(resolveSuperclassName(type)), interfase);
+
+ return false;
}
/**
- * Return the names of the specified type's super interfaces.
- * This is necessary because, for whatever reason, {@link IType#getSuperInterfaceNames()}
- * returns unqualified names when the type is from Java source.
+ * Return the names of the specified type's supertypes (class and interfaces).
+ * This is necessary because, for whatever reason, { @link IType#getSuperInterfaceNames()}
+ * {@link IType#getSuperclassName()} return unqualified names when the type is from Java source.
*/
- private static Iterable<String> resolveSuperInterfaceNames(IType type) throws JavaModelException {
+ private static Iterable<String> getResolvedSuperTypeNames(IType type) throws JavaModelException {
+ Iterable<String> nonResolvedSuperTypeNames = getNonResolvedSuperTypeNames(type);
if (type.isBinary()) {
- return new ArrayIterable<String>(type.getSuperInterfaceNames());
+ // if type is binary, the types are already resolved
+ return nonResolvedSuperTypeNames;
}
- ArrayList<String> resolvedSuperInterfaceNames = new ArrayList<String>();
- for (String superInterfaceName : type.getSuperInterfaceNames()) {
- resolvedSuperInterfaceNames.add(resolveType(type, superInterfaceName));
+ ArrayList<String> resolvedSuperTypeNames = new ArrayList<String>();
+ for (String superTypeName : nonResolvedSuperTypeNames) {
+ resolvedSuperTypeNames.add(resolveType(type, superTypeName));
}
- return resolvedSuperInterfaceNames;
+ return resolvedSuperTypeNames;
}
-
+
/**
- * Return the name of the specified type's superclass.
- * This is necessary because, for whatever reason, {@link IType#getSuperclassName()}
- * returns unqualified names when the type is from Java source.
+ * Return the (potentially) non-resolved names of the specified type's supertypes (class and interfaces).
+ * This is necessary because, for whatever reason, { @link IType#getSuperInterfaceNames()} and
+ * {@link IType#getSuperclassName()} return unqualified names when the type is from Java source.
*/
- private static String resolveSuperclassName(IType type) throws JavaModelException {
- return type.isBinary() ?
- type.getSuperclassName() :
- resolveType(type, type.getSuperclassName());
+ private static Iterable<String> getNonResolvedSuperTypeNames(IType type) throws JavaModelException {
+ return new CompositeIterable<String>(
+ new FilteringIterable<String>(
+ new SingleElementIterable<String>(type.getSuperclassName()),
+ NotNullFilter.<String>instance()),
+ new ArrayIterable<String>(type.getSuperInterfaceNames()));
}
/**
@@ -227,7 +235,7 @@ public final class JDTTools {
if (typeIsOtherValidBasicType(fullyQualifiedName)) {
return true;
}
- if (typeImplementsInterfaceNamed(javaProject, type, SERIALIZABLE_CLASS_NAME)) {
+ if (typeIsSubType(javaProject, type, SERIALIZABLE_CLASS_NAME)) {
return true;
}
if (type.isEnum()) {
diff --git a/common/tests/org.eclipse.jpt.common.core.tests/src/org/eclipse/jpt/common/core/tests/internal/utility/jdt/JDTToolsTests.java b/common/tests/org.eclipse.jpt.common.core.tests/src/org/eclipse/jpt/common/core/tests/internal/utility/jdt/JDTToolsTests.java
new file mode 100644
index 0000000000..d5d1c3eec9
--- /dev/null
+++ b/common/tests/org.eclipse.jpt.common.core.tests/src/org/eclipse/jpt/common/core/tests/internal/utility/jdt/JDTToolsTests.java
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jpt.common.core.tests.internal.utility.jdt;
+
+import java.util.AbstractList;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Vector;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jpt.common.core.internal.utility.JDTTools;
+
+
+public class JDTToolsTests
+ extends AnnotationTestCase {
+
+ public JDTToolsTests(String name) {
+ super(name);
+ }
+
+
+ public void testTypeIsSubtype() throws Exception {
+ IJavaProject jProj = getJavaProject().getJavaProject();
+
+ // concrete type is subtype of interface
+ assertTrue(JDTTools.typeIsSubType(jProj, ArrayList.class.getName(), List.class.getName()));
+
+ // concrete type is not subtype of interface
+ assertFalse(JDTTools.typeIsSubType(jProj, ArrayList.class.getName(), Map.class.getName()));
+
+ // interface is subtype of interface
+ assertTrue(JDTTools.typeIsSubType(jProj, List.class.getName(), Collection.class.getName()));
+
+ // interface is not subtype of interface
+ assertFalse(JDTTools.typeIsSubType(jProj, List.class.getName(), Map.class.getName()));
+
+ // concrete type is subtype of concrete type
+ assertTrue(JDTTools.typeIsSubType(jProj, ArrayList.class.getName(), AbstractList.class.getName()));
+
+ // concrete type is not subtype of concrete type
+ assertFalse(JDTTools.typeIsSubType(jProj, ArrayList.class.getName(), Vector.class.getName()));
+ }
+}
diff --git a/common/tests/org.eclipse.jpt.common.core.tests/src/org/eclipse/jpt/common/core/tests/internal/utility/jdt/JptCommonCoreUtilityJdtTests.java b/common/tests/org.eclipse.jpt.common.core.tests/src/org/eclipse/jpt/common/core/tests/internal/utility/jdt/JptCommonCoreUtilityJdtTests.java
index df288615f1..0f485953b4 100644
--- a/common/tests/org.eclipse.jpt.common.core.tests/src/org/eclipse/jpt/common/core/tests/internal/utility/jdt/JptCommonCoreUtilityJdtTests.java
+++ b/common/tests/org.eclipse.jpt.common.core.tests/src/org/eclipse/jpt/common/core/tests/internal/utility/jdt/JptCommonCoreUtilityJdtTests.java
@@ -19,6 +19,7 @@ public class JptCommonCoreUtilityJdtTests {
suite.addTestSuite(CombinationIndexedDeclarationAnnotationAdapterTests.class);
suite.addTestSuite(DefaultAnnotationEditFormatterTests.class);
suite.addTestSuite(ASTToolsTests.class);
+ suite.addTestSuite(JDTToolsTests.class);
suite.addTestSuite(MemberAnnotationElementAdapterTests.class);
suite.addTestSuite(NestedDeclarationAnnotationAdapterTests.class);
suite.addTestSuite(NestedIndexedDeclarationAnnotationAdapterTests.class);
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/context/persistence/EclipseLinkPersistenceUnit.java b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/context/persistence/EclipseLinkPersistenceUnit.java
index b755b627bd..73c035b3ba 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/context/persistence/EclipseLinkPersistenceUnit.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/context/persistence/EclipseLinkPersistenceUnit.java
@@ -640,7 +640,7 @@ public class EclipseLinkPersistenceUnit
loggerProperty.getValidationTextRange()
)
);
- } else if (!JDTTools.typeNamedImplementsInterfaceNamed(
+ } else if (!JDTTools.typeIsSubType(
javaProject, loggerProperty.getValue(), Logging.ECLIPSELINK_LOGGER_CLASS_NAME)
) {
messages.add(
@@ -691,7 +691,7 @@ public class EclipseLinkPersistenceUnit
handlerProperty.getValidationTextRange()
)
);
- } else if (!JDTTools.typeNamedImplementsInterfaceNamed(
+ } else if (!JDTTools.typeIsSubType(
javaProject, handlerProperty.getValue(), Customization.ECLIPSELINK_EXCEPTION_HANDLER_CLASS_NAME)
) {
messages.add(
@@ -747,7 +747,7 @@ public class EclipseLinkPersistenceUnit
profilerProperty.getValidationTextRange()
)
);
- } else if (!JDTTools.typeNamedImplementsInterfaceNamed(
+ } else if (!JDTTools.typeIsSubType(
javaProject, profilerProperty.getValue(), Customization.ECLIPSELINK_SESSION_PROFILER_CLASS_NAME)
) {
messages.add(
@@ -799,7 +799,7 @@ public class EclipseLinkPersistenceUnit
property.getValidationTextRange()
)
);
- } else if (!JDTTools.typeNamedImplementsInterfaceNamed(
+ } else if (!JDTTools.typeIsSubType(
javaProject, property.getValue(), Customization.ECLIPSELINK_SESSION_CUSTOMIZER_CLASS_NAME)
) {
messages.add(
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/java/JavaEclipseLinkConverterClassConverter.java b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/java/JavaEclipseLinkConverterClassConverter.java
index c7f9dc47e9..cd24dcb264 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/java/JavaEclipseLinkConverterClassConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/java/JavaEclipseLinkConverterClassConverter.java
@@ -156,7 +156,7 @@ public abstract class JavaEclipseLinkConverterClassConverter<A extends EclipseLi
*/
protected boolean typeImplementsInterface(String typeName, String interfaceName) {
return (typeName != null) &&
- JDTTools.typeNamedImplementsInterfaceNamed(this.getJavaProject(), typeName, interfaceName);
+ JDTTools.typeIsSubType(this.getJavaProject(), typeName, interfaceName);
}
protected boolean converterClassImplementsInterface(String interfaceName) {
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkConverterClassConverter.java b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkConverterClassConverter.java
index 6da7315235..9868e13404 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkConverterClassConverter.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkConverterClassConverter.java
@@ -158,7 +158,7 @@ public abstract class OrmEclipseLinkConverterClassConverter<X extends XmlNamedCo
*/
protected boolean typeImplementsInterface(String typeName, String interfaceName) {
return (typeName != null) &&
- JDTTools.typeNamedImplementsInterfaceNamed(this.getJavaProject(), typeName, interfaceName);
+ JDTTools.typeIsSubType(this.getJavaProject(), typeName, interfaceName);
}
protected boolean converterClassImplementsInterface(String interfaceName) {
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkCustomizer.java b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkCustomizer.java
index f7a18b6c40..7071a3aa08 100644
--- a/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkCustomizer.java
+++ b/jpa/plugins/org.eclipse.jpt.jpa.eclipselink.core/src/org/eclipse/jpt/jpa/eclipselink/core/internal/context/orm/OrmEclipseLinkCustomizer.java
@@ -275,7 +275,7 @@ public class OrmEclipseLinkCustomizer
this.getValidationTextRange()
)
);
- } else if (!JDTTools.typeNamedImplementsInterfaceNamed(javaProject, this.getCustomizerClass(), ECLIPSELINK_DESCRIPTOR_CUSTOMIZER_CLASS_NAME)) {
+ } else if (!JDTTools.typeIsSubType(javaProject, this.getCustomizerClass(), ECLIPSELINK_DESCRIPTOR_CUSTOMIZER_CLASS_NAME)) {
messages.add(
DefaultEclipseLinkJpaValidationMessages.buildMessage(
IMessage.HIGH_SEVERITY,

Back to the top