Search in sources :

Example 11 with JavaModelException

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

the class StubUtility method suggestArgumentNamesWithProposals.

public static String[][] suggestArgumentNamesWithProposals(IJavaProject project, IMethodBinding binding) {
    int nParams = binding.getParameterTypes().length;
    if (nParams > 0) {
        try {
            IMethod method = (IMethod) binding.getMethodDeclaration().getJavaElement();
            if (method != null) {
                String[] parameterNames = method.getParameterNames();
                if (parameterNames.length == nParams) {
                    return suggestArgumentNamesWithProposals(project, parameterNames);
                }
            }
        } catch (JavaModelException e) {
            // ignore
            e.printStackTrace();
        }
    }
    String[][] names = new String[nParams][];
    for (int i = 0; i < names.length; i++) {
        //$NON-NLS-1$
        names[i] = new String[] { "arg" + i };
    }
    return names;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IMethod(org.eclipse.jdt.core.IMethod)

Example 12 with JavaModelException

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

the class StubUtility method getLineDelimiterUsed.

/**
	 * @param elem a Java element (doesn't have to exist)
	 * @return the existing or default line delimiter for the element
	 */
public static String getLineDelimiterUsed(IJavaElement elem) {
    IOpenable openable = elem.getOpenable();
    if (openable instanceof ITypeRoot) {
        try {
            return openable.findRecommendedLineSeparator();
        } catch (JavaModelException exception) {
        // Use project setting
        }
    }
    IJavaProject project = elem.getJavaProject();
    return getProjectLineDelimiter(project.exists() ? project : null);
}
Also used : IOpenable(org.eclipse.jdt.core.IOpenable) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeRoot(org.eclipse.jdt.core.ITypeRoot)

Example 13 with JavaModelException

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

the class ContextSensitiveImportRewriteContext method findInContext.

@Override
public int findInContext(String qualifier, String name, int kind) {
    IBinding[] declarationsInScope = getDeclarationsInScope();
    for (int i = 0; i < declarationsInScope.length; i++) {
        if (declarationsInScope[i] instanceof ITypeBinding) {
            ITypeBinding typeBinding = (ITypeBinding) declarationsInScope[i];
            if (isSameType(typeBinding, qualifier, name)) {
                return RES_NAME_FOUND;
            } else if (isConflicting(typeBinding, name)) {
                return RES_NAME_CONFLICT;
            }
        } else if (declarationsInScope[i] != null) {
            if (isConflicting(declarationsInScope[i], name)) {
                return RES_NAME_CONFLICT;
            }
        }
    }
    Name[] names = getImportedNames();
    for (int i = 0; i < names.length; i++) {
        IBinding binding = names[i].resolveBinding();
        if (binding instanceof ITypeBinding && !binding.isRecovered()) {
            ITypeBinding typeBinding = (ITypeBinding) binding;
            if (isConflictingType(typeBinding, qualifier, name)) {
                return RES_NAME_CONFLICT;
            }
        }
    }
    List<AbstractTypeDeclaration> list = fCompilationUnit.types();
    for (Iterator<AbstractTypeDeclaration> iter = list.iterator(); iter.hasNext(); ) {
        AbstractTypeDeclaration type = iter.next();
        ITypeBinding binding = type.resolveBinding();
        if (binding != null) {
            if (isSameType(binding, qualifier, name)) {
                return RES_NAME_FOUND;
            } else {
                ITypeBinding decl = containingDeclaration(binding, qualifier, name);
                while (decl != null && !decl.equals(binding)) {
                    int modifiers = decl.getModifiers();
                    if (Modifier.isPrivate(modifiers))
                        return RES_NAME_CONFLICT;
                    decl = decl.getDeclaringClass();
                }
            }
        }
    }
    String[] addedImports = fImportRewrite.getAddedImports();
    String qualifiedName = JavaModelUtil.concatenateName(qualifier, name);
    for (int i = 0; i < addedImports.length; i++) {
        String addedImport = addedImports[i];
        if (qualifiedName.equals(addedImport)) {
            return RES_NAME_FOUND;
        } else {
            if (isConflicting(name, addedImport))
                return RES_NAME_CONFLICT;
        }
    }
    if (qualifier.equals("java.lang")) {
        //$NON-NLS-1$
        //No explicit import statement required
        ITypeRoot typeRoot = fCompilationUnit.getTypeRoot();
        if (typeRoot != null) {
            IPackageFragment packageFragment = (IPackageFragment) typeRoot.getParent();
            try {
                ICompilationUnit[] compilationUnits = packageFragment.getCompilationUnits();
                for (int i = 0; i < compilationUnits.length; i++) {
                    ICompilationUnit cu = compilationUnits[i];
                    IType[] allTypes = cu.getAllTypes();
                    for (int j = 0; j < allTypes.length; j++) {
                        IType type = allTypes[j];
                        String packageTypeName = type.getFullyQualifiedName();
                        if (isConflicting(name, packageTypeName))
                            return RES_NAME_CONFLICT;
                    }
                }
            } catch (JavaModelException e) {
            }
        }
    }
    return fImportRewrite.getDefaultImportRewriteContext().findInContext(qualifier, name, kind);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 14 with JavaModelException

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

the class HandleFactory method getJarPkgFragmentRoot.

private PackageFragmentRoot getJarPkgFragmentRoot(IPath jarPath, Object target, IJavaProject[] projects) {
    for (int i = 0, projectCount = projects.length; i < projectCount; i++) {
        try {
            JavaProject javaProject = (JavaProject) projects[i];
            IClasspathEntry classpathEnty = javaProject.getClasspathEntryFor(jarPath);
            if (classpathEnty != null) {
                if (target instanceof IFile) {
                    // internal jar
                    return (PackageFragmentRoot) javaProject.getPackageFragmentRoot((IFile) target);
                } else {
                    // external jar
                    return (PackageFragmentRoot) javaProject.getPackageFragmentRoot0(jarPath);
                }
            }
        } catch (JavaModelException e) {
        // JavaModelException from getResolvedClasspath - a problem occurred while accessing project: nothing we can do, ignore
        }
    }
    return null;
}
Also used : JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 15 with JavaModelException

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

the class ASTNodes method getNodeSource.

/**
     * Returns the source of the given node from the location where it was parsed.
     * @param node the node to get the source from
     * @param extendedRange if set, the extended ranges of the nodes should ne used
     * @param removeIndent if set, the indentation is removed.
     * @return return the source for the given node or null if accessing the source failed.
     */
public static String getNodeSource(ASTNode node, boolean extendedRange, boolean removeIndent) {
    ASTNode root = node.getRoot();
    if (root instanceof CompilationUnit) {
        CompilationUnit astRoot = (CompilationUnit) root;
        ITypeRoot typeRoot = astRoot.getTypeRoot();
        try {
            if (typeRoot != null && typeRoot.getBuffer() != null) {
                IBuffer buffer = typeRoot.getBuffer();
                int offset = extendedRange ? astRoot.getExtendedStartPosition(node) : node.getStartPosition();
                int length = extendedRange ? astRoot.getExtendedLength(node) : node.getLength();
                String str = buffer.getText(offset, length);
                if (removeIndent) {
                    IJavaProject project = typeRoot.getJavaProject();
                    int indent = StubUtility.getIndentUsed(buffer, node.getStartPosition(), project);
                    str = Strings.changeIndent(str, indent, project, new String(), typeRoot.findRecommendedLineSeparator());
                }
                return str;
            }
        } catch (JavaModelException e) {
        // ignore
        }
    }
    return null;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) IBuffer(org.eclipse.jdt.core.IBuffer)

Aggregations

JavaModelException (org.eclipse.jdt.core.JavaModelException)172 IJavaProject (org.eclipse.jdt.core.IJavaProject)44 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)41 IType (org.eclipse.jdt.core.IType)40 IJavaElement (org.eclipse.jdt.core.IJavaElement)35 CoreException (org.eclipse.core.runtime.CoreException)30 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)25 IPath (org.eclipse.core.runtime.IPath)24 ArrayList (java.util.ArrayList)21 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)17 IFile (org.eclipse.core.resources.IFile)15 IResource (org.eclipse.core.resources.IResource)15 HashMap (java.util.HashMap)14 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 IMethod (org.eclipse.jdt.core.IMethod)12 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 Iterator (java.util.Iterator)8 ISourceRange (org.eclipse.jdt.core.ISourceRange)8