Search in sources :

Example 31 with JavaModelException

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

the class Java50Fix method hasFatalError.

private static boolean hasFatalError(CompilationUnit compilationUnit) {
    try {
        if (!((ICompilationUnit) compilationUnit.getJavaElement()).isStructureKnown())
            return true;
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
        return true;
    }
    IProblem[] problems = compilationUnit.getProblems();
    for (int i = 0; i < problems.length; i++) {
        if (problems[i].isError()) {
            if (!(problems[i] instanceof CategorizedProblem))
                return true;
            CategorizedProblem categorizedProblem = (CategorizedProblem) problems[i];
            int categoryID = categorizedProblem.getCategoryID();
            if (categoryID == CategorizedProblem.CAT_BUILDPATH)
                return true;
            if (categoryID == CategorizedProblem.CAT_SYNTAX)
                return true;
            if (categoryID == CategorizedProblem.CAT_IMPORT)
                return true;
            if (categoryID == CategorizedProblem.CAT_TYPE)
                return true;
            if (categoryID == CategorizedProblem.CAT_MEMBER)
                return true;
            if (categoryID == CategorizedProblem.CAT_INTERNAL)
                return true;
        }
    }
    return false;
}
Also used : CategorizedProblem(org.eclipse.jdt.core.compiler.CategorizedProblem) JavaModelException(org.eclipse.jdt.core.JavaModelException) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 32 with JavaModelException

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

the class ChangeSignatureProcessor method getParticipantArguments.

private ChangeMethodSignatureArguments getParticipantArguments() {
    ArrayList<Parameter> parameterList = new ArrayList<Parameter>();
    List<ParameterInfo> pis = getParameterInfos();
    String[] originalParameterTypeSigs = fMethod.getParameterTypes();
    for (Iterator<ParameterInfo> iter = pis.iterator(); iter.hasNext(); ) {
        ParameterInfo pi = iter.next();
        if (!pi.isDeleted()) {
            int oldIndex = pi.isAdded() ? -1 : pi.getOldIndex();
            String newName = pi.getNewName();
            String typeSig;
            if (pi.isTypeNameChanged()) {
                String newType = pi.getNewTypeName();
                if (pi.isNewVarargs()) {
                    //$NON-NLS-1$
                    newType = ParameterInfo.stripEllipsis(newType) + "[]";
                }
                typeSig = Signature.createTypeSignature(newType, false);
            } else {
                typeSig = originalParameterTypeSigs[pi.getOldIndex()];
            }
            String defaultValue = pi.getDefaultValue();
            parameterList.add(new Parameter(oldIndex, newName, typeSig, defaultValue));
        }
    }
    Parameter[] parameters = parameterList.toArray(new Parameter[parameterList.size()]);
    ArrayList<ThrownException> exceptionList = new ArrayList<ThrownException>();
    List<ExceptionInfo> exceptionInfos = getExceptionInfos();
    for (int i = 0; i < exceptionInfos.size(); i++) {
        ExceptionInfo ei = exceptionInfos.get(i);
        if (!ei.isDeleted()) {
            int oldIndex = ei.isAdded() ? -1 : i;
            String qualifiedTypeName = ei.getFullyQualifiedName();
            String newTypeSig = Signature.createTypeSignature(qualifiedTypeName, true);
            exceptionList.add(new ThrownException(oldIndex, newTypeSig));
        }
    }
    ThrownException[] exceptions = exceptionList.toArray(new ThrownException[exceptionList.size()]);
    String returnTypeSig;
    if (fReturnTypeInfo.isTypeNameChanged()) {
        returnTypeSig = Signature.createTypeSignature(fReturnTypeInfo.getNewTypeName(), false);
    } else {
        try {
            returnTypeSig = fMethod.getReturnType();
        } catch (JavaModelException e) {
            returnTypeSig = Signature.createTypeSignature(fReturnTypeInfo.getNewTypeName(), false);
        }
    }
    return new ChangeMethodSignatureArguments(fMethodName, returnTypeSig, fVisibility, parameters, exceptions, fDelegateUpdating);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) ChangeMethodSignatureArguments(org.eclipse.jdt.core.refactoring.participants.ChangeMethodSignatureArguments) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) Parameter(org.eclipse.jdt.core.refactoring.participants.ChangeMethodSignatureArguments.Parameter) MethodRefParameter(org.eclipse.jdt.core.dom.MethodRefParameter) ExceptionInfo(org.eclipse.jdt.internal.corext.refactoring.ExceptionInfo) ThrownException(org.eclipse.jdt.core.refactoring.participants.ChangeMethodSignatureArguments.ThrownException)

Example 33 with JavaModelException

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

the class ChangeSignatureProcessor method createParameterInfoList.

private static List<ParameterInfo> createParameterInfoList(IMethod method) {
    try {
        String[] typeNames = method.getParameterTypes();
        String[] oldNames = method.getParameterNames();
        List<ParameterInfo> result = new ArrayList<ParameterInfo>(typeNames.length);
        for (int i = 0; i < oldNames.length; i++) {
            ParameterInfo parameterInfo;
            if (i == oldNames.length - 1 && Flags.isVarargs(method.getFlags())) {
                String varargSignature = typeNames[i];
                int arrayCount = Signature.getArrayCount(varargSignature);
                String baseSignature = Signature.getElementType(varargSignature);
                if (arrayCount > 1)
                    baseSignature = Signature.createArraySignature(baseSignature, arrayCount - 1);
                parameterInfo = new ParameterInfo(Signature.toString(baseSignature) + ParameterInfo.ELLIPSIS, oldNames[i], i);
            } else {
                parameterInfo = new ParameterInfo(Signature.toString(typeNames[i]), oldNames[i], i);
            }
            result.add(parameterInfo);
        }
        return result;
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
        return new ArrayList<ParameterInfo>(0);
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) ArrayList(java.util.ArrayList) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo)

Example 34 with JavaModelException

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

the class ChangeSignatureProcessor method createDescriptor.

public JavaRefactoringDescriptor createDescriptor() {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fMethod.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    ChangeMethodSignatureDescriptor descriptor = null;
    try {
        final String description = Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethod.getElementName()));
        final String header = Messages.format(RefactoringCoreMessages.ChangeSignatureRefactoring_descriptor_description, new String[] { getOldMethodSignature(), getNewMethodSignature() });
        final JDTRefactoringDescriptorComment comment = createComment(project, header);
        descriptor = RefactoringSignatureDescriptorFactory.createChangeMethodSignatureDescriptor(project, description, comment.asString(), arguments, getDescriptorFlags());
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fMethod));
        arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME, fMethodName);
        arguments.put(ATTRIBUTE_DELEGATE, Boolean.valueOf(fDelegateUpdating).toString());
        arguments.put(ATTRIBUTE_DEPRECATE, Boolean.valueOf(fDelegateDeprecation).toString());
        if (fReturnTypeInfo.isTypeNameChanged())
            arguments.put(ATTRIBUTE_RETURN, fReturnTypeInfo.getNewTypeName());
        try {
            if (!isVisibilitySameAsInitial())
                arguments.put(ATTRIBUTE_VISIBILITY, new Integer(fVisibility).toString());
        } catch (JavaModelException exception) {
            JavaPlugin.log(exception);
        }
        int count = 1;
        for (final Iterator<ParameterInfo> iterator = fParameterInfos.iterator(); iterator.hasNext(); ) {
            final ParameterInfo info = iterator.next();
            final StringBuffer buffer = new StringBuffer(64);
            if (info.isAdded())
                //$NON-NLS-1$
                buffer.append("{added}");
            else
                buffer.append(info.getOldTypeName());
            //$NON-NLS-1$
            buffer.append(" ");
            if (info.isAdded())
                //$NON-NLS-1$
                buffer.append("{added}");
            else
                buffer.append(info.getOldName());
            //$NON-NLS-1$
            buffer.append(" ");
            buffer.append(info.getOldIndex());
            //$NON-NLS-1$
            buffer.append(" ");
            if (info.isDeleted())
                //$NON-NLS-1$
                buffer.append("{deleted}");
            else
                //$NON-NLS-1$//$NON-NLS-2$
                buffer.append(info.getNewTypeName().replaceAll(" ", ""));
            //$NON-NLS-1$
            buffer.append(" ");
            if (info.isDeleted())
                //$NON-NLS-1$
                buffer.append("{deleted}");
            else
                buffer.append(info.getNewName());
            //$NON-NLS-1$
            buffer.append(" ");
            buffer.append(info.isDeleted());
            arguments.put(ATTRIBUTE_PARAMETER + count, buffer.toString());
            final String value = info.getDefaultValue();
            if (//$NON-NLS-1$
            value != null && !"".equals(value))
                arguments.put(ATTRIBUTE_DEFAULT + count, value);
            count++;
        }
        count = 1;
        for (final Iterator<ExceptionInfo> iterator = fExceptionInfos.iterator(); iterator.hasNext(); ) {
            final ExceptionInfo info = iterator.next();
            arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_ELEMENT + count, JavaRefactoringDescriptorUtil.elementToHandle(project, info.getElement()));
            arguments.put(ATTRIBUTE_KIND + count, new Integer(info.getKind()).toString());
            count++;
        }
    } catch (JavaModelException exception) {
        JavaPlugin.log(exception);
        return null;
    }
    return descriptor;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) IJavaProject(org.eclipse.jdt.core.IJavaProject) ChangeMethodSignatureDescriptor(org.eclipse.jdt.core.refactoring.descriptors.ChangeMethodSignatureDescriptor) ExceptionInfo(org.eclipse.jdt.internal.corext.refactoring.ExceptionInfo)

Example 35 with JavaModelException

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

the class SelfEncapsulateFieldRefactoring method checkName.

private static void checkName(RefactoringStatus status, String name, List<IMethodBinding> usedNames, IType type, boolean reUseExistingField, IField field) {
    if ("".equals(name)) {
        //$NON-NLS-1$
        status.addFatalError(RefactoringCoreMessages.Checks_Choose_name);
        return;
    }
    boolean isStatic = false;
    try {
        isStatic = Flags.isStatic(field.getFlags());
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
    }
    status.merge(Checks.checkMethodName(name, field));
    for (Iterator<IMethodBinding> iter = usedNames.iterator(); iter.hasNext(); ) {
        IMethodBinding method = iter.next();
        String selector = method.getName();
        if (selector.equals(name)) {
            if (!reUseExistingField) {
                status.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_method_exists, new String[] { BindingLabelProvider.getBindingLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getJavaElementName(type.getElementName()) }));
            } else {
                boolean methodIsStatic = Modifier.isStatic(method.getModifiers());
                if (methodIsStatic && !isStatic)
                    status.addWarning(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_static_method_but_nonstatic_field, new String[] { BasicElementLabels.getJavaElementName(method.getName()), BasicElementLabels.getJavaElementName(field.getElementName()) }));
                if (!methodIsStatic && isStatic)
                    status.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_nonstatic_method_but_static_field, new String[] { BasicElementLabels.getJavaElementName(method.getName()), BasicElementLabels.getJavaElementName(field.getElementName()) }));
                return;
            }
        }
    }
    if (reUseExistingField)
        status.addFatalError(Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_methoddoesnotexist_status_fatalError, new String[] { BasicElementLabels.getJavaElementName(name), BasicElementLabels.getJavaElementName(type.getElementName()) }));
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) JavaModelException(org.eclipse.jdt.core.JavaModelException)

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