Search in sources :

Example 6 with SourceType

use of org.eclipse.jdt.internal.core.SourceType in project evosuite by EvoSuite.

the class ExtendSuiteAction method addTestJob.

// @Override
// public void selectionChanged(IAction action, ISelection selection) {
// currentSelection.clear();
// 
// if (selection instanceof IStructuredSelection) {
// IStructuredSelection sel = (IStructuredSelection) selection;
// 
// for (Object o : sel.toList()) {
// if (o instanceof IJavaElement) {
// IJavaElement jEl = (IJavaElement) o;
// try {
// IResource jRes = jEl.getCorrespondingResource();
// if (jRes != null) {
// jRes.accept(new IResourceVisitor() {
// @Override
// public boolean visit(IResource resource)
// throws CoreException {
// if ("java".equals(resource.getFileExtension()))
// currentSelection.add(resource);
// return true;
// }
// });
// }
// } catch (JavaModelException e) {
// System.err.println("Error while traversing resources!" + e);
// } catch (CoreException e) {
// System.err.println("Error while traversing resources!" + e);
// }
// }
// }
// }
// }
// 
// @Override
// public void run(IAction action) {
// if (currentSelection.isEmpty()) {
// MessageDialog.openError(shell, "Evosuite",
// "Unable to generate test cases for selection: Cannot find .java files.");
// } else if (currentSelection.size() > 1) {
// MessageDialog.openError(shell, "Evosuite",
// "Please only select one class at a time.");
// } else {
// 
// for (IResource res : currentSelection) {
// IProject proj = res.getProject();
// fixJUnitClassPath(JavaCore.create(proj));
// generateTests(res);
// }
// }
// }
/**
 * Add a new test generation job to the job queue
 *
 * @param target
 */
@Override
protected void addTestJob(final IResource target) {
    IJavaElement element = JavaCore.create(target);
    IJavaElement packageElement = element.getParent();
    String packageName = packageElement.getElementName();
    final String suiteClass = (!packageName.equals("") ? packageName + "." : "") + target.getName().replace(".java", "").replace(File.separator, ".");
    System.out.println("Building new job for " + suiteClass);
    DetermineSUT det = new DetermineSUT();
    IJavaProject jProject = JavaCore.create(target.getProject());
    try {
        String classPath = target.getWorkspace().getRoot().findMember(jProject.getOutputLocation()).getLocation().toOSString();
        String SUT = det.getSUTName(suiteClass, classPath);
        // choose
        SelectionDialog typeDialog = JavaUI.createTypeDialog(shell, new ProgressMonitorDialog(shell), target.getProject(), IJavaElementSearchConstants.CONSIDER_CLASSES, false);
        Object[] sutDefault = new Object[1];
        sutDefault[0] = SUT;
        typeDialog.setInitialSelections(sutDefault);
        typeDialog.setTitle("Please select the class under test");
        typeDialog.open();
        // Type selected by the user
        Object[] result = typeDialog.getResult();
        if (result.length > 0) {
            SourceType sourceType = (SourceType) result[0];
            SUT = sourceType.getFullyQualifiedName();
        } else {
            return;
        }
        Job job = new TestExtensionJob(shell, target, SUT, suiteClass);
        job.setPriority(Job.SHORT);
        IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace().getRuleFactory();
        ISchedulingRule rule = ruleFactory.createRule(target.getProject());
        // IFolder folder = proj.getFolder(ResourceUtil.EVOSUITE_FILES);
        job.setRule(rule);
        job.setUser(true);
        // start as soon as possible
        job.schedule();
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (NoJUnitClassException e) {
        MessageDialog.openError(shell, "Evosuite", "Cannot find JUnit tests in " + suiteClass);
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) SourceType(org.eclipse.jdt.internal.core.SourceType) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IJavaProject(org.eclipse.jdt.core.IJavaProject) NoJUnitClassException(org.evosuite.junit.DetermineSUT.NoJUnitClassException) DetermineSUT(org.evosuite.junit.DetermineSUT) IResourceRuleFactory(org.eclipse.core.resources.IResourceRuleFactory) Job(org.eclipse.core.runtime.jobs.Job)

Example 7 with SourceType

use of org.eclipse.jdt.internal.core.SourceType in project che by eclipse.

the class SourceTypeConverter method convert.

/*
	 * Convert a method source element into a parsed method/constructor declaration
	 */
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException {
    AbstractMethodDeclaration method;
    /* only source positions available */
    int start = methodInfo.getNameSourceStart();
    int end = methodInfo.getNameSourceEnd();
    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed
		   on behalf of a 1.4 project we must internalize type variables properly in order to be able to
		   recognize usages of them in the method signature, to apply substitutions and thus to be able to
		   detect overriding in the presence of generics. If we simply drop them, when the method signature
		   refers to the type parameter, we won't know it should be bound to the type parameter and perform
		   incorrect lookup and may mistakenly end up with missing types
		 */
    TypeParameter[] typeParams = null;
    char[][] typeParameterNames = methodInfo.getTypeParameterNames();
    if (typeParameterNames != null) {
        int parameterCount = typeParameterNames.length;
        if (parameterCount > 0) {
            // method's type parameters must be null if no type parameter
            char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds();
            typeParams = new TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
            }
        }
    }
    int modifiers = methodInfo.getModifiers();
    if (methodInfo.isConstructor()) {
        ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
        decl.bits &= ~ASTNode.IsDefaultConstructor;
        method = decl;
        decl.typeParameters = typeParams;
    } else {
        MethodDeclaration decl;
        if (methodInfo.isAnnotationMethod()) {
            AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration(compilationResult);
            /* conversion of default value */
            SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo;
            boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1 || annotationMethodInfo.defaultValueEnd != -1;
            if ((this.flags & FIELD_INITIALIZATION) != 0) {
                if (hasDefaultValue) {
                    char[] defaultValueSource = CharOperation.subarray(getSource(), annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1);
                    if (defaultValueSource != null) {
                        Expression expression = parseMemberValue(defaultValueSource);
                        if (expression != null) {
                            annotationMethodDeclaration.defaultValue = expression;
                        }
                    } else {
                        // could not retrieve the default value
                        hasDefaultValue = false;
                    }
                }
            }
            if (hasDefaultValue)
                modifiers |= ClassFileConstants.AccAnnotationDefault;
            decl = annotationMethodDeclaration;
        } else {
            decl = new MethodDeclaration(compilationResult);
        }
        // convert return type
        decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end);
        // type parameters
        decl.typeParameters = typeParams;
        method = decl;
    }
    method.selector = methodHandle.getElementName().toCharArray();
    boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0;
    method.modifiers = modifiers & ~ClassFileConstants.AccVarargs;
    method.sourceStart = start;
    method.sourceEnd = end;
    method.declarationSourceStart = methodInfo.getDeclarationSourceStart();
    method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd();
    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        method.annotations = convertAnnotations(methodHandle);
    }
    /* convert arguments */
    String[] argumentTypeSignatures = methodHandle.getParameterTypes();
    char[][] argumentNames = methodInfo.getArgumentNames();
    int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length;
    if (argumentCount > 0) {
        ILocalVariable[] parameters = methodHandle.getParameters();
        long position = ((long) start << 32) + end;
        method.arguments = new Argument[argumentCount];
        for (int i = 0; i < argumentCount; i++) {
            TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end);
            if (isVarargs && i == argumentCount - 1) {
                typeReference.bits |= ASTNode.IsVarArgs;
            }
            method.arguments[i] = new Argument(argumentNames[i], position, typeReference, ClassFileConstants.AccDefault);
            // convert 1.5 specific constructs only if compliance is 1.5 or above
            if (this.has1_5Compliance) {
                /* convert annotations */
                method.arguments[i].annotations = convertAnnotations(parameters[i]);
            }
        }
    }
    /* convert thrown exceptions */
    char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames();
    int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
    if (exceptionCount > 0) {
        method.thrownExceptions = new TypeReference[exceptionCount];
        for (int i = 0; i < exceptionCount; i++) {
            method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end);
        }
    }
    /* convert local and anonymous types */
    if ((this.flags & LOCAL_TYPE) != 0) {
        IJavaElement[] children = methodInfo.getChildren();
        int typesLength = children.length;
        if (typesLength != 0) {
            Statement[] statements = new Statement[typesLength];
            for (int i = 0; i < typesLength; i++) {
                SourceType type = (SourceType) children[i];
                TypeDeclaration localType = convert(type, compilationResult);
                if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
                    QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
                    expression.type = localType.superclass;
                    localType.superclass = null;
                    localType.superInterfaces = null;
                    localType.allocation = expression;
                    statements[i] = expression;
                } else {
                    statements[i] = localType;
                }
            }
            method.statements = statements;
        }
    }
    return method;
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) IJavaElement(org.eclipse.jdt.core.IJavaElement) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) SourceAnnotationMethodInfo(org.eclipse.jdt.internal.core.SourceAnnotationMethodInfo) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 8 with SourceType

use of org.eclipse.jdt.internal.core.SourceType in project che by eclipse.

the class SourceTypeConverter method convert.

/*
	 * Convert a source element type into a parsed type declaration
	 */
private TypeDeclaration convert(SourceType typeHandle, CompilationResult compilationResult) throws JavaModelException {
    SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) typeHandle.getElementInfo();
    if (typeInfo.isAnonymousMember())
        throw new AnonymousMemberFound();
    /* create type declaration - can be member type */
    TypeDeclaration type = new TypeDeclaration(compilationResult);
    if (typeInfo.getEnclosingType() == null) {
        if (typeHandle.isAnonymous()) {
            type.name = CharOperation.NO_CHAR;
            type.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
        } else {
            if (typeHandle.isLocal()) {
                type.bits |= ASTNode.IsLocalType;
            }
        }
    } else {
        type.bits |= ASTNode.IsMemberType;
    }
    if ((type.bits & ASTNode.IsAnonymousType) == 0) {
        type.name = typeInfo.getName();
    }
    type.name = typeInfo.getName();
    // only positions available
    int start, end;
    type.sourceStart = start = typeInfo.getNameSourceStart();
    type.sourceEnd = end = typeInfo.getNameSourceEnd();
    type.modifiers = typeInfo.getModifiers();
    type.declarationSourceStart = typeInfo.getDeclarationSourceStart();
    type.declarationSourceEnd = typeInfo.getDeclarationSourceEnd();
    type.bodyEnd = type.declarationSourceEnd;
    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        type.annotations = convertAnnotations(typeHandle);
    }
    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, even in a 1.4 project, we
		   must internalize type variables and observe any parameterization of super class
		   and/or super interfaces in order to be able to detect overriding in the presence
		   of generics.
		 */
    char[][] typeParameterNames = typeInfo.getTypeParameterNames();
    if (typeParameterNames.length > 0) {
        int parameterCount = typeParameterNames.length;
        char[][][] typeParameterBounds = typeInfo.getTypeParameterBounds();
        type.typeParameters = new TypeParameter[parameterCount];
        for (int i = 0; i < parameterCount; i++) {
            type.typeParameters[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
        }
    }
    /* set superclass and superinterfaces */
    if (typeInfo.getSuperclassName() != null) {
        type.superclass = createTypeReference(typeInfo.getSuperclassName(), start, end, true);
        type.superclass.bits |= ASTNode.IsSuperType;
    }
    char[][] interfaceNames = typeInfo.getInterfaceNames();
    int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length;
    if (interfaceCount > 0) {
        type.superInterfaces = new TypeReference[interfaceCount];
        for (int i = 0; i < interfaceCount; i++) {
            type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end, true);
            type.superInterfaces[i].bits |= ASTNode.IsSuperType;
        }
    }
    /* convert member types */
    if ((this.flags & MEMBER_TYPE) != 0) {
        SourceType[] sourceMemberTypes = typeInfo.getMemberTypeHandles();
        int sourceMemberTypeCount = sourceMemberTypes.length;
        type.memberTypes = new TypeDeclaration[sourceMemberTypeCount];
        for (int i = 0; i < sourceMemberTypeCount; i++) {
            type.memberTypes[i] = convert(sourceMemberTypes[i], compilationResult);
            type.memberTypes[i].enclosingType = type;
        }
    }
    /* convert intializers and fields*/
    InitializerElementInfo[] initializers = null;
    int initializerCount = 0;
    if ((this.flags & LOCAL_TYPE) != 0) {
        initializers = typeInfo.getInitializers();
        initializerCount = initializers.length;
    }
    SourceField[] sourceFields = null;
    int sourceFieldCount = 0;
    if ((this.flags & FIELD) != 0) {
        sourceFields = typeInfo.getFieldHandles();
        sourceFieldCount = sourceFields.length;
    }
    int length = initializerCount + sourceFieldCount;
    if (length > 0) {
        type.fields = new FieldDeclaration[length];
        for (int i = 0; i < initializerCount; i++) {
            type.fields[i] = convert(initializers[i], compilationResult);
        }
        int index = 0;
        for (int i = initializerCount; i < length; i++) {
            type.fields[i] = convert(sourceFields[index++], type, compilationResult);
        }
    }
    /* convert methods - need to add default constructor if necessary */
    boolean needConstructor = (this.flags & CONSTRUCTOR) != 0;
    boolean needMethod = (this.flags & METHOD) != 0;
    if (needConstructor || needMethod) {
        SourceMethod[] sourceMethods = typeInfo.getMethodHandles();
        int sourceMethodCount = sourceMethods.length;
        /* source type has a constructor ?           */
        /* by default, we assume that one is needed. */
        int extraConstructor = 0;
        int methodCount = 0;
        int kind = TypeDeclaration.kind(type.modifiers);
        boolean isAbstract = kind == TypeDeclaration.INTERFACE_DECL || kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
        if (!isAbstract) {
            extraConstructor = needConstructor ? 1 : 0;
            for (int i = 0; i < sourceMethodCount; i++) {
                if (sourceMethods[i].isConstructor()) {
                    if (needConstructor) {
                        // Does not need the extra constructor since one constructor already exists.
                        extraConstructor = 0;
                        methodCount++;
                    }
                } else if (needMethod) {
                    methodCount++;
                }
            }
        } else {
            methodCount = needMethod ? sourceMethodCount : 0;
        }
        type.methods = new AbstractMethodDeclaration[methodCount + extraConstructor];
        if (extraConstructor != 0) {
            // add default constructor in first position
            type.methods[0] = type.createDefaultConstructor(false, false);
        }
        int index = 0;
        boolean hasAbstractMethods = false;
        for (int i = 0; i < sourceMethodCount; i++) {
            SourceMethod sourceMethod = sourceMethods[i];
            SourceMethodElementInfo methodInfo = (SourceMethodElementInfo) sourceMethod.getElementInfo();
            boolean isConstructor = methodInfo.isConstructor();
            if ((methodInfo.getModifiers() & ClassFileConstants.AccAbstract) != 0) {
                hasAbstractMethods = true;
            }
            if ((isConstructor && needConstructor) || (!isConstructor && needMethod)) {
                AbstractMethodDeclaration method = convert(sourceMethod, methodInfo, compilationResult);
                if (isAbstract || method.isAbstract()) {
                    // fix-up flag
                    method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
                }
                type.methods[extraConstructor + index++] = method;
            }
        }
        if (hasAbstractMethods)
            type.bits |= ASTNode.HasAbstractMethods;
    }
    return type;
}
Also used : SourceMethodElementInfo(org.eclipse.jdt.internal.core.SourceMethodElementInfo) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) SourceField(org.eclipse.jdt.internal.core.SourceField) InitializerElementInfo(org.eclipse.jdt.internal.core.InitializerElementInfo) SourceTypeElementInfo(org.eclipse.jdt.internal.core.SourceTypeElementInfo) SourceMethod(org.eclipse.jdt.internal.core.SourceMethod) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Example 9 with SourceType

use of org.eclipse.jdt.internal.core.SourceType in project abstools by abstools.

the class SourceUtils method getMethodsFromJavaFile.

public static IMethod[] getMethodsFromJavaFile(IJavaElement element) throws Exception {
    try {
        ICompilationUnit unit = (ICompilationUnit) element;
        IJavaElement[] elements = unit.getChildren();
        for (int i = 0; i < elements.length; i++) {
            // LogUtils.debug(elements[i].getClass());
            if (elements[i] instanceof org.eclipse.jdt.internal.core.SourceType) {
                SourceType source = (SourceType) elements[i];
                return source.getMethods();
            }
        }
        throw new Exception("No methods found in file ");
    } catch (Exception e) {
        throw new Exception("Cannot find the methods from Java file");
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) SourceType(org.eclipse.jdt.internal.core.SourceType) CostabsException(costabs.exceptions.CostabsException) JavaModelException(org.eclipse.jdt.core.JavaModelException)

Example 10 with SourceType

use of org.eclipse.jdt.internal.core.SourceType 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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) BinaryType(org.eclipse.jdt.internal.core.BinaryType) SourceType(org.eclipse.jdt.internal.core.SourceType) FieldGetterType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo.FieldGetterType) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) BinaryType(org.eclipse.jdt.internal.core.BinaryType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) RefType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.RefType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) FieldGetterType(org.hibernate.eclipse.jdt.ui.internal.jpa.common.EntityInfo.FieldGetterType) SourceType(org.eclipse.jdt.internal.core.SourceType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Iterator(java.util.Iterator) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Aggregations

SourceType (org.eclipse.jdt.internal.core.SourceType)15 JavaModelException (org.eclipse.jdt.core.JavaModelException)9 IJavaElement (org.eclipse.jdt.core.IJavaElement)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IType (org.eclipse.jdt.core.IType)4 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)4 ISourceType (org.eclipse.jdt.internal.compiler.env.ISourceType)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)3 Type (org.eclipse.jdt.core.dom.Type)3 IResourceRuleFactory (org.eclipse.core.resources.IResourceRuleFactory)2 CoreException (org.eclipse.core.runtime.CoreException)2 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)2 Job (org.eclipse.core.runtime.jobs.Job)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)2 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)2 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)2 SimpleType (org.eclipse.jdt.core.dom.SimpleType)2