diff options
author | nhauge | 2011-01-20 23:11:50 +0000 |
---|---|---|
committer | nhauge | 2011-01-20 23:11:50 +0000 |
commit | 846bb81bda67823e1ed285ca03f1cb75ad7ed21a (patch) | |
tree | f4bc91eb1fc691bd9549313a129dbf5f1df9ce4f /jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal | |
parent | 66480f7cb94e16d7f9c15975e94e23b6d8537f26 (diff) | |
download | webtools.dali-846bb81bda67823e1ed285ca03f1cb75ad7ed21a.tar.gz webtools.dali-846bb81bda67823e1ed285ca03f1cb75ad7ed21a.tar.xz webtools.dali-846bb81bda67823e1ed285ca03f1cb75ad7ed21a.zip |
[331558] validation added for converter class. Patch from Les.
Diffstat (limited to 'jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal')
2 files changed, 176 insertions, 3 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/ASTTools.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/ASTTools.java index 531ae81be7..828319f8c9 100644 --- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/ASTTools.java +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/ASTTools.java @@ -183,15 +183,27 @@ public class ASTTools { return typeBinding; } visited.add(typeName); + + ITypeBinding interfaceBinding = findTypeInInterfaces(typeBinding, searchTypeName, visited); + if (interfaceBinding != null) { + return interfaceBinding; + } + + return findTypeInSuperclasses(typeBinding, searchTypeName, visited); + } + private static ITypeBinding findTypeInInterfaces(ITypeBinding typeBinding, String searchTypeName, HashSet<String> visited) { ITypeBinding[] interfaceBindings = typeBinding.getInterfaces(); for (ITypeBinding interfaceBinding : interfaceBindings) { // recurse up interfaces ITypeBinding result = findTypeInHierarchy(interfaceBinding, searchTypeName, visited); if (result != null) { - return result; + return result; } } - + return null; + } + + private static ITypeBinding findTypeInSuperclasses(ITypeBinding typeBinding, String searchTypeName, HashSet<String> visited) { ITypeBinding superBinding = typeBinding.getSuperclass(); if (superBinding != null) { // recurse up superclasses ITypeBinding result = findTypeInHierarchy(superBinding, searchTypeName, visited); @@ -199,8 +211,41 @@ public class ASTTools { return result; } } + return null; + } + /** + * Return whether the specified expression is a type literal and the type binding + * corresponding to the specified interface name exists in the type + * literal's inheritance hierarchy (superclasses and interfaces). + * Return null otherwise. + */ + public static boolean typeImplementsInterface(Expression expression, String searchInterfaceName) { + ITypeBinding typeBinding = resolveTypeBinding(expression); + if (typeBinding == null) { + return false; + } else { + return findInterfaceInHierarchy(typeBinding, searchInterfaceName) != null; + } + } + + /** + * Return whether a type binding with the specified interface name exists in + * the specified type binding's inheritance hierarchy (superclasses + * and interfaces). + */ + public static boolean typeImplementsInterface(ITypeBinding typeBinding, String searchInterfaceName) { + return findInterfaceInHierarchy(typeBinding, searchInterfaceName) != null; + } + + private static ITypeBinding findInterfaceInHierarchy(ITypeBinding typeBinding, String searchInterfaceName) { + HashSet<String> visited = new HashSet<String>(); + ITypeBinding interfaceBinding = findTypeInInterfaces(typeBinding, searchInterfaceName, visited); + if (interfaceBinding != null) { + return interfaceBinding; + } + + return findTypeInSuperclasses(typeBinding, searchInterfaceName, visited); - return null; } } diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/JDTTools.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/JDTTools.java index 9db2db61fc..a7c0f54edc 100644 --- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/JDTTools.java +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/JDTTools.java @@ -10,12 +10,16 @@ package org.eclipse.jpt.core.internal.utility.jdt; import java.io.FileNotFoundException; +import java.util.ArrayList; + import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jpt.core.JptCorePlugin; import org.eclipse.jpt.utility.Filter; +import org.eclipse.jpt.utility.internal.CollectionTools; import org.eclipse.jpt.utility.internal.iterables.ArrayIterable; import org.eclipse.jpt.utility.internal.iterables.EmptyIterable; import org.eclipse.jpt.utility.internal.iterables.FilteringIterable; @@ -56,6 +60,130 @@ public final class JDTTools } private static final IJavaElement[] EMPTY_JAVA_ELEMENT_ARRAY = new IJavaElement[0]; + + public static boolean typeNamedImplementsInterfaceNamed(IJavaProject javaProject, String typeName, String interfaceNamed) { + try { + return typeImplementsInterface(javaProject, javaProject.findType(typeName), javaProject.findType(interfaceNamed)); + } + catch (JavaModelException ex) { + JptCorePlugin.log(ex); + return false; + } + } + + public static boolean typeImplementsInterface(IJavaProject javaProject, IType type, IType interfase) { + try { + if (type == null || interfase == null) { + return false; + } + + Iterable<String> resolvedSuperInterfaceNames = resolveSuperInterfaceNames(type); + if (CollectionTools.contains(resolvedSuperInterfaceNames, interfase.getFullyQualifiedName())) { + return true; + } + + for (String interfaceName : resolveSuperInterfaceNames(type)) { + IType superInterface = javaProject.findType(interfaceName); + if (superInterface != null && typeImplementsInterface(javaProject, superInterface, interfase)) { + return true; + } + } + + if (type.getSuperclassName() == null) { + return false; + } + + return typeImplementsInterface(javaProject, getJDTSuperclass(javaProject, type), interfase); + } + catch (JavaModelException ex) { + return false; + } + + } + + /** + * This is necessary because for whatever reason getSuperClassName() on IType returns unqualified names + * when the type is java sourced. + * @param type + * @return String - resolved super class name or null + */ + private static String resolveSuperClassName(final IType type) { + try { + if (!type.isBinary()) { + String superClassName = type.getSuperclassName(); + String[][] resolvedSuperClassName = type.resolveType(superClassName); + return resolvedSuperClassName[0][0] + "." + resolvedSuperClassName[0][1]; + } else { + return type.getSuperclassName(); + } + } catch (JavaModelException ex) { + JptCorePlugin.log(ex); + return null; + } + } + + /** + * This is necessary because for whatever reason getSuperInterfaceNames() on IType returns unqualified names + * when the type is java sourced. + * @param type + * @return Iterable<String> - resolved super interface names + */ + private static Iterable<String> resolveSuperInterfaceNames(final IType type) { + try { + if (!type.isBinary()) { + ArrayList<String> resolvedSuperInterfaceNames = new ArrayList<String>(); + for (String superInterfaceName : new ArrayIterable<String>(type.getSuperInterfaceNames())) { + String[][] resolvedTypeNames = type.resolveType(superInterfaceName); + for (String[] resolvedTypeName : resolvedTypeNames) { + resolvedSuperInterfaceNames.add(resolvedTypeName[0] + "." + resolvedTypeName[1]); + } + } + return resolvedSuperInterfaceNames; + } else { + return new ArrayIterable<String>(type.getSuperInterfaceNames()); + } + } + catch (JavaModelException ex) { + JptCorePlugin.log(ex); + return EmptyIterable.instance(); + } + } + + public static IType getJDTType(IJavaProject javaProject, String fullyQualifiedName) { + try { + return javaProject.findType(fullyQualifiedName); + } + catch (JavaModelException ex) { + JptCorePlugin.log(ex); + return null; + } + } + + public static IType getJDTSuperclass(IJavaProject javaProject, IType child) { + try { + return javaProject.findType(resolveSuperClassName(child)); + } + catch (JavaModelException ex) { + JptCorePlugin.log(ex); + return null; + } + } + + public static Iterable<IType> getJDTSuperInterfaces(IJavaProject javaProject, IType child) { + ArrayList<IType> superclassInterfaces = new ArrayList<IType>(); + try { + Iterable<String> superInterfaceNameIterable = resolveSuperInterfaceNames(child); + for (String superInterfaceName : superInterfaceNameIterable) { + superclassInterfaces.add(javaProject.findType(superInterfaceName)); + } + } + catch (JavaModelException ex) { + JptCorePlugin.log(ex); + return EmptyIterable.instance(); + } + + return superclassInterfaces; + } public static Iterable<IPackageFragmentRoot> getJavaSourceFolders(IJavaProject javaProject) { try { |