Example usage for org.eclipse.jdt.core IType isReadOnly

List of usage examples for org.eclipse.jdt.core IType isReadOnly

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType isReadOnly.

Prototype

boolean isReadOnly();

Source Link

Document

Returns whether this Java element is read-only.

Usage

From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java

License:Open Source License

public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance,
        Map<String, LinkedCorrectionProposal> proposals) throws CoreException {
    Name node = refNode;//from w  w  w .ja  va 2 s . co m
    //      do {
    String typeName = ASTNodes.getSimpleNameIdentifier(node);
    Name qualifier = null;
    // only propose to create types for qualifiers when the name starts with upper case
    boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
    if (isPossibleName) {
        IPackageFragment enclosingPackage = null;
        IType enclosingType = null;
        if (node.isSimpleName()) {
            enclosingPackage = (IPackageFragment) cu.getParent();
            return;
            // don't suggest member type, user can select it in wizard
        } else {
            Name qualifierName = ((QualifiedName) node).getQualifier();
            IBinding binding = qualifierName.resolveBinding();
            if (binding != null && binding.isRecovered()) {
                binding = null;
            }
            if (binding instanceof ITypeBinding) {
                enclosingType = (IType) binding.getJavaElement();
            } else if (binding instanceof IPackageBinding) {
                qualifier = qualifierName;
                enclosingPackage = (IPackageFragment) binding.getJavaElement();
            } else {
                IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
                if (res != null && res.length > 0 && res[0] instanceof IType) {
                    enclosingType = (IType) res[0];
                } else {
                    qualifier = qualifierName;
                    enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu)
                            .getPackageFragment(ASTResolving.getFullName(qualifierName));
                }
            }
        }
        int rel = relevance;
        if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
            rel += 3;
        }

        if (enclosingPackage != null
                && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists()
                || enclosingType != null && !enclosingType.isReadOnly()
                        && !enclosingType.getType(typeName).exists()) { // new member type
            IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;

            String name = node.getFullyQualifiedName();

            if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_CLASS, enclosing, rel + 3));
            } else if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_INTERFACE, enclosing, rel + 2));
            } else if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
                proposals.put(name, new NewCUProposal(cu, node, NewCUProposal.K_ENUM, enclosing, rel));
            }
        }
    }
    node = qualifier;
    //      } while (node != null);
}

From source file:edu.illinois.compositerefactorings.refactorings.copymembertosubtype.CopyMemberToSubtypeRefactoringProcessor.java

License:Open Source License

/**
 * {@link org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor#getAbstractDestinations(IProgressMonitor)}
 *//*from  w ww  .  j av a2s.com*/
private IType[] getAbstractDestinations(IProgressMonitor monitor) throws JavaModelException {
    IType[] allDirectSubclasses = getHierarchyOfDeclaringClass(monitor).getSubclasses(getDeclaringType());
    List<IType> result = new ArrayList<IType>(allDirectSubclasses.length);
    for (int index = 0; index < allDirectSubclasses.length; index++) {
        IType subclass = allDirectSubclasses[index];
        if (subclass.exists() && !subclass.isBinary() && !subclass.isReadOnly()
                && subclass.getCompilationUnit() != null && subclass.isStructureKnown())
            result.add(subclass);
    }
    return result.toArray(new IType[result.size()]);
}

From source file:net.sf.guavaeclipse.utils.Utils.java

License:Apache License

public static String validateMethodGeneration(IType type) throws JavaModelException {
    if (type.isReadOnly())
        return "File is read only";
    if (type.isInterface())
        return "Unable to generate method for Interface";
    else/*  www  . java  2 s . c o  m*/
        return null;
}

From source file:org.eclipse.ajdt.internal.ui.editor.quickfix.UnresolvedElementsSubProcessor.java

License:Open Source License

private static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance,
        Collection proposals) throws JavaModelException {
    Name node = refNode;/*from  w  ww.j a v  a 2 s  .  c  o m*/
    do {
        String typeName = ASTNodes.getSimpleNameIdentifier(node);
        Name qualifier = null;
        // only propose to create types for qualifiers when the name starts with upper case
        boolean isPossibleName = isLikelyTypeName(typeName) || (node == refNode);
        if (isPossibleName) {
            IPackageFragment enclosingPackage = null;
            IType enclosingType = null;
            if (node.isSimpleName()) {
                enclosingPackage = (IPackageFragment) cu.getParent();
                // don't suggest member type, user can select it in wizard
            } else {
                Name qualifierName = ((QualifiedName) node).getQualifier();
                IBinding binding = qualifierName.resolveBinding();
                if (binding instanceof ITypeBinding) {
                    enclosingType = (IType) binding.getJavaElement();
                } else if (binding instanceof IPackageBinding) {
                    qualifier = qualifierName;
                    enclosingPackage = (IPackageFragment) binding.getJavaElement();
                } else {
                    IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(),
                            qualifierName.getLength());
                    if (res != null && res.length > 0 && res[0] instanceof IType) {
                        enclosingType = (IType) res[0];
                    } else {
                        qualifier = qualifierName;
                        enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu)
                                .getPackageFragment(ASTResolving.getFullName(qualifierName));
                    }
                }
            }
            int rel = relevance;
            if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
                rel += 3;
            }

            if ((enclosingPackage != null && !enclosingPackage
                    .getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists()) // new top level type
                    || (enclosingType != null && !enclosingType.isReadOnly()
                            && !enclosingType.getType(typeName).exists())) { // new member type
                IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage
                        : enclosingType;

                if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
                    proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_CLASS,
                            enclosing, rel + 2));
                }
                if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
                    proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_INTERFACE,
                            enclosing, rel + 1));
                }
                if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
                    proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ENUM,
                            enclosing, rel));
                }
                if (kind == SimilarElementsRequestor.ANNOTATIONS) { // only when in annotation
                    proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ANNOTATION,
                            enclosing, rel + 4));
                }
            }
        }
        node = qualifier;
    } while (node != null);

    // type parameter proposals
    if (refNode.isSimpleName() && ((kind & SimilarElementsRequestor.VARIABLES) != 0)) {
        CompilationUnit root = (CompilationUnit) refNode.getRoot();
        String name = ((SimpleName) refNode).getIdentifier();
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
        int baseRel = relevance;
        if (isLikelyTypeParameterName(name)) {
            baseRel += 4;
        }
        while (declaration != null) {
            IBinding binding = null;
            int rel = baseRel;
            if (declaration instanceof MethodDeclaration) {
                binding = ((MethodDeclaration) declaration).resolveBinding();
            } else if (declaration instanceof TypeDeclaration) {
                binding = ((TypeDeclaration) declaration).resolveBinding();
                rel++;
            }
            if (binding != null) {
                AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null,
                        rel);
                proposals.add(proposal);
            }
            if (!Modifier.isStatic(declaration.getModifiers())) {
                declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
            } else {
                declaration = null;
            }
        }
    }
}

From source file:org.eclipse.ajdt.internal.ui.refactoring.pullout.PullOutRefactoring.java

License:Open Source License

/**
 * Set the target aspect by giving the name of the aspect. Note that this method
 * only works if it can figure out what project to look for the aspect, so at least
 * one member to be pulled out has to be set prior to calling this method.
 */// w  ww .  j  av a  2  s  .  c  o m
public RefactoringStatus setAspect(String name) {
    IType type = null;

    try {
        if (name.length() == 0)
            return RefactoringStatus.createFatalErrorStatus("Select an Aspect.");

        type = getJavaProject().findType(name, new NullProgressMonitor());
        if (type == null || !type.exists())
            return RefactoringStatus
                    .createErrorStatus(MessageFormat.format("Aspect ''{0}'' does not exist.", name));
        if (!(type instanceof AspectElement))
            return RefactoringStatus.createErrorStatus(MessageFormat.format("''{0}'' is not an Aspect.", name));
    } catch (JavaModelException exception) {
        return RefactoringStatus.createFatalErrorStatus("Could not determine type.");
    }

    if (type.isReadOnly())
        return RefactoringStatus.createFatalErrorStatus("Type is read-only.");

    if (type.isBinary())
        return RefactoringStatus.createFatalErrorStatus("Type is binary.");

    targetAspect = (AspectElement) type;

    return new RefactoringStatus();
}

From source file:org.grails.ide.eclipse.editor.gsp.search.FindTagReferences.java

License:Open Source License

/**
 * only check for tags if the java element states that 
 * is a field in a taglib/*w  w w.ja  v a  2 s . c o m*/
 * @param elt
 * @return the tag name if the specification corresponds to a tag, or else null
 */
boolean shouldSearchForTagRefs(IJavaElement elt) {
    // strangely, if the referenced tag is from a class file, then the type is ILocalVariable
    if (elt.getElementType() == IJavaElement.LOCAL_VARIABLE) {
        elt = elt.getParent();
    }
    if (elt.getElementType() == IJavaElement.FIELD) {
        ICompilationUnit unit = (ICompilationUnit) elt.getAncestor(IJavaElement.COMPILATION_UNIT);
        if (unit instanceof GroovyCompilationUnit) {
            if (GrailsWorkspaceCore.isTagLibClass((GroovyCompilationUnit) unit)) {
                tagField = elt;
                tagName = tagField.getElementName();
            }
        } else {
            // could be a built in tag
            IType type = (IType) elt.getAncestor(IJavaElement.TYPE);
            if (type != null && type.isReadOnly() && type.getElementName().endsWith("TagLib")) {
                IPackageFragment frag = (IPackageFragment) elt.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
                if (frag.getElementName().equals("org.codehaus.groovy.grails.plugins.web.taglib")) {
                    tagField = elt;
                    tagName = tagField.getElementName();
                }
            }
        }
    }
    return tagField != null;
}

From source file:org.jboss.tools.cdi.internal.core.validation.CDICoreValidator.java

License:Open Source License

boolean shouldValidateType(IType type) {
    return type.exists() && !type.isReadOnly();
}

From source file:org.springframework.ide.eclipse.quickfix.processors.ClassDeprecatedQuickAssistProcessor.java

License:Open Source License

public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    IType type = JdtUtils.getJavaType(project.getProject(), className);
    if (!type.isReadOnly()) {
        return new ICompletionProposal[] {
                new RemoveDeprecatedQuickFixProposal(offset, length, missingEndQuote, className, type) };
    }/*  www .  j  a va  2  s  . c  om*/
    return new ICompletionProposal[0];
}

From source file:org.springframework.ide.eclipse.quickfix.processors.MethodDeprecatedQuickAssistProcessor.java

License:Open Source License

public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    IType type = JdtUtils.getJavaType(method.getJavaProject().getProject(), className);
    if (!type.isReadOnly() && method != null) {

        return new ICompletionProposal[] { new RemoveDeprecatedQuickFixProposal(offset, length, missingEndQuote,
                className, methodName, method) };
    }//  w w w .  j av a2s . co m

    return new ICompletionProposal[0];
}

From source file:org.springframework.ide.eclipse.quickfix.processors.NameSpaceElementsQuickAssistProcessor.java

License:Open Source License

public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
    String className = null, fieldName = null, methodName = null, beanName = null;

    for (ValidationProblemAttribute problemAttribute : problemAttributes) {
        if ("CLASS".equals(problemAttribute.getKey())) {
            className = (String) problemAttribute.getValue();
        } else if ("FIELD".equals(problemAttribute.getKey())) {
            fieldName = (String) problemAttribute.getValue();
        } else if ("METHOD".equals(problemAttribute.getKey())) {
            methodName = (String) problemAttribute.getValue();
        } else if ("BEAN".equals(problemAttribute.getKey())) {
            beanName = (String) problemAttribute.getValue();
        }//w  ww .j a v  a2s .co m
    }
    if ("CLASS_NOT_FOUND".equals(problemId) && className != null) {
        proposals.add(new CreateNewClassQuickFixProposal(offset, length, className, missingEndQuote,
                javaProject, new HashSet<String>(), 0));
    } else if ("FIELD_NOT_FOUND".equals(problemId) && className != null && fieldName != null) {
        IType type = JdtUtils.getJavaType(javaProject.getProject(), className);
        if (!type.isReadOnly()) {
            // rename to similar field names if one exists

            // create new field
            proposals.add(new CreateNewFieldQuickFixProposal(offset, length, text, missingEndQuote, javaProject,
                    className, fieldName));
        }
    } else if ("FIELD_NOT_STATIC".equals(problemId) && className != null && fieldName != null) {
        IType type = JdtUtils.getJavaType(javaProject.getProject(), className);
        if (!type.isReadOnly()) {
            proposals.add(new AddStaticToFieldQuickFixProposal(offset, length, missingEndQuote, javaProject,
                    className, fieldName));
        }
    } else if ("METHOD_NOT_FOUND".equals(problemId) && className != null && methodName != null) {
        ICompletionProposal proposal = QuickfixUtils.getNewMethodQuickFixProposal(methodName, "Object",
                new String[0], javaProject, className, offset, length, text, missingEndQuote, false, "method");
        if (proposal != null) {
            proposals.add(proposal);
        }
    } else if ("UNDEFINED_REFERENCED_BEAN".equals(problemId) && beanName != null) {
        proposals.addAll(BeanReferenceQuickAssistProcessor.computeBeanReferenceQuickAssistProposals(node,
                BeansSchemaConstants.ATTR_REF, file, text, beanName, offset, length, missingEndQuote));
    }

    // TODO: CLASS_NOT_FOUND
    // TODO: CLASS_IS_NOT_IN_HIERACHY
    // TODO: CLASS_IS_INTERFACE
    // TODO: CLASS_IS_CLASS

    return proposals.toArray(new ICompletionProposal[proposals.size()]);
}