Search in sources :

Example 1 with SourceType

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

the class SourceTypeConverter method convert.

/*
	 * Convert an initializerinfo into a parsed initializer declaration
	 */
private Initializer convert(InitializerElementInfo initializerInfo, CompilationResult compilationResult) throws JavaModelException {
    Block block = new Block(0);
    Initializer initializer = new Initializer(block, ClassFileConstants.AccDefault);
    int start = initializerInfo.getDeclarationSourceStart();
    int end = initializerInfo.getDeclarationSourceEnd();
    initializer.sourceStart = initializer.declarationSourceStart = start;
    initializer.sourceEnd = initializer.declarationSourceEnd = end;
    initializer.modifiers = initializerInfo.getModifiers();
    /* convert local and anonymous types */
    IJavaElement[] children = initializerInfo.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;
            }
        }
        block.statements = statements;
    }
    return initializer;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer) Initializer(org.eclipse.jdt.internal.compiler.ast.Initializer) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) Block(org.eclipse.jdt.internal.compiler.ast.Block) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 2 with SourceType

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

the class SourceTypeConverter method convert.

/*
	 * Convert a set of source element types into a parsed compilation unit declaration
	 * The argument types are then all grouped in the same unit. The argument types must
	 * at least contain one type.
	 */
private CompilationUnitDeclaration convert(ISourceType[] sourceTypes, CompilationResult compilationResult) throws JavaModelException {
    this.unit = new CompilationUnitDeclaration(this.problemReporter, compilationResult, 0);
    if (sourceTypes.length == 0)
        return this.unit;
    SourceTypeElementInfo topLevelTypeInfo = (SourceTypeElementInfo) sourceTypes[0];
    org.eclipse.jdt.core.ICompilationUnit cuHandle = topLevelTypeInfo.getHandle().getCompilationUnit();
    this.cu = (ICompilationUnit) cuHandle;
    final CompilationUnitElementInfo compilationUnitElementInfo = (CompilationUnitElementInfo) ((JavaElement) this.cu).getElementInfo();
    if (this.has1_5Compliance && (compilationUnitElementInfo.annotationNumber >= CompilationUnitElementInfo.ANNOTATION_THRESHOLD_FOR_DIET_PARSE || (compilationUnitElementInfo.hasFunctionalTypes && (this.flags & LOCAL_TYPE) != 0))) {
        // Also see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=405843
        if ((this.flags & LOCAL_TYPE) == 0) {
            return new Parser(this.problemReporter, true).dietParse(this.cu, compilationResult);
        } else {
            return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
        }
    }
    /* only positions available */
    int start = topLevelTypeInfo.getNameSourceStart();
    int end = topLevelTypeInfo.getNameSourceEnd();
    /* convert package and imports */
    String[] packageName = ((PackageFragment) cuHandle.getParent()).names;
    if (packageName.length > 0)
        // if its null then it is defined in the default package
        this.unit.currentPackage = createImportReference(packageName, start, end, false, ClassFileConstants.AccDefault);
    IImportDeclaration[] importDeclarations = topLevelTypeInfo.getHandle().getCompilationUnit().getImports();
    int importCount = importDeclarations.length;
    this.unit.imports = new ImportReference[importCount];
    for (int i = 0; i < importCount; i++) {
        ImportDeclaration importDeclaration = (ImportDeclaration) importDeclarations[i];
        ISourceImport sourceImport = (ISourceImport) importDeclaration.getElementInfo();
        String nameWithoutStar = importDeclaration.getNameWithoutStar();
        this.unit.imports[i] = createImportReference(Util.splitOn('.', nameWithoutStar, 0, nameWithoutStar.length()), sourceImport.getDeclarationSourceStart(), sourceImport.getDeclarationSourceEnd(), importDeclaration.isOnDemand(), sourceImport.getModifiers());
    }
    /* convert type(s) */
    try {
        int typeCount = sourceTypes.length;
        final TypeDeclaration[] types = new TypeDeclaration[typeCount];
        /*
			 * We used a temporary types collection to prevent this.unit.types from being null during a call to
			 * convert(...) when the source is syntactically incorrect and the parser is flushing the unit's types.
			 * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=97466
			 */
        for (int i = 0; i < typeCount; i++) {
            SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) sourceTypes[i];
            types[i] = convert((SourceType) typeInfo.getHandle(), compilationResult);
        }
        this.unit.types = types;
        return this.unit;
    } catch (AnonymousMemberFound e) {
        return new Parser(this.problemReporter, true).parse(this.cu, compilationResult);
    }
}
Also used : PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) Parser(org.eclipse.jdt.internal.compiler.parser.Parser) ISourceImport(org.eclipse.jdt.internal.compiler.env.ISourceImport) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) SourceTypeElementInfo(org.eclipse.jdt.internal.core.SourceTypeElementInfo) CompilationUnitElementInfo(org.eclipse.jdt.internal.core.CompilationUnitElementInfo) ImportDeclaration(org.eclipse.jdt.internal.core.ImportDeclaration) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 3 with SourceType

use of org.eclipse.jdt.internal.core.SourceType in project jbosstools-hibernate by jbosstools.

the class CollectEntityInfo method visit.

public boolean visit(TypeDeclaration node) {
    ITypeBinding typeBinding = node.resolveBinding();
    String nodeName = typeBinding == null ? null : typeBinding.getBinaryName();
    if (fullyQualifiedName == null || !fullyQualifiedName.equalsIgnoreCase(nodeName)) {
        return false;
    }
    boolean isAbstruct = entityInfo.isAbstractFlag() || Modifier.isAbstract(node.getModifiers()) || node.isInterface();
    entityInfo.setAbstractFlag(isAbstruct);
    if (isAbstruct) {
        entityInfo.setAddEntityFlag(false);
        entityInfo.setAddMappedSuperclassFlag(true);
    }
    entityInfo.setInterfaceFlag(node.isInterface());
    Type superType = node.getSuperclassType();
    if (superType != null) {
        ITypeBinding tb = superType.resolveBinding();
        if (tb != null) {
            // $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);
            entityInfo.setFullyQualifiedParentName(entityFullyQualifiedName);
        }
    }
    List<?> superInterfaces = node.superInterfaceTypes();
    Iterator<?> it = superInterfaces.iterator();
    while (it.hasNext()) {
        Object obj = it.next();
        if (obj instanceof SimpleType) {
            // TODO process interfaces
            SimpleType st = (SimpleType) obj;
            String fullyQualifiedName = st.getName().getFullyQualifiedName();
            if (JPAConst.IMPORT_SERIALIZABLE.compareTo(fullyQualifiedName) == 0) {
                entityInfo.setAddSerializableInterfaceFlag(false);
            } else if (JPAConst.ANNOTATION_SERIALIZABLE.compareTo(fullyQualifiedName) == 0) {
                entityInfo.setAddSerializableInterfaceFlag(false);
                entityInfo.addRequiredImport(JPAConst.IMPORT_SERIALIZABLE);
            }
        }
    }
    node.resolveBinding();
    return true;
}
Also used : 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) JavaModelException(org.eclipse.jdt.core.JavaModelException) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) SourceType(org.eclipse.jdt.internal.core.SourceType)

Example 4 with SourceType

use of org.eclipse.jdt.internal.core.SourceType in project jbosstools-hibernate by jbosstools.

the class CompilationUnitCollector method processJavaElements.

/**
 * Process object - java element to collect all it's children CompilationUnits
 * @param obj
 * @param bCollect
 */
public void processJavaElements(Object obj, boolean bCollect) {
    if (obj instanceof ICompilationUnit) {
        ICompilationUnit cu = (ICompilationUnit) obj;
        addCompilationUnit(cu, bCollect);
    } else if (obj instanceof File) {
        File file = (File) obj;
        if (file.getProject() != null) {
            IJavaProject javaProject = JavaCore.create(file.getProject());
            ICompilationUnit[] cus = Utils.findCompilationUnits(javaProject, file.getFullPath());
            if (cus != null) {
                for (int i = 0; i < cus.length; i++) {
                    addCompilationUnit(cus[i], bCollect);
                }
            }
        }
    } else if (obj instanceof JavaProject) {
        JavaProject javaProject = (JavaProject) obj;
        IPackageFragmentRoot[] pfr = null;
        try {
            pfr = javaProject.getAllPackageFragmentRoots();
        } catch (JavaModelException e) {
        // just ignore it!
        // HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
        }
        if (pfr != null) {
            for (int i = 0; i < pfr.length; i++) {
                processJavaElements(pfr[i], bCollect);
            }
        }
    } else if (obj instanceof PackageFragment) {
        PackageFragment packageFragment = (PackageFragment) obj;
        ICompilationUnit[] cus = null;
        try {
            cus = packageFragment.getCompilationUnits();
        } catch (JavaModelException e) {
        // just ignore it!
        // HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
        }
        if (cus != null && cus.length > 0) {
            if (bCollect) {
                selection2UpdateList.add(obj);
                bCollect = false;
            }
            for (int i = 0; i < cus.length; i++) {
                addCompilationUnit(cus[i], bCollect);
            }
        }
    } else if (obj instanceof PackageFragmentRoot) {
        JavaElement javaElement = (JavaElement) obj;
        JavaElementInfo javaElementInfo = null;
        try {
            javaElementInfo = (JavaElementInfo) javaElement.getElementInfo();
        } catch (JavaModelException e) {
        // just ignore it!
        // HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
        }
        if (javaElementInfo != null) {
            IJavaElement[] je = javaElementInfo.getChildren();
            for (int i = 0; i < je.length; i++) {
                processJavaElements(je[i], true);
            }
        }
    } else if (obj instanceof JavaElement) {
        JavaElement javaElement = (JavaElement) obj;
        ICompilationUnit cu = javaElement.getCompilationUnit();
        addCompilationUnit(cu, bCollect);
    } else if (obj instanceof SourceType) {
        if (bCollect) {
            selection2UpdateList.add(obj);
            bCollect = false;
        }
        SourceType sourceType = (SourceType) obj;
        processJavaElements(sourceType.getJavaModel(), bCollect);
    } else {
    // ignore
    // System.out.println("1 Blah! " + selection); //$NON-NLS-1$
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) SourceType(org.eclipse.jdt.internal.core.SourceType) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaElement(org.eclipse.jdt.internal.core.JavaElement) JavaElementInfo(org.eclipse.jdt.internal.core.JavaElementInfo) IJavaProject(org.eclipse.jdt.core.IJavaProject) File(org.eclipse.core.internal.resources.File)

Example 5 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)

Aggregations

SourceType (org.eclipse.jdt.internal.core.SourceType)11 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 IJavaElement (org.eclipse.jdt.core.IJavaElement)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)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 IJavaProject (org.eclipse.jdt.core.IJavaProject)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 WildcardType (org.eclipse.jdt.core.dom.WildcardType)2 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)2 QualifiedAllocationExpression (org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression)2 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)2 BinaryType (org.eclipse.jdt.internal.core.BinaryType)2 PackageFragment (org.eclipse.jdt.internal.core.PackageFragment)2