Search in sources :

Example 1 with PackageFragment

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

the class Util method isExcluded.

/*
 * Returns whether the given java element is exluded from its root's classpath.
 * It doesn't check whether the root itself is on the classpath or not
 */
public static final boolean isExcluded(IJavaElement element) {
    int elementType = element.getElementType();
    switch(elementType) {
        case IJavaElement.JAVA_MODEL:
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return false;
        case IJavaElement.PACKAGE_FRAGMENT:
            PackageFragmentRoot root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            IResource resource = ((PackageFragment) element).resource();
            return resource != null && isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars());
        case IJavaElement.COMPILATION_UNIT:
            root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            resource = element.getResource();
            if (resource == null)
                return false;
            if (isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars()))
                return true;
            return isExcluded(element.getParent());
        default:
            IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
            return cu != null && isExcluded(cu);
    }
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) IJavaElement(org.eclipse.jdt.core.IJavaElement) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IResource(org.eclipse.core.resources.IResource)

Example 2 with PackageFragment

use of org.eclipse.jdt.internal.core.PackageFragment 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 PackageFragment

use of org.eclipse.jdt.internal.core.PackageFragment 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 4 with PackageFragment

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

the class SourceMapper method getType.

/**
     * Returns the type with the given <code>typeName</code>.  Returns inner classes
     * as well.
     */
protected IType getType(String typeName) {
    if (typeName.length() == 0) {
        IJavaElement classFile = this.binaryType.getParent();
        String classFileName = classFile.getElementName();
        StringBuffer newClassFileName = new StringBuffer();
        int lastDollar = classFileName.lastIndexOf('$');
        for (int i = 0; i <= lastDollar; i++) newClassFileName.append(classFileName.charAt(i));
        newClassFileName.append(Integer.toString(this.anonymousCounter));
        PackageFragment pkg = (PackageFragment) classFile.getParent();
        return new BinaryType(new ClassFile(pkg, newClassFileName.toString()), typeName);
    } else if (this.binaryType.getElementName().equals(typeName))
        return this.binaryType;
    else
        return this.binaryType.getType(typeName);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) BinaryType(org.eclipse.jdt.internal.core.BinaryType) IBinaryType(org.eclipse.jdt.internal.compiler.env.IBinaryType) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.jdt.internal.core.ClassFile)

Example 5 with PackageFragment

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

the class SourceMapper method findSource.

/**
     * Locates and returns source code for the given (binary) type, in this
     * SourceMapper's ZIP file, or returns <code>null</code> if source
     * code cannot be found.
     * The given simpleSourceFileName is the .java file name (without the enclosing
     * folder) used to create the given type (e.g. "A.java" for x/y/A$Inner.class)
     */
public char[] findSource(IType type, String simpleSourceFileName) {
    long time = 0;
    if (VERBOSE) {
        time = System.currentTimeMillis();
    }
    PackageFragment pkgFrag = (PackageFragment) type.getPackageFragment();
    String name = org.eclipse.jdt.internal.core.util.Util.concatWith(pkgFrag.names, simpleSourceFileName, '/');
    char[] source = null;
    org.eclipse.jdt.internal.core.JavaModelManager manager = org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager();
    try {
        // Cache any zip files we open during this operation
        manager.cacheZipFiles(this);
        if (this.rootPath != null) {
            source = getSourceForRootPath(this.rootPath, name);
        }
        if (source == null) {
            computeAllRootPaths(type);
            if (this.rootPaths != null) {
                loop: for (Iterator iterator = this.rootPaths.iterator(); iterator.hasNext(); ) {
                    String currentRootPath = (String) iterator.next();
                    if (!currentRootPath.equals(this.rootPath)) {
                        source = getSourceForRootPath(currentRootPath, name);
                        if (source != null) {
                            // remember right root path
                            this.rootPath = currentRootPath;
                            break loop;
                        }
                    }
                }
            }
        }
    } finally {
        // clean up cached zip files.
        manager.flushZipFiles(this);
    }
    if (VERBOSE) {
        System.out.println(//$NON-NLS-1$ //$NON-NLS-2$
        "spent " + (System.currentTimeMillis() - time) + "ms for " + type.getElementName());
    }
    return source;
}
Also used : PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) Iterator(java.util.Iterator)

Aggregations

PackageFragment (org.eclipse.jdt.internal.core.PackageFragment)8 IJavaElement (org.eclipse.jdt.core.IJavaElement)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)3 JavaModelException (org.eclipse.jdt.core.JavaModelException)3 JavaElement (org.eclipse.jdt.internal.core.JavaElement)3 PackageFragmentRoot (org.eclipse.jdt.internal.core.PackageFragmentRoot)3 IClassFile (org.eclipse.jdt.core.IClassFile)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)2 IBinaryType (org.eclipse.jdt.internal.compiler.env.IBinaryType)2 BinaryType (org.eclipse.jdt.internal.core.BinaryType)2 JavaElementInfo (org.eclipse.jdt.internal.core.JavaElementInfo)2 JavaProject (org.eclipse.jdt.internal.core.JavaProject)2 SourceType (org.eclipse.jdt.internal.core.SourceType)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1