Search in sources :

Example 1 with BinaryType

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

the class CheASTParser method internalCreateAST.

private ASTNode internalCreateAST(IProgressMonitor monitor) {
    boolean needToResolveBindings = (this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0;
    switch(this.astKind) {
        case K_CLASS_BODY_DECLARATIONS:
        case K_EXPRESSION:
        case K_STATEMENTS:
            if (this.rawSource == null) {
                if (this.typeRoot != null) {
                    // get the source from the type root
                    if (this.typeRoot instanceof ICompilationUnit) {
                        org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.typeRoot;
                        this.rawSource = sourceUnit.getContents();
                    } else if (this.typeRoot instanceof IClassFile) {
                        try {
                            String sourceString = this.typeRoot.getSource();
                            if (sourceString != null) {
                                this.rawSource = sourceString.toCharArray();
                            }
                        } catch (JavaModelException e) {
                            // an error occured accessing the java element
                            StringWriter stringWriter = new StringWriter();
                            PrintWriter writer = null;
                            try {
                                writer = new PrintWriter(stringWriter);
                                e.printStackTrace(writer);
                            } finally {
                                if (writer != null)
                                    writer.close();
                            }
                            throw new IllegalStateException(String.valueOf(stringWriter.getBuffer()));
                        }
                    }
                }
            }
            if (this.rawSource != null) {
                if (this.sourceOffset + this.sourceLength > this.rawSource.length) {
                    throw new IllegalStateException();
                }
                return internalCreateASTForKind();
            }
            break;
        case K_COMPILATION_UNIT:
            CompilationUnitDeclaration compilationUnitDeclaration = null;
            try {
                NodeSearcher searcher = null;
                org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = null;
                WorkingCopyOwner wcOwner = this.workingCopyOwner;
                if (this.typeRoot instanceof ICompilationUnit) {
                    /*
							 * this.compilationUnitSource is an instance of org.eclipse.jdt.internal.core.CompilationUnit that implements
							 * both org.eclipse.jdt.core.ICompilationUnit and org.eclipse.jdt.internal.compiler.env.ICompilationUnit
							 */
                    sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.typeRoot;
                    /*
							 * use a BasicCompilation that caches the source instead of using the compilationUnitSource directly
							 * (if it is a working copy, the source can change between the parse and the AST convertion)
							 * (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632)
							 */
                    sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project);
                    wcOwner = ((ICompilationUnit) this.typeRoot).getOwner();
                } else if (this.typeRoot instanceof IClassFile) {
                    try {
                        String sourceString = this.typeRoot.getSource();
                        if (sourceString == null) {
                            throw new IllegalStateException();
                        }
                        PackageFragment packageFragment = (PackageFragment) this.typeRoot.getParent();
                        BinaryType type = (BinaryType) this.typeRoot.findPrimaryType();
                        IBinaryType binaryType = (IBinaryType) type.getElementInfo();
                        // file name is used to recreate the Java element, so it has to be the toplevel .class file name
                        char[] fileName = binaryType.getFileName();
                        int firstDollar = CharOperation.indexOf('$', fileName);
                        if (firstDollar != -1) {
                            char[] suffix = SuffixConstants.SUFFIX_class;
                            int suffixLength = suffix.length;
                            char[] newFileName = new char[firstDollar + suffixLength];
                            System.arraycopy(fileName, 0, newFileName, 0, firstDollar);
                            System.arraycopy(suffix, 0, newFileName, firstDollar, suffixLength);
                            fileName = newFileName;
                        }
                        sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project);
                    } catch (JavaModelException e) {
                        // an error occured accessing the java element
                        StringWriter stringWriter = new StringWriter();
                        PrintWriter writer = null;
                        try {
                            writer = new PrintWriter(stringWriter);
                            e.printStackTrace(writer);
                        } finally {
                            if (writer != null)
                                writer.close();
                        }
                        throw new IllegalStateException(String.valueOf(stringWriter.getBuffer()));
                    }
                } else if (this.rawSource != null) {
                    needToResolveBindings = ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0) && this.unitName != null && (this.project != null || this.classpaths != null || this.sourcepaths != null || ((this.bits & CompilationUnitResolver.INCLUDE_RUNNING_VM_BOOTCLASSPATH) != 0)) && this.compilerOptions != null;
                    //$NON-NLS-1$
                    sourceUnit = new BasicCompilationUnit(this.rawSource, null, this.unitName == null ? "" : this.unitName, this.project);
                } else {
                    throw new IllegalStateException();
                }
                if ((this.bits & CompilationUnitResolver.PARTIAL) != 0) {
                    searcher = new NodeSearcher(this.focalPointPosition);
                }
                int flags = 0;
                if ((this.bits & CompilationUnitResolver.STATEMENT_RECOVERY) != 0) {
                    flags |= ICompilationUnit.ENABLE_STATEMENTS_RECOVERY;
                }
                if (searcher == null && ((this.bits & CompilationUnitResolver.IGNORE_METHOD_BODIES) != 0)) {
                    flags |= ICompilationUnit.IGNORE_METHOD_BODIES;
                }
                if (needToResolveBindings) {
                    if ((this.bits & CompilationUnitResolver.BINDING_RECOVERY) != 0) {
                        flags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
                    }
                    try {
                        // parse and resolve
                        compilationUnitDeclaration = CheCompilationUnitResolver.resolve(sourceUnit, this.project, getClasspath(), searcher, this.compilerOptions, this.workingCopyOwner, flags, monitor);
                    } catch (JavaModelException e) {
                        flags &= ~ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
                        compilationUnitDeclaration = CompilationUnitResolver.parse(sourceUnit, searcher, this.compilerOptions, flags);
                        needToResolveBindings = false;
                    }
                } else {
                    compilationUnitDeclaration = CompilationUnitResolver.parse(sourceUnit, searcher, this.compilerOptions, flags);
                    needToResolveBindings = false;
                }
                CompilationUnit result = CompilationUnitResolver.convert(compilationUnitDeclaration, sourceUnit.getContents(), this.apiLevel, this.compilerOptions, needToResolveBindings, wcOwner, needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, flags, monitor, this.project != null);
                result.setTypeRoot(this.typeRoot);
                return result;
            } finally {
                if (compilationUnitDeclaration != null && ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0)) {
                    compilationUnitDeclaration.cleanUp();
                }
            }
    }
    throw new IllegalStateException();
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) IBinaryType(org.eclipse.jdt.internal.compiler.env.IBinaryType) StringWriter(java.io.StringWriter) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) WorkingCopyOwner(org.eclipse.jdt.core.WorkingCopyOwner) DefaultWorkingCopyOwner(org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner) PrintWriter(java.io.PrintWriter) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) BasicCompilationUnit(org.eclipse.jdt.internal.core.BasicCompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) BinaryType(org.eclipse.jdt.internal.core.BinaryType) IBinaryType(org.eclipse.jdt.internal.compiler.env.IBinaryType) BasicCompilationUnit(org.eclipse.jdt.internal.core.BasicCompilationUnit)

Example 2 with BinaryType

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

Aggregations

IClassFile (org.eclipse.jdt.core.IClassFile)2 IBinaryType (org.eclipse.jdt.internal.compiler.env.IBinaryType)2 BinaryType (org.eclipse.jdt.internal.core.BinaryType)2 PackageFragment (org.eclipse.jdt.internal.core.PackageFragment)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 WorkingCopyOwner (org.eclipse.jdt.core.WorkingCopyOwner)1 CompilationUnitDeclaration (org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration)1 BasicCompilationUnit (org.eclipse.jdt.internal.core.BasicCompilationUnit)1 ClassFile (org.eclipse.jdt.internal.core.ClassFile)1 DefaultWorkingCopyOwner (org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner)1