Search in sources :

Example 21 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project che by eclipse.

the class SourceMapper method enterAbstractMethod.

private void enterAbstractMethod(MethodInfo methodInfo) {
    if (this.typeDepth >= 0) {
        this.memberName[this.typeDepth] = new String(methodInfo.name);
        this.memberNameRange[this.typeDepth] = new SourceRange(methodInfo.nameSourceStart, methodInfo.nameSourceEnd - methodInfo.nameSourceStart + 1);
        this.memberDeclarationStart[this.typeDepth] = methodInfo.declarationStart;
        IType currentType = this.types[this.typeDepth];
        int currenTypeModifiers = this.typeModifiers[this.typeDepth];
        char[][] parameterTypes = methodInfo.parameterTypes;
        if (methodInfo.isConstructor && currentType.getDeclaringType() != null && !Flags.isStatic(currenTypeModifiers)) {
            IType declaringType = currentType.getDeclaringType();
            String declaringTypeName = declaringType.getElementName();
            if (declaringTypeName.length() == 0) {
                IClassFile classFile = declaringType.getClassFile();
                int length = parameterTypes != null ? parameterTypes.length : 0;
                char[][] newParameterTypes = new char[length + 1][];
                declaringTypeName = classFile.getElementName();
                declaringTypeName = declaringTypeName.substring(0, declaringTypeName.indexOf('.'));
                newParameterTypes[0] = declaringTypeName.toCharArray();
                if (length != 0) {
                    System.arraycopy(parameterTypes, 0, newParameterTypes, 1, length);
                }
                this.methodParameterTypes[this.typeDepth] = newParameterTypes;
            } else {
                int length = parameterTypes != null ? parameterTypes.length : 0;
                char[][] newParameterTypes = new char[length + 1][];
                newParameterTypes[0] = declaringTypeName.toCharArray();
                if (length != 0) {
                    System.arraycopy(parameterTypes, 0, newParameterTypes, 1, length);
                }
                this.methodParameterTypes[this.typeDepth] = newParameterTypes;
            }
        } else {
            this.methodParameterTypes[this.typeDepth] = parameterTypes;
        }
        this.methodParameterNames[this.typeDepth] = methodInfo.parameterNames;
        IMethod method = currentType.getMethod(this.memberName[this.typeDepth], convertTypeNamesToSigs(this.methodParameterTypes[this.typeDepth]));
        // type parameters
        if (methodInfo.typeParameters != null) {
            for (int i = 0, length = methodInfo.typeParameters.length; i < length; i++) {
                TypeParameterInfo typeParameterInfo = methodInfo.typeParameters[i];
                ITypeParameter typeParameter = method.getTypeParameter(new String(typeParameterInfo.name));
                setSourceRange(typeParameter, new SourceRange(typeParameterInfo.declarationStart, typeParameterInfo.declarationEnd - typeParameterInfo.declarationStart + 1), new SourceRange(typeParameterInfo.nameSourceStart, typeParameterInfo.nameSourceEnd - typeParameterInfo.nameSourceStart + 1));
            }
        }
        // parameters infos
        if (methodInfo.parameterInfos != null) {
            for (int i = 0, length = methodInfo.parameterInfos.length; i < length; i++) {
                ParameterInfo parameterInfo = methodInfo.parameterInfos[i];
                LocalVariableElementKey key = new LocalVariableElementKey(method, new String(parameterInfo.name));
                SourceRange[] allRanges = new SourceRange[] { new SourceRange(parameterInfo.declarationStart, parameterInfo.declarationEnd - parameterInfo.declarationStart + 1), new SourceRange(parameterInfo.nameSourceStart, parameterInfo.nameSourceEnd - parameterInfo.nameSourceStart + 1) };
                this.parametersRanges.put(key, allRanges);
                if (parameterInfo.modifiers != 0) {
                    if (this.finalParameters == null) {
                        this.finalParameters = new HashSet();
                    }
                    this.finalParameters.add(key);
                }
            }
        }
        // categories
        addCategories(method, methodInfo.categories);
    }
}
Also used : IClassFile(org.eclipse.jdt.core.IClassFile) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) IType(org.eclipse.jdt.core.IType) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) HashSet(java.util.HashSet)

Example 22 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project che by eclipse.

the class HandleFactory method createOpenable.

/**
	 * Creates an Openable handle from the given resource path.
	 * The resource path can be a path to a file in the workbench (e.g. /Proj/com/ibm/jdt/core/HandleFactory.java)
	 * or a path to a file in a jar file - it then contains the path to the jar file and the path to the file in the jar
	 * (e.g. c:/jdk1.2.2/jre/lib/rt.jar|java/lang/Object.class or /Proj/rt.jar|java/lang/Object.class)
	 * NOTE: This assumes that the resource path is the toString() of an IPath,
	 *       in other words, it uses the IPath.SEPARATOR for file path
	 *            and it uses '/' for entries in a zip file.
	 * If not null, uses the given scope as a hint for getting Java project handles.
	 */
public Openable createOpenable(String resourcePath, IJavaSearchScope scope) {
    int separatorIndex;
    if ((separatorIndex = resourcePath.indexOf(IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR)) > -1) {
        // path to a class file inside a jar
        // Optimization: cache package fragment root handle and package handles
        int rootPathLength;
        if (this.lastPkgFragmentRootPath == null || (rootPathLength = this.lastPkgFragmentRootPath.length()) != resourcePath.length() || !resourcePath.regionMatches(0, this.lastPkgFragmentRootPath, 0, rootPathLength)) {
            String jarPath = resourcePath.substring(0, separatorIndex);
            PackageFragmentRoot root = getJarPkgFragmentRoot(resourcePath, separatorIndex, jarPath, scope);
            if (root == null)
                // match is outside classpath
                return null;
            this.lastPkgFragmentRootPath = jarPath;
            this.lastPkgFragmentRoot = root;
            this.packageHandles = new HashtableOfArrayToObject(5);
        }
        // create handle
        String classFilePath = resourcePath.substring(separatorIndex + 1);
        String[] simpleNames = new Path(classFilePath).segments();
        String[] pkgName;
        int length = simpleNames.length - 1;
        if (length > 0) {
            pkgName = new String[length];
            System.arraycopy(simpleNames, 0, pkgName, 0, length);
        } else {
            pkgName = CharOperation.NO_STRINGS;
        }
        IPackageFragment pkgFragment = (IPackageFragment) this.packageHandles.get(pkgName);
        if (pkgFragment == null) {
            pkgFragment = this.lastPkgFragmentRoot.getPackageFragment(pkgName);
            this.packageHandles.put(pkgName, pkgFragment);
        }
        IClassFile classFile = pkgFragment.getClassFile(simpleNames[length]);
        return (Openable) classFile;
    } else {
        // path to a file in a directory
        // Optimization: cache package fragment root handle and package handles
        int rootPathLength = -1;
        if (this.lastPkgFragmentRootPath == null || !(resourcePath.startsWith(this.lastPkgFragmentRootPath) && !org.eclipse.jdt.internal.compiler.util.Util.isExcluded(resourcePath.toCharArray(), this.lastPkgFragmentRoot.fullInclusionPatternChars(), this.lastPkgFragmentRoot.fullExclusionPatternChars(), false) && (rootPathLength = this.lastPkgFragmentRootPath.length()) > 0 && resourcePath.charAt(rootPathLength) == '/')) {
            PackageFragmentRoot root = getPkgFragmentRoot(resourcePath);
            if (root == null)
                // match is outside classpath
                return null;
            this.lastPkgFragmentRoot = root;
            this.lastPkgFragmentRootPath = this.lastPkgFragmentRoot.internalPath().toString();
            this.packageHandles = new HashtableOfArrayToObject(5);
        }
        // create handle
        resourcePath = resourcePath.substring(this.lastPkgFragmentRootPath.length() + 1);
        String[] simpleNames = new Path(resourcePath).segments();
        String[] pkgName;
        int length = simpleNames.length - 1;
        if (length > 0) {
            pkgName = new String[length];
            System.arraycopy(simpleNames, 0, pkgName, 0, length);
        } else {
            pkgName = CharOperation.NO_STRINGS;
        }
        IPackageFragment pkgFragment = (IPackageFragment) this.packageHandles.get(pkgName);
        if (pkgFragment == null) {
            pkgFragment = this.lastPkgFragmentRoot.getPackageFragment(pkgName);
            this.packageHandles.put(pkgName, pkgFragment);
        }
        String simpleName = simpleNames[length];
        if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(simpleName)) {
            ICompilationUnit unit = pkgFragment.getCompilationUnit(simpleName);
            return (Openable) unit;
        } else if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(simpleName)) {
            IClassFile classFile = pkgFragment.getClassFile(simpleName);
            return (Openable) classFile;
        }
        return null;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClassFile(org.eclipse.jdt.core.IClassFile) Openable(org.eclipse.jdt.internal.core.Openable)

Example 23 with IClassFile

use of org.eclipse.jdt.core.IClassFile 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 24 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project che by eclipse.

the class JavaNavigation method convertToJarEntry.

private List<JarEntry> convertToJarEntry(Object[] rootContent, IPackageFragmentRoot root) throws JavaModelException {
    List<JarEntry> result = new ArrayList<>();
    for (Object o : rootContent) {
        if (o instanceof IPackageFragment) {
            JarEntry entry = DtoFactory.getInstance().createDto(JarEntry.class);
            IPackageFragment packageFragment = (IPackageFragment) o;
            entry.setName(getSpecificText((IJavaElement) o));
            entry.setPath(packageFragment.getElementName());
            entry.setType(JarEntryType.PACKAGE);
            result.add(entry);
        }
        if (o instanceof IClassFile) {
            JarEntry entry = getJarClass((IClassFile) o);
            result.add(entry);
        }
        if (o instanceof JarEntryResource) {
            result.add(getJarEntryResource((JarEntryResource) o));
        }
    }
    Collections.sort(result, comparator);
    return result;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClassFile(org.eclipse.jdt.core.IClassFile) ArrayList(java.util.ArrayList) JarEntry(org.eclipse.che.ide.ext.java.shared.JarEntry) JarEntryResource(org.eclipse.jdt.internal.core.JarEntryResource) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource)

Example 25 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project eclipse.jdt.ls by eclipse.

the class NavigateToDefinitionHandler method computeDefinitionNavigation.

private Location computeDefinitionNavigation(ITypeRoot unit, int line, int column, IProgressMonitor monitor) {
    try {
        IJavaElement element = JDTUtils.findElementAtSelection(unit, line, column, this.preferenceManager, monitor);
        if (element == null) {
            return null;
        }
        ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
        IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
        if (compilationUnit != null || (cf != null && cf.getSourceRange() != null)) {
            return JDTUtils.toLocation(element);
        }
        if (element instanceof IMember && ((IMember) element).getClassFile() != null) {
            return JDTUtils.toLocation(((IMember) element).getClassFile());
        }
    } catch (JavaModelException e) {
        JavaLanguageServerPlugin.logException("Problem computing definition for" + unit.getElementName(), e);
    }
    return null;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) IMember(org.eclipse.jdt.core.IMember)

Aggregations

IClassFile (org.eclipse.jdt.core.IClassFile)34 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)20 IJavaElement (org.eclipse.jdt.core.IJavaElement)13 IType (org.eclipse.jdt.core.IType)9 JavaModelException (org.eclipse.jdt.core.JavaModelException)8 CoreException (org.eclipse.core.runtime.CoreException)7 ArrayList (java.util.ArrayList)6 ISourceRange (org.eclipse.jdt.core.ISourceRange)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 IResource (org.eclipse.core.resources.IResource)3 IStorage (org.eclipse.core.resources.IStorage)3 IMethod (org.eclipse.jdt.core.IMethod)3 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)3 ISourceReference (org.eclipse.jdt.core.ISourceReference)3 Document (org.eclipse.jface.text.Document)3 IOException (java.io.IOException)2 DebuggerException (org.eclipse.che.api.debugger.server.exceptions.DebuggerException)2 JarEntry (org.eclipse.che.ide.ext.java.shared.JarEntry)2