Search in sources :

Example 71 with JavaModelException

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

the class ExtractConstantRefactoring method getExcludedVariableNames.

private String[] getExcludedVariableNames() {
    if (fExcludedVariableNames == null) {
        try {
            IExpressionFragment expr = getSelectedExpression();
            Collection<String> takenNames = new ScopeAnalyzer(fCuRewrite.getRoot()).getUsedVariableNames(expr.getStartPosition(), expr.getLength());
            fExcludedVariableNames = takenNames.toArray(new String[takenNames.size()]);
        } catch (JavaModelException e) {
            fExcludedVariableNames = new String[0];
        }
    }
    return fExcludedVariableNames;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)

Example 72 with JavaModelException

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

the class JavaContext method addStaticImport.

/**
	 * Adds a static import for the member with name <code>qualifiedMemberName</code>. The member is
	 * either a static field or a static method or a '*' to import all static members of a type.
	 *
	 * @param qualifiedMemberName the fully qualified name of the member to import or a qualified type
	 * 			name plus a '.*' suffix.
	 * @return returns either the simple member name if the import was successful or else the qualified name.
	 * @since 3.4
	 */
public String addStaticImport(String qualifiedMemberName) {
    if (isReadOnly())
        return qualifiedMemberName;
    ICompilationUnit cu = getCompilationUnit();
    if (cu == null)
        return qualifiedMemberName;
    int memberOffset = qualifiedMemberName.lastIndexOf('.');
    if (memberOffset == -1)
        return qualifiedMemberName;
    String typeName = qualifiedMemberName.substring(0, memberOffset);
    String memberName = qualifiedMemberName.substring(memberOffset + 1, qualifiedMemberName.length());
    try {
        boolean isField;
        if ("*".equals(memberName)) {
            //$NON-NLS-1$
            isField = true;
        } else {
            IJavaProject javaProject = cu.getJavaProject();
            IType type = javaProject.findType(typeName);
            if (type == null)
                return qualifiedMemberName;
            IField field = type.getField(memberName);
            if (field.exists()) {
                isField = true;
            } else if (hasMethod(type, memberName)) {
                isField = false;
            } else {
                return qualifiedMemberName;
            }
        }
        CompilationUnit root = getASTRoot(cu);
        if (fImportRewrite == null) {
            if (root == null) {
                fImportRewrite = StubUtility.createImportRewrite(cu, true);
            } else {
                fImportRewrite = StubUtility.createImportRewrite(root, true);
            }
        }
        ImportRewriteContext context;
        if (root == null)
            context = null;
        else
            context = new ContextSensitiveImportRewriteContext(root, getCompletionOffset(), fImportRewrite);
        return fImportRewrite.addStaticImport(typeName, memberName, isField, context);
    } catch (JavaModelException e) {
        handleException(null, e);
        return typeName;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Example 73 with JavaModelException

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

the class MethodsSourcePositionComparator method compareInTheSameType.

private int compareInTheSameType(IMethodBinding firstMethodBinding, IMethodBinding secondMethodBinding) {
    try {
        IMethod firstMethod = (IMethod) firstMethodBinding.getJavaElement();
        IMethod secondMethod = (IMethod) secondMethodBinding.getJavaElement();
        if (firstMethod == null || secondMethod == null) {
            return 0;
        }
        ISourceRange firstSourceRange = firstMethod.getSourceRange();
        ISourceRange secondSourceRange = secondMethod.getSourceRange();
        if (!SourceRange.isAvailable(firstSourceRange) || !SourceRange.isAvailable(secondSourceRange)) {
            return firstMethod.getElementName().compareTo(secondMethod.getElementName());
        } else {
            return firstSourceRange.getOffset() - secondSourceRange.getOffset();
        }
    } catch (JavaModelException e) {
        return 0;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IMethod(org.eclipse.jdt.core.IMethod) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 74 with JavaModelException

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

the class JavaSearchScopeFactory method addJavaElements.

private void addJavaElements(Set<IJavaElement> javaElements, IWorkingSet workingSet) {
    if (workingSet == null)
        return;
    if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
        try {
            IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
            javaElements.addAll(Arrays.asList(projects));
        } catch (JavaModelException e) {
            JavaPlugin.log(e);
        }
        return;
    }
    IAdaptable[] elements = workingSet.getElements();
    for (int i = 0; i < elements.length; i++) {
        IJavaElement javaElement = (IJavaElement) elements[i].getAdapter(IJavaElement.class);
        if (javaElement != null) {
            addJavaElements(javaElements, javaElement);
            continue;
        }
        IResource resource = (IResource) elements[i].getAdapter(IResource.class);
        if (resource != null) {
            addJavaElements(javaElements, resource);
        }
    // else we don't know what to do with it, ignore.
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IResource(org.eclipse.core.resources.IResource)

Example 75 with JavaModelException

use of org.eclipse.jdt.core.JavaModelException in project tdi-studio-se by Talend.

the class AddPackageDialog method getPackageFragments.

/**
     * Gets the package fragments.
     * 
     * @param javaModel The java model
     * @return The package fragments
     */
private Object[] getPackageFragments(IJavaModel javaModel) {
    Set<String> packageElements = new HashSet<String>();
    IJavaProject[] projects;
    try {
        projects = javaModel.getJavaProjects();
    } catch (JavaModelException e) {
        Activator.log(IStatus.ERROR, Messages.getJavaModelFailedMsg, e);
        return new Object[0];
    }
    for (IJavaProject project : projects) {
        if (EXTERNAL_PLUGIN_LIBRARIES.equals(project.getResource().getName())) {
            continue;
        }
        IPackageFragmentRoot[] packageFragmentRoots;
        try {
            packageFragmentRoots = project.getPackageFragmentRoots();
        } catch (JavaModelException e) {
            continue;
        }
        for (IPackageFragmentRoot packageFragment : packageFragmentRoots) {
            try {
                addPackage(packageElements, packageFragment);
            } catch (JavaModelException e) {
            // do nothing
            }
        }
    }
    for (Object packageName : filteringPackages) {
        packageElements.remove(packageName);
    }
    return packageElements.toArray(new String[0]);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashSet(java.util.HashSet) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

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