diff options
author | kmoore | 2011-02-14 21:07:26 +0000 |
---|---|---|
committer | kmoore | 2011-02-14 21:07:26 +0000 |
commit | ca8b6d9ac3c3fc2c769a6c476ed57a8d26c075b2 (patch) | |
tree | 5b04ddea932bfff92d4855c67d32f191ece72fb7 /common/plugins/org.eclipse.jpt.common.core | |
parent | 82cc76de19805d949f4298f198dffc5ffc6292df (diff) | |
download | webtools.dali-ca8b6d9ac3c3fc2c769a6c476ed57a8d26c075b2.tar.gz webtools.dali-ca8b6d9ac3c3fc2c769a6c476ed57a8d26c075b2.tar.xz webtools.dali-ca8b6d9ac3c3fc2c769a6c476ed57a8d26c075b2.zip |
310464 - element collection target class validation for unmapped non-basic types - patch from Les
Diffstat (limited to 'common/plugins/org.eclipse.jpt.common.core')
-rw-r--r-- | common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/JDTTools.java | 97 |
1 files changed, 96 insertions, 1 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/JDTTools.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/JDTTools.java index 63af352530..14cd0c2217 100644 --- a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/JDTTools.java +++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/utility/jdt/JDTTools.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2010 Oracle. All rights reserved. +* Copyright (c) 2010, 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. @@ -18,7 +18,10 @@ import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaModelException; 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.CollectionTools; +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.EmptyIterable; import org.eclipse.jpt.common.utility.internal.iterables.FilteringIterable; @@ -233,4 +236,96 @@ public final class JDTTools protected static Iterable<IPackageFragmentRoot> getPackageFragmentRoots(IJavaProject javaProject) throws JavaModelException { return new ArrayIterable<IPackageFragmentRoot>(javaProject.getPackageFragmentRoots()); } + + /** + *This returns true if the given fully qualified type name is of the basic type. + *@param fullyQualifiedName - may include array brackets but not generic type arguments + */ + public static boolean typeIsBasic(IJavaProject javaProject, String fullyQualifiedName) { + return typeIsBasic(javaProject, getJDTType(javaProject, fullyQualifiedName)); + } + + public static boolean typeIsBasic(IJavaProject javaProject, IType type) { + if (type == null) { + return false; + } + String fullyQualifiedName = type.getFullyQualifiedName(); + + if (fullyQualifiedName == null) { + return false; + } + + int arrayDepth = ReflectionTools.getArrayDepthForTypeDeclaration(fullyQualifiedName); + if (arrayDepth > 1) { + return false; // multi-dimensional arrays are not supported + } + + if (arrayDepth == 1) { + String elementTypeName = ReflectionTools.getElementTypeNameForTypeDeclaration(fullyQualifiedName, 1); + return elementTypeIsValidForBasicArray(elementTypeName); + } + + // arrayDepth == 0 + if (ClassName.isVariablePrimitive(fullyQualifiedName)) { + return true; // any primitive but 'void' + } + if (ClassName.isVariablePrimitiveWrapper(fullyQualifiedName)) { + return true; // any primitive wrapper but 'java.lang.Void' + } + if (typeIsOtherValidBasicType(fullyQualifiedName)) { + return true; + } + if (typeImplementsInterface(javaProject, type, getJDTType(javaProject, java.io.Serializable.class.getName()))) { + return true; + } + try { + if (type.isEnum()) { + return true; + } + } catch (JavaModelException ex) { + JptCommonCorePlugin.log(ex); + return false; + } + return false; + } + + /** + * Return whether the specified type is a valid element type for + * a one-dimensional array that can default to a basic mapping:<ul> + * <li><code>byte</code> + * <li><code>java.lang.Byte</code> + * <li><code>char</code> + * <li><code>java.lang.Character</code> + * </ul> + */ + public static boolean elementTypeIsValidForBasicArray(String elementTypeName) { + return ArrayTools.contains(VALID_BASIC_ARRAY_ELEMENT_TYPE_NAMES, elementTypeName); + } + + protected static final String[] VALID_BASIC_ARRAY_ELEMENT_TYPE_NAMES = { + byte.class.getName(), + char.class.getName(), + java.lang.Byte.class.getName(), + java.lang.Character.class.getName() + }; + + /** + * Return whether the specified type is among the various "other" types + * that can default to a basic mapping. + */ + public static boolean typeIsOtherValidBasicType(String typeName) { + return ArrayTools.contains(OTHER_VALID_BASIC_TYPE_NAMES, typeName); + } + + protected static final String[] OTHER_VALID_BASIC_TYPE_NAMES = { + java.lang.String.class.getName(), + java.math.BigInteger.class.getName(), + java.math.BigDecimal.class.getName(), + java.util.Date.class.getName(), + java.util.Calendar.class.getName(), + java.sql.Date.class.getName(), + java.sql.Time.class.getName(), + java.sql.Timestamp.class.getName(), + }; + } |