Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorpfullbright2011-08-16 19:55:57 +0000
committerpfullbright2011-08-16 19:55:57 +0000
commit97ad59a0f924d4eb66498f516cf07012d5290f37 (patch)
treeddbb89012df0614bf145532192cb3282f25ef92a /common
parent57241c4c8abbbcd876ca5b91846cb73e2a8348a4 (diff)
downloadwebtools.dali-97ad59a0f924d4eb66498f516cf07012d5290f37.tar.gz
webtools.dali-97ad59a0f924d4eb66498f516cf07012d5290f37.tar.xz
webtools.dali-97ad59a0f924d4eb66498f516cf07012d5290f37.zip
changed JDTTools.typeNamedImplementsInterfaceNamed(..) to typeIsSubType(..) and added tests
Diffstat (limited to 'common')
-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
3 files changed, 99 insertions, 39 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);

Back to the top