use of org.eclipse.jdt.core.dom.ArrayType in project evosuite by EvoSuite.
the class TestExtractingVisitor method retrieveTypeClass.
/**
* <p>
* retrieveTypeClass
* </p>
*
* @param argument
* a {@link java.lang.Object} object.
* @return a {@link java.lang.Class} object.
*/
protected Class<?> retrieveTypeClass(Object argument) {
assert argument != null;
if (argument instanceof SimpleType) {
SimpleType simpleType = (SimpleType) argument;
return retrieveTypeClass(simpleType);
}
if (argument instanceof ITypeBinding) {
ITypeBinding binding = (ITypeBinding) argument;
return retrieveTypeClass(binding);
}
if (argument instanceof IVariableBinding) {
IVariableBinding variableBinding = (IVariableBinding) argument;
return retrieveTypeClass(variableBinding.getType());
}
if (argument instanceof SimpleName) {
SimpleName simpleName = (SimpleName) argument;
return retrieveTypeClass(simpleName.resolveBinding());
}
if (argument instanceof StringLiteral) {
return String.class;
}
if (argument instanceof NumberLiteral) {
return retrieveTypeClass((NumberLiteral) argument);
}
if (argument instanceof PrimitiveType) {
PrimitiveType primitiveType = (PrimitiveType) argument;
String typeCode = primitiveType.getPrimitiveTypeCode().toString();
Class<?> result = PRIMITIVE_TYPECODE_MAPPING.get(typeCode);
assert result != null : "Could not resolve typecode " + typeCode + ".";
return result;
}
if (argument instanceof ArrayType) {
ArrayType arrayType = (ArrayType) argument;
return retrieveTypeClass(arrayType);
}
if (argument instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) argument;
return retrieveTypeClass(parameterizedType.getType());
}
if (argument instanceof VariableDeclarationFragment) {
VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) argument;
return retrieveTypeClass(varDeclFrgmnt.resolveBinding());
}
if (argument instanceof InfixExpression) {
InfixExpression infixExpr = (InfixExpression) argument;
ITypeBinding refTypeBinding = infixExpr.resolveTypeBinding();
if (refTypeBinding != null) {
return retrieveTypeClass(refTypeBinding);
} else {
throw new RuntimeException("Could not determine type class of infix expression '" + infixExpr + "'.");
}
}
if (argument instanceof MethodInvocation) {
MethodInvocation methodInvocation = (MethodInvocation) argument;
ITypeBinding typeBinding = methodInvocation.resolveTypeBinding();
if (typeBinding != null) {
return retrieveTypeClass(typeBinding);
}
Expression typeExpression = methodInvocation.getExpression();
if (typeExpression instanceof MethodInvocation) {
MethodInvocation parentMethodInvocation = (MethodInvocation) typeExpression;
IMethodBinding parentMethodBinding = parentMethodInvocation.resolveMethodBinding();
return retrieveTypeClass(parentMethodBinding.getDeclaringClass());
} else {
return retrieveTypeClass(typeExpression);
}
}
if (argument instanceof ArrayAccess) {
ArrayAccess arrayAccess = (ArrayAccess) argument;
return retrieveTypeClass(arrayAccess.getArray());
}
if (argument instanceof Class<?>) {
return (Class<?>) argument;
}
if (argument instanceof ClassInstanceCreation) {
return retrieveTypeClass(((ClassInstanceCreation) argument).resolveTypeBinding());
}
if (argument instanceof BooleanLiteral) {
return Boolean.TYPE;
}
throw new UnsupportedOperationException("Retrieval of type " + argument.getClass() + " not implemented yet!");
}
use of org.eclipse.jdt.core.dom.ArrayType in project jbosstools-hibernate by jbosstools.
the class ProcessEntityInfo method isSimilarType.
// simple type name check
public boolean isSimilarType(Type type, String fullyQualifiedName) {
String typeName = null;
if (type.isSimpleType()) {
SimpleType st = (SimpleType) type;
typeName = st.getName().getFullyQualifiedName();
} else if (type.isArrayType()) {
ArrayType at = (ArrayType) type;
Type elementType = at.getElementType();
if (elementType.isSimpleType()) {
SimpleType st = (SimpleType) elementType;
typeName = st.getName().getFullyQualifiedName();
}
}
if (typeName != null && fullyQualifiedName.indexOf(typeName) == -1) {
return false;
}
return true;
}
use of org.eclipse.jdt.core.dom.ArrayType in project jbosstools-hibernate by jbosstools.
the class TypeVisitor method visit.
@Override
public boolean visit(ArrayType type) {
IValue array = null;
Type elementType = type.getElementType();
ITypeBinding tb = elementType.resolveBinding();
// Unresolved binding. Omit the property.
if (tb == null)
return false;
if (tb.isPrimitive()) {
array = service.newPrimitiveArray(rootClass);
IValue value = buildSimpleValue(tb.getName());
value.setTable(rootClass.getTable());
array.setElement(value);
// TODO what to set?
array.setCollectionTable(rootClass.getTable());
} else {
IPersistentClass associatedClass = rootClasses.get(tb.getBinaryName());
array = service.newArray(rootClass);
array.setElementClassName(tb.getBinaryName());
array.setCollectionTable(associatedClass.getTable());
IValue oValue = service.newOneToMany(rootClass);
oValue.setAssociatedClass(associatedClass);
oValue.setReferencedEntityName(tb.getBinaryName());
array.setElement(oValue);
}
IValue key = service.newSimpleValue();
if (StringHelper.isNotEmpty(entityInfo.getPrimaryIdName())) {
key.addColumn(service.newColumn(entityInfo.getPrimaryIdName().toUpperCase()));
}
array.setKey(key);
array.setFetchModeJoin();
IValue index = service.newSimpleValue();
// add default index
// index.addColumn(new Column(varName.toUpperCase()+"_POSITION"));
array.setIndex(index);
buildProperty(array);
// $NON-NLS-1$
prop.setCascade("none");
// do not visit children
return false;
}
use of org.eclipse.jdt.core.dom.ArrayType in project jbosstools-hibernate by jbosstools.
the class CollectEntityInfo method processFieldOrGetter.
public boolean processFieldOrGetter(Type type, List<String> list, boolean fieldFlag) {
if (type == null) {
return false;
}
if (type.isPrimitiveType()) {
PrimitiveType pt = (PrimitiveType) type;
if (!pt.getPrimitiveTypeCode().equals(PrimitiveType.BOOLEAN)) {
// this is candidate for primary id
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
if ("version".equalsIgnoreCase(name)) {
// $NON-NLS-1$
FieldGetterType versionFieldGetter = updateFieldGetter(entityInfo.getVersionFieldGetter(), fieldFlag);
entityInfo.setVersionFieldGetter(versionFieldGetter);
} else {
entityInfo.addPrimaryIdCandidate(name);
}
}
}
} else if (type.isSimpleType()) {
SimpleType st = (SimpleType) type;
ITypeBinding tb = st.resolveBinding();
if (tb != null) {
// $NON-NLS-1$
String entityFullyQualifiedName = "";
if (tb.getJavaElement() instanceof SourceType) {
SourceType sourceT = (SourceType) tb.getJavaElement();
entityFullyQualifiedName = sourceT.getFullyQualifiedName();
entityInfo.addDependency(entityFullyQualifiedName);
RefType refType2Use = tb.isEnum() ? RefType.ENUMERATED : RefType.MANY2ONE;
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
entityInfo.addReference(name, entityFullyQualifiedName, refType2Use);
}
} else if (tb.getJavaElement() instanceof BinaryType) {
ITypeBinding tbParent = tb.getTypeDeclaration().getSuperclass();
if (tbParent != null) {
if ("java.lang.Number".equals(tbParent.getBinaryName())) {
// $NON-NLS-1$
// this is candidate for primary id
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
if ("version".equalsIgnoreCase(name)) {
// $NON-NLS-1$
FieldGetterType versionFieldGetter = updateFieldGetter(entityInfo.getVersionFieldGetter(), fieldFlag);
entityInfo.setVersionFieldGetter(versionFieldGetter);
} else {
entityInfo.addPrimaryIdCandidate(name);
}
}
} else if ("java.util.Date".equals(tbParent.getBinaryName())) {
// $NON-NLS-1$
// this is candidate for version
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
if ("version".equalsIgnoreCase(name)) {
// $NON-NLS-1$
FieldGetterType versionFieldGetter = updateFieldGetter(entityInfo.getVersionFieldGetter(), fieldFlag);
entityInfo.setVersionFieldGetter(versionFieldGetter);
}
}
}
}
if ("java.lang.String".equals(tb.getBinaryName())) {
// $NON-NLS-1$
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
entityInfo.updateAnnotationColumn(name, null, false);
entityInfo.addPrimaryIdCandidate(name);
}
}
}
}
} else if (type.isArrayType()) {
ArrayType at = (ArrayType) type;
Type elementType = at.getElementType();
ITypeBinding tb = elementType.resolveBinding();
if (tb != null) {
if (tb.getJavaElement() instanceof SourceType) {
// $NON-NLS-1$
String entityFullyQualifiedName = "";
SourceType sourceT = (SourceType) tb.getJavaElement();
try {
entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
} catch (JavaModelException e) {
// $NON-NLS-1$
HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e);
}
entityInfo.addDependency(entityFullyQualifiedName);
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
entityInfo.addReference(name, entityFullyQualifiedName, RefType.ONE2MANY);
}
}
}
} else if (type.isParameterizedType()) {
ParameterizedType pt = (ParameterizedType) type;
Type typeP = pt.getType();
ITypeBinding tb = typeP.resolveBinding();
if (tb != null) {
ITypeBinding[] interfaces = Utils.getAllInterfaces(tb);
// $NON-NLS-1$
String fullyQualifiedNameTypeName = "";
if (Utils.isImplementInterface(interfaces, "java.util.Collection")) {
// $NON-NLS-1$
// $NON-NLS-1$
fullyQualifiedNameTypeName = "java.util.Collection";
}
if (Utils.isImplementInterface(interfaces, "java.util.Map")) {
// $NON-NLS-1$
// $NON-NLS-1$
fullyQualifiedNameTypeName = "java.util.Map";
}
/*for (int i = 0; i < interfaces.length; i++) {
if (interfaces[i].getJavaElement() instanceof BinaryType) {
BinaryType binaryT = (BinaryType)interfaces[i].getJavaElement();
String tmp = binaryT.getFullyQualifiedName('.');
if (0 == "java.util.Collection".compareTo(tmp)) { //$NON-NLS-1$
fullyQualifiedNameTypeName = tmp;
break;
}
}
}*/
if (fullyQualifiedNameTypeName.length() > 0) {
Iterator<Type> typeArgsIt = pt.typeArguments().iterator();
while (typeArgsIt.hasNext()) {
typeP = typeArgsIt.next();
tb = typeP.resolveBinding();
// $NON-NLS-1$
String entityFullyQualifiedName = "";
if (tb.getJavaElement() instanceof SourceType) {
SourceType sourceT = (SourceType) tb.getJavaElement();
try {
entityFullyQualifiedName = sourceT.getFullyQualifiedParameterizedName();
} catch (JavaModelException e) {
// $NON-NLS-1$
HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e);
}
entityInfo.addDependency(entityFullyQualifiedName);
Iterator<String> itVarNames = list.iterator();
while (itVarNames.hasNext()) {
String name = itVarNames.next();
entityInfo.addReference(name, entityFullyQualifiedName, RefType.ONE2MANY);
}
}
}
}
}
} else if (type.isQualifiedType()) {
QualifiedType qt = (QualifiedType) type;
@SuppressWarnings("unused") ITypeBinding tb = qt.resolveBinding();
} else if (type.isWildcardType()) {
WildcardType wt = (WildcardType) type;
@SuppressWarnings("unused") ITypeBinding tb = wt.resolveBinding();
}
return true;
}
Aggregations