Search in sources :

Example 91 with JavaModelException

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

the class JavaElementLabelComposer method appendMethodLabel.

/**
	 * Appends the label for a method. Considers the M_* flags.
	 *
	 * @param method the element to render
	 * @param flags the rendering flags. Flags with names starting with 'M_' are considered.
	 */
public void appendMethodLabel(IMethod method, long flags) {
    try {
        BindingKey resolvedKey = getFlag(flags, JavaElementLabels.USE_RESOLVED) && method.isResolved() ? new BindingKey(method.getKey()) : null;
        String resolvedSig = (resolvedKey != null) ? resolvedKey.toSignature() : null;
        // type parameters
        if (getFlag(flags, JavaElementLabels.M_PRE_TYPE_PARAMETERS)) {
            if (resolvedKey != null) {
                if (resolvedKey.isParameterizedMethod()) {
                    String[] typeArgRefs = resolvedKey.getTypeArguments();
                    if (typeArgRefs.length > 0) {
                        appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags);
                        fBuffer.append(' ');
                    }
                } else {
                    String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
                    if (typeParameterSigs.length > 0) {
                        appendTypeParameterSignaturesLabel(typeParameterSigs, flags);
                        fBuffer.append(' ');
                    }
                }
            } else if (method.exists()) {
                ITypeParameter[] typeParameters = method.getTypeParameters();
                if (typeParameters.length > 0) {
                    appendTypeParametersLabels(typeParameters, flags);
                    fBuffer.append(' ');
                }
            }
        }
        // return type
        if (getFlag(flags, JavaElementLabels.M_PRE_RETURNTYPE) && method.exists() && !method.isConstructor()) {
            String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig) : method.getReturnType();
            appendTypeSignatureLabel(method, returnTypeSig, flags);
            fBuffer.append(' ');
        }
        // qualification
        if (getFlag(flags, JavaElementLabels.M_FULLY_QUALIFIED)) {
            appendTypeLabel(method.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            fBuffer.append('.');
        }
        fBuffer.append(getElementName(method));
        // constructor type arguments
        if (getFlag(flags, JavaElementLabels.T_TYPE_PARAMETERS) && method.exists() && method.isConstructor()) {
            if (resolvedSig != null && resolvedKey.isParameterizedType()) {
                BindingKey declaringType = resolvedKey.getDeclaringType();
                if (declaringType != null) {
                    String[] declaringTypeArguments = declaringType.getTypeArguments();
                    appendTypeArgumentSignaturesLabel(method, declaringTypeArguments, flags);
                }
            }
        }
        // parameters
        fBuffer.append('(');
        String[] declaredParameterTypes = method.getParameterTypes();
        if (getFlag(flags, JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES)) {
            String[] types = null;
            int nParams = 0;
            boolean renderVarargs = false;
            boolean isPolymorphic = false;
            if (getFlag(flags, JavaElementLabels.M_PARAMETER_TYPES)) {
                if (resolvedSig != null) {
                    types = Signature.getParameterTypes(resolvedSig);
                } else {
                    types = declaredParameterTypes;
                }
                nParams = types.length;
                renderVarargs = method.exists() && Flags.isVarargs(method.getFlags());
                if (renderVarargs && resolvedSig != null && declaredParameterTypes.length == 1 && JavaModelUtil.isPolymorphicSignature(method)) {
                    renderVarargs = false;
                    isPolymorphic = true;
                }
            }
            String[] names = null;
            if (getFlag(flags, JavaElementLabels.M_PARAMETER_NAMES) && method.exists()) {
                names = method.getParameterNames();
                if (isPolymorphic) {
                // handled specially below
                } else if (types == null) {
                    nParams = names.length;
                } else {
                    // types != null
                    if (nParams != names.length) {
                        if (resolvedSig != null && types.length > names.length) {
                            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=99137
                            nParams = names.length;
                            String[] typesWithoutSyntheticParams = new String[nParams];
                            System.arraycopy(types, types.length - nParams, typesWithoutSyntheticParams, 0, nParams);
                            types = typesWithoutSyntheticParams;
                        } else {
                            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=101029
                            // JavaPlugin.logErrorMessage("JavaElementLabels: Number of param types(" + nParams + ") != number of names(" + names.length + "): " + method.getElementName());   //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
                            // no names rendered
                            names = null;
                        }
                    }
                }
            }
            ILocalVariable[] annotatedParameters = null;
            if (nParams > 0 && getFlag(flags, JavaElementLabels.M_PARAMETER_ANNOTATIONS)) {
                annotatedParameters = method.getParameters();
            }
            for (int i = 0; i < nParams; i++) {
                if (i > 0) {
                    fBuffer.append(JavaElementLabels.COMMA_STRING);
                }
                if (annotatedParameters != null && i < annotatedParameters.length) {
                    appendAnnotationLabels(annotatedParameters[i].getAnnotations(), flags);
                }
                if (types != null) {
                    String paramSig = types[i];
                    if (renderVarargs && (i == nParams - 1)) {
                        int newDim = Signature.getArrayCount(paramSig) - 1;
                        appendTypeSignatureLabel(method, Signature.getElementType(paramSig), flags);
                        for (int k = 0; k < newDim; k++) {
                            fBuffer.append('[').append(']');
                        }
                        fBuffer.append(JavaElementLabels.ELLIPSIS_STRING);
                    } else {
                        appendTypeSignatureLabel(method, paramSig, flags);
                    }
                }
                if (names != null) {
                    if (types != null) {
                        fBuffer.append(' ');
                    }
                    if (isPolymorphic) {
                        fBuffer.append(names[0] + i);
                    } else {
                        fBuffer.append(names[i]);
                    }
                }
            }
        } else {
            if (declaredParameterTypes.length > 0) {
                fBuffer.append(JavaElementLabels.ELLIPSIS_STRING);
            }
        }
        fBuffer.append(')');
        if (getFlag(flags, JavaElementLabels.M_EXCEPTIONS)) {
            String[] types;
            if (resolvedKey != null) {
                types = resolvedKey.getThrownExceptions();
            } else {
                types = method.exists() ? method.getExceptionTypes() : new String[0];
            }
            if (types.length > 0) {
                //$NON-NLS-1$
                fBuffer.append(" throws ");
                for (int i = 0; i < types.length; i++) {
                    if (i > 0) {
                        fBuffer.append(JavaElementLabels.COMMA_STRING);
                    }
                    appendTypeSignatureLabel(method, types[i], flags);
                }
            }
        }
        if (getFlag(flags, JavaElementLabels.M_APP_TYPE_PARAMETERS)) {
            int offset = fBuffer.length();
            if (resolvedKey != null) {
                if (resolvedKey.isParameterizedMethod()) {
                    String[] typeArgRefs = resolvedKey.getTypeArguments();
                    if (typeArgRefs.length > 0) {
                        fBuffer.append(' ');
                        appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags);
                    }
                } else {
                    String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
                    if (typeParameterSigs.length > 0) {
                        fBuffer.append(' ');
                        appendTypeParameterSignaturesLabel(typeParameterSigs, flags);
                    }
                }
            } else if (method.exists()) {
                ITypeParameter[] typeParameters = method.getTypeParameters();
                if (typeParameters.length > 0) {
                    fBuffer.append(' ');
                    appendTypeParametersLabels(typeParameters, flags);
                }
            }
        //				if (getFlag(flags, JavaElementLabels.COLORIZE) && offset != fBuffer.length()) {
        //					fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
        //				}
        }
        if (getFlag(flags, JavaElementLabels.M_APP_RETURNTYPE) && method.exists() && !method.isConstructor()) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.DECL_STRING);
            String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig) : method.getReturnType();
            appendTypeSignatureLabel(method, returnTypeSig, flags);
        //				if (getFlag(flags, JavaElementLabels.COLORIZE)) {
        //					fBuffer.setStyle(offset, fBuffer.length() - offset, DECORATIONS_STYLE);
        //				}
        }
        // category
        if (getFlag(flags, JavaElementLabels.M_CATEGORY) && method.exists())
            appendCategoryLabel(method, flags);
        // post qualification
        if (getFlag(flags, JavaElementLabels.M_POST_QUALIFIED)) {
            int offset = fBuffer.length();
            fBuffer.append(JavaElementLabels.CONCAT_STRING);
            appendTypeLabel(method.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS));
            if (getFlag(flags, JavaElementLabels.COLORIZE)) {
                fBuffer.setStyle(offset, fBuffer.length() - offset, QUALIFIER_STYLE);
            }
        }
    } catch (JavaModelException e) {
        // NotExistsException will not reach this point
        JavaPlugin.log(e);
    }
}
Also used : ILocalVariable(org.eclipse.jdt.core.ILocalVariable) JavaModelException(org.eclipse.jdt.core.JavaModelException) BindingKey(org.eclipse.jdt.core.BindingKey) StyledString(org.eclipse.jface.viewers.StyledString)

Example 92 with JavaModelException

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

the class JavaContentAssistInvocationContext method computeKeywordsAndContext.

/**
	 * Fallback to retrieve a core context and keyword proposals when no collector is available.
	 * Runs code completion on the cu and collects keyword proposals. {@link #fKeywordProposals} is
	 * non-<code>null</code> after this call.
	 *
	 * @since 3.3
	 */
private void computeKeywordsAndContext() {
    ICompilationUnit cu = getCompilationUnit();
    if (cu == null) {
        if (fKeywordProposals == null)
            fKeywordProposals = new IJavaCompletionProposal[0];
        return;
    }
    CompletionProposalCollector collector = new CompletionProposalCollector(cu, true);
    collector.setIgnored(CompletionProposal.KEYWORD, false);
    try {
        cu.codeComplete(getInvocationOffset(), collector);
        if (fCoreContext == null)
            fCoreContext = collector.getContext();
        if (fKeywordProposals == null)
            fKeywordProposals = collector.getKeywordCompletionProposals();
        if (fLabelProvider == null)
            fLabelProvider = collector.getLabelProvider();
    } catch (JavaModelException x) {
        if (!x.isDoesNotExist() || cu.getJavaProject() == null || cu.getJavaProject().isOnClasspath(cu))
            JavaPlugin.log(x);
        if (fKeywordProposals == null)
            fKeywordProposals = new IJavaCompletionProposal[0];
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException)

Example 93 with JavaModelException

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

the class InlineConstantRefactoring method initialize.

private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
    final String selection = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
    if (selection != null) {
        int offset = -1;
        int length = -1;
        final StringTokenizer tokenizer = new StringTokenizer(selection);
        if (tokenizer.hasMoreTokens())
            offset = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (tokenizer.hasMoreTokens())
            length = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (offset >= 0 && length >= 0) {
            fSelectionStart = offset;
            fSelectionLength = length;
        } else
            return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION }));
    }
    final String handle = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
        if (element == null || !element.exists())
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
        else {
            if (element instanceof ICompilationUnit) {
                fSelectionCu = (ICompilationUnit) element;
                if (selection == null)
                    return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
            } else if (element instanceof IField) {
                final IField field = (IField) element;
                try {
                    final ISourceRange range = field.getNameRange();
                    if (range != null) {
                        fSelectionStart = range.getOffset();
                        fSelectionLength = range.getLength();
                    } else
                        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, IJavaRefactorings.INLINE_CONSTANT));
                } catch (JavaModelException exception) {
                    return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
                }
                fSelectionCu = field.getCompilationUnit();
            } else
                return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { handle, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT }));
            final ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
            parser.setResolveBindings(true);
            parser.setSource(fSelectionCu);
            final CompilationUnit unit = (CompilationUnit) parser.createAST(null);
            initialize(fSelectionCu, unit);
            if (checkStaticFinalConstantNameSelected().hasFatalError())
                return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
        }
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
    final String replace = arguments.getAttribute(ATTRIBUTE_REPLACE);
    if (replace != null) {
        fReplaceAllReferences = Boolean.valueOf(replace).booleanValue();
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REPLACE));
    final String remove = arguments.getAttribute(ATTRIBUTE_REMOVE);
    if (remove != null)
        fRemoveDeclaration = Boolean.valueOf(remove).booleanValue();
    else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REMOVE));
    return new RefactoringStatus();
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) StringTokenizer(java.util.StringTokenizer) JavaModelException(org.eclipse.jdt.core.JavaModelException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IField(org.eclipse.jdt.core.IField) ASTParser(org.eclipse.jdt.core.dom.ASTParser) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 94 with JavaModelException

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

the class ExtractTempRefactoring method guessTempNames.

/**
	 * @return proposed variable names (may be empty, but not null). The first proposal should be used as "best guess" (if it exists).
	 */
public String[] guessTempNames() {
    if (fGuessedTempNames == null) {
        try {
            Expression expression = getSelectedExpression().getAssociatedExpression();
            if (expression != null) {
                ITypeBinding binding = guessBindingForReference(expression);
                fGuessedTempNames = StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, fCu.getJavaProject(), binding, expression, Arrays.asList(getExcludedVariableNames()));
            }
        } catch (JavaModelException e) {
        }
        if (fGuessedTempNames == null)
            fGuessedTempNames = new String[0];
    }
    return fGuessedTempNames;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 95 with JavaModelException

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

the class InlineConstantRefactoring method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    try {
        pm.beginTask(RefactoringCoreMessages.InlineConstantRefactoring_preview, 2);
        final Map<String, String> arguments = new HashMap<String, String>();
        String project = null;
        IJavaProject javaProject = fSelectionCu.getJavaProject();
        if (javaProject != null)
            project = javaProject.getElementName();
        int flags = RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
        try {
            if (!Flags.isPrivate(fField.getFlags()))
                flags |= RefactoringDescriptor.MULTI_CHANGE;
        } catch (JavaModelException exception) {
            JavaPlugin.log(exception);
        }
        final String description = Messages.format(RefactoringCoreMessages.InlineConstantRefactoring_descriptor_description_short, JavaElementLabels.getElementLabel(fField, JavaElementLabels.ALL_DEFAULT));
        final String header = Messages.format(RefactoringCoreMessages.InlineConstantRefactoring_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fField, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getElementLabel(fField.getParent(), JavaElementLabels.ALL_FULLY_QUALIFIED) });
        final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
        comment.addSetting(Messages.format(RefactoringCoreMessages.InlineConstantRefactoring_original_pattern, JavaElementLabels.getElementLabel(fField, JavaElementLabels.ALL_FULLY_QUALIFIED)));
        if (fRemoveDeclaration)
            comment.addSetting(RefactoringCoreMessages.InlineConstantRefactoring_remove_declaration);
        if (fReplaceAllReferences)
            comment.addSetting(RefactoringCoreMessages.InlineConstantRefactoring_replace_references);
        final InlineConstantDescriptor descriptor = RefactoringSignatureDescriptorFactory.createInlineConstantDescriptor(project, description, comment.asString(), arguments, flags);
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fSelectionCu));
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, //$NON-NLS-1$
        new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString());
        arguments.put(ATTRIBUTE_REMOVE, Boolean.valueOf(fRemoveDeclaration).toString());
        arguments.put(ATTRIBUTE_REPLACE, Boolean.valueOf(fReplaceAllReferences).toString());
        return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.InlineConstantRefactoring_inline, fChanges);
    } finally {
        pm.done();
        fChanges = null;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) HashMap(java.util.HashMap) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) InlineConstantDescriptor(org.eclipse.jdt.core.refactoring.descriptors.InlineConstantDescriptor) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)

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