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

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

Introduction

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

Prototype

boolean isReadOnly();

Source Link

Document

Returns whether this Java element is read-only.

Usage

From source file:org.bonitasoft.studio.importer.bar.custom.migration.connector.mapper.ConnectorDescriptorToConnectorDefinition.java

License:Open Source License

protected Map<String, String> mergeSourceFile(String implementationClassname,
        ConnectorSourceRepositoryStore sourceStore) throws ZipException, IOException, CoreException {
    final ZipFile zipfile = new ZipFile(tmpConnectorJarFile);
    Enumeration<?> enumEntries = zipfile.entries();
    ZipEntry zipEntry = null;//from  w  w w.ja  va  2s  .co m
    boolean sourceImported = false;
    while (enumEntries.hasMoreElements()) {
        zipEntry = (ZipEntry) enumEntries.nextElement();
        if (!zipEntry.isDirectory()
                && zipEntry.getName().equals(implementationClassname.replace(".", "/") + ".java")) {
            sourceStore.importInputStream(zipEntry.getName(), zipfile.getInputStream(zipEntry));
            sourceImported = true;
            break;
        }
    }
    zipfile.close();
    if (sourceImported) {
        RepositoryManager.getInstance().getCurrentRepository().refresh(Repository.NULL_PROGRESS_MONITOR);
        IJavaProject project = RepositoryManager.getInstance().getCurrentRepository().getJavaProject();
        IType originalImplType = project.findType(implementationClassname);
        IType implType = project.findType(getNewImplementationClassName());
        if (originalImplType != null && implType != null) {
            org.eclipse.jface.text.Document doc = new org.eclipse.jface.text.Document();
            for (IMethod method : originalImplType.getMethods()) {
                if (!isGetter(method) && !isSetter(method)) {
                    if (!method.getElementName().equals("executeConnector")) {
                        try {
                            if (!method.isReadOnly()) {
                                method.copy(implType, null, null, false, Repository.NULL_PROGRESS_MONITOR);
                            }
                        } catch (CoreException e) {
                            BonitaStudioLog.error(e);
                        }
                    }
                }
            }
            for (IMethod method : originalImplType.getMethods()) {
                if (!isGetter(method) && !isSetter(method)) {
                    if (method.getElementName().equals("executeConnector")) {
                        IMethod executeMethod = implType.getMethod("executeBusinessLogic", new String[0]);
                        ISourceRange range = executeMethod.getSourceRange();
                        String toAdd = MIGRATION_COMMENT + method.getSource().substring(
                                method.getSource().indexOf("{") + 1, method.getSource().length() - 1);
                        doc.set(implType.getCompilationUnit().getSource());
                        try {
                            doc.replace(range.getOffset() + range.getLength() - 1, 0, toAdd);
                        } catch (BadLocationException e) {
                            BonitaStudioLog.error(e);
                        }

                    }
                }
            }
            if (!doc.get().isEmpty()) {
                BufferedWriter out = null;
                try {
                    out = new BufferedWriter(new FileWriter(
                            implType.getCompilationUnit().getCorrespondingResource().getLocation().toFile()));
                    out.write(doc.get());
                    out.flush();
                } finally {
                    if (out != null) {
                        out.close();
                    }
                }
            }
            ICompilationUnit compilationUnit = originalImplType.getCompilationUnit();
            if (compilationUnit != null) {
                compilationUnit.delete(true, Repository.NULL_PROGRESS_MONITOR);
            }
            RepositoryManager.getInstance().getCurrentRepository().refresh(Repository.NULL_PROGRESS_MONITOR);
        }
    }
    return Collections.emptyMap();
}

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

License:Open Source License

/**
 * Checks the ripple methods to make sure they can validly be renamed.
 *///  ww  w  . j a v  a  2 s . c  o  m
private RefactoringStatus checkRelatedElements() throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    if (itdKind == Kind.INTER_TYPE_FIELD) {
        return result;
    }
    for (IMember member : elementsToRename) {
        if (!(member instanceof IMethod)) {
            result.merge(RefactoringStatus.createErrorStatus("Related element is not a method.",
                    JavaStatusContext.create(member)));
        }
        IMethod method = (IMethod) member;

        result.merge(Checks.checkIfConstructorName(method, getNewElementName(),
                method.getDeclaringType().getElementName()));

        String[] msgData = new String[] { BasicElementLabels.getJavaElementName(method.getElementName()),
                BasicElementLabels.getJavaElementName(method.getDeclaringType().getFullyQualifiedName('.')) };
        if (!method.exists()) {
            result.addFatalError(
                    Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_not_in_model, msgData));
            continue;
        }
        if (method.isBinary())
            result.addFatalError(
                    Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_binary, msgData));
        if (method.isReadOnly())
            result.addFatalError(
                    Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_read_only, msgData));
        if (JdtFlags.isNative(method))
            result.addError(
                    Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_native_1, msgData));
    }
    return result;
}

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

License:Open Source License

private void validateProducer(CDIValidationContext context, IProducer producer) {
    try {//from w w  w. j a  v  a  2  s.co m
        Collection<ITypeDeclaration> typeDeclarations = producer.getAllTypeDeclarations();
        String[] typeVariables = producer.getBeanClass().getTypeParameterSignatures();
        ITypeDeclaration typeDeclaration = null;
        ITextSourceReference typeDeclarationReference = null;
        if (!typeDeclarations.isEmpty()) {
            /*
             * 3.3. Producer methods
             *  - producer method return type contains a wildcard type parameter
             * 
             * 2.2.1 Legal bean types
             *  - a parameterized type that contains a wildcard type parameter is not a legal bean type.
             * 
             * 3.4. Producer fields
             *  - producer field type contains a wildcard type parameter
             */
            typeDeclaration = typeDeclarations.iterator().next();

            typeDeclarationReference = CDIUtil.convertToJavaSourceReference(typeDeclaration,
                    producer.getSourceMember());

            String[] paramTypes = Signature.getTypeArguments(typeDeclaration.getSignature());
            boolean variable = false;
            for (String paramType : paramTypes) {
                if (Signature.getTypeSignatureKind(paramType) == Signature.WILDCARD_TYPE_SIGNATURE) {
                    if (producer instanceof IProducerField) {
                        addProblem(CDIValidationMessages.PRODUCER_FIELD_TYPE_HAS_WILDCARD,
                                CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE,
                                typeDeclarationReference, producer.getResource());
                    } else {
                        addProblem(CDIValidationMessages.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD,
                                CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE,
                                typeDeclarationReference, producer.getResource());
                    }
                } else if (!variable
                        && isTypeVariable(producer, Signature.toString(paramType), typeVariables)) {
                    /*
                     * 3.3. Producer methods
                     *  - producer method with a parameterized return type with a type variable declares any scope other than @Dependent
                     * 
                     * 3.4. Producer fields
                     *  - producer field with a parameterized type with a type variable declares any scope other than @Dependent
                     */
                    variable = true;
                    IAnnotationDeclaration scopeOrStereotypeDeclaration = CDIUtil
                            .getDifferentScopeDeclarationThanDepentend(producer);
                    if (scopeOrStereotypeDeclaration != null) {
                        boolean field = producer instanceof IProducerField;
                        addProblem(
                                field ? CDIValidationMessages.ILLEGAL_SCOPE_FOR_PRODUCER_FIELD
                                        : CDIValidationMessages.ILLEGAL_SCOPE_FOR_PRODUCER_METHOD,
                                field ? CDIPreferences.ILLEGAL_SCOPE_FOR_BEAN
                                        : CDIPreferences.ILLEGAL_SCOPE_FOR_BEAN,
                                scopeOrStereotypeDeclaration, producer.getResource());
                    }
                    break;
                }
            }
        }

        /*
         * 3.3.2. Declaring a producer method
         *  - producer method is annotated @Inject
         */
        IAnnotationDeclaration inject = producer.getAnnotation(CDIConstants.INJECT_ANNOTATION_TYPE_NAME);
        if (inject != null) {
            addProblem(CDIValidationMessages.PRODUCER_ANNOTATED_INJECT,
                    CDIPreferences.PRODUCER_ANNOTATED_INJECT, inject,
                    inject.getResource() != null ? inject.getResource() : producer.getResource(),
                    PRODUCER_ANNOTATED_INJECT_ID);
        }

        if (producer instanceof IProducerField) {
            /*
             * 3.5.1. Declaring a resource
             *  - producer field declaration specifies an EL name (together with one of @Resource, @PersistenceContext, @PersistenceUnit, @EJB, @WebServiceRef)
             */
            IProducerField producerField = (IProducerField) producer;
            if (producerField.getName() != null) {
                IAnnotationDeclaration declaration;
                for (String annotationType : RESOURCE_ANNOTATIONS) {
                    declaration = producerField.getAnnotation(annotationType);
                    if (declaration != null) {
                        IAnnotationDeclaration nameDeclaration = producerField
                                .getAnnotation(CDIConstants.NAMED_QUALIFIER_TYPE_NAME);
                        if (nameDeclaration != null) {
                            declaration = nameDeclaration;
                        }
                        addProblem(CDIValidationMessages.RESOURCE_PRODUCER_FIELD_SETS_EL_NAME,
                                CDIPreferences.RESOURCE_PRODUCER_FIELD_SETS_EL_NAME, declaration,
                                producer.getResource());
                    }
                }
            }
            /*
             * 3.4. Producer fields
             *  - producer field type is a type variable
             */
            if (typeVariables.length > 0) {
                String typeSign = producerField.getField().getTypeSignature();
                String typeString = Signature.toString(typeSign);
                for (String variableSig : typeVariables) {
                    String variableName = Signature.getTypeVariable(variableSig);
                    if (typeString.equals(variableName)) {
                        addProblem(CDIValidationMessages.PRODUCER_FIELD_TYPE_IS_VARIABLE,
                                CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE,
                                typeDeclaration != null ? typeDeclarationReference : producer,
                                producer.getResource());
                    }
                }
            }
            /*
             * 3.4.2. Declaring a producer field
             *  - non-static field of a session bean class is annotated @Produces
             */
            if (producer.getClassBean() instanceof ISessionBean
                    && !Flags.isStatic(producerField.getField().getFlags())) {
                addProblem(CDIValidationMessages.ILLEGAL_PRODUCER_FIELD_IN_SESSION_BEAN,
                        CDIPreferences.ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN,
                        producer.getProducesAnnotation(), producer.getResource(),
                        ILLEGAL_PRODUCER_FIELD_IN_SESSION_BEAN_ID);
            }
        } else {
            IProducerMethod producerMethod = (IProducerMethod) producer;
            List<IParameter> params = producerMethod.getParameters();
            Collection<ITextSourceReference> observesDeclarations = new ArrayList<ITextSourceReference>();
            Collection<ITextSourceReference> disposalDeclarations = new ArrayList<ITextSourceReference>();
            IAnnotationDeclaration producesDeclaration = producerMethod
                    .getAnnotation(CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME);
            if (producesDeclaration != null) {
                observesDeclarations.add(producesDeclaration);
                disposalDeclarations.add(producesDeclaration);
            }
            for (IParameter param : params) {
                /*
                 * 3.3.6. Declaring a disposer method
                 *  - a disposer method is annotated @Produces.
                 * 
                 * 3.3.2. Declaring a producer method
                 *  - a has a parameter annotated @Disposes
                 */
                ITextSourceReference declaration = param
                        .getAnnotationPosition(CDIConstants.DISPOSES_ANNOTATION_TYPE_NAME);
                if (declaration != null) {
                    disposalDeclarations.add(declaration);
                }
                /*
                 * 3.3.2. Declaring a producer method
                 *  - a has a parameter annotated @Observers
                 * 
                 * 10.4.2. Declaring an observer method
                 *  - an observer method is annotated @Produces
                 */
                declaration = param.getAnnotationPosition(CDIConstants.OBSERVERS_ANNOTATION_TYPE_NAME);
                if (declaration != null) {
                    observesDeclarations.add(declaration);
                }
            }
            if (observesDeclarations.size() > 1) {
                for (ITextSourceReference declaration : observesDeclarations) {
                    addProblem(CDIValidationMessages.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_OBSERVES,
                            CDIPreferences.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED, declaration,
                            producer.getResource(), PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_OBSERVES_ID);
                }
            }
            if (disposalDeclarations.size() > 1) {
                for (ITextSourceReference declaration : disposalDeclarations) {
                    addProblem(CDIValidationMessages.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_DISPOSES,
                            CDIPreferences.PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED, declaration,
                            producer.getResource(), PRODUCER_PARAMETER_ILLEGALLY_ANNOTATED_DISPOSES_ID);
                }
            }

            /*
             * 3.3. Producer methods
             *  - producer method return type is a type variable
             * 
             * 2.2.1 - Legal bean types
             *  - a type variable is not a legal bean type
             */
            String typeSign = producerMethod.getMethod().getReturnType();
            String typeString = Signature.toString(typeSign);
            if (isTypeVariable(producerMethod, typeString, typeVariables)) {
                addProblem(CDIValidationMessages.PRODUCER_METHOD_RETURN_TYPE_IS_VARIABLE,
                        CDIPreferences.PRODUCER_METHOD_RETURN_TYPE_HAS_WILDCARD_OR_VARIABLE,
                        typeDeclaration != null ? typeDeclarationReference : producer, producer.getResource());
            }
            /*
             * 3.3.2. Declaring a producer method
             *  - non-static method of a session bean class is annotated @Produces, and the method is not a business method of the session bean
             */
            IClassBean classBean = producer.getClassBean();
            if (classBean instanceof ISessionBean) {
                IMethod method = CDIUtil.getBusinessMethodDeclaration((SessionBean) classBean, producerMethod);
                if (method == null) {
                    String bindedErrorMessage = NLS.bind(
                            CDIValidationMessages.ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN,
                            new String[] { producerMethod.getMethod().getElementName(),
                                    producer.getBeanClass().getElementName() });
                    addProblem(bindedErrorMessage, CDIPreferences.ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN,
                            producer.getProducesAnnotation(), producer.getResource(),
                            ILLEGAL_PRODUCER_METHOD_IN_SESSION_BEAN_ID);
                    saveAllSuperTypesAsLinkedResources(classBean);
                } else if (!isAsYouTypeValidation() && method != producerMethod.getMethod() && method.exists()
                        && !method.isReadOnly()) {
                    getValidationContext().addLinkedCoreResource(SHORT_ID,
                            classBean.getSourcePath().toOSString(), method.getResource().getFullPath(), false);
                }
            }

            IAnnotationDeclaration sDeclaration = producerMethod.getSpecializesAnnotationDeclaration();
            if (sDeclaration != null) {
                if (Flags.isStatic(producerMethod.getMethod().getFlags())) {
                    /*
                     * 3.3.3. Specializing a producer method
                     *  - method annotated @Specializes is static
                     */
                    addProblem(CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_STATIC,
                            CDIPreferences.ILLEGAL_SPECIALIZING_BEAN, sDeclaration, producer.getResource());
                } else {
                    /*
                     * 3.3.3. Specializing a producer method
                     *  - method annotated @Specializes does not directly override another producer method
                     */
                    IMethod superMethod = CDIUtil.getDirectOverridingMethodDeclaration(producerMethod);
                    boolean overrides = false;
                    if (superMethod != null) {
                        IType superType = superMethod.getDeclaringType();
                        if (superType.isBinary()) {
                            IAnnotation[] ants = superMethod.getAnnotations();
                            for (IAnnotation an : ants) {
                                if (CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME.equals(an.getElementName())) {
                                    overrides = true;
                                }
                            }
                        } else {
                            Collection<IBean> beans = context.getCdiProject()
                                    .getBeans(superType.getResource().getFullPath());
                            for (IBean iBean : beans) {
                                if (iBean instanceof IProducerMethod) {
                                    IProducerMethod prMethod = (IProducerMethod) iBean;
                                    if (prMethod.getMethod().isSimilar(superMethod)) {
                                        overrides = true;
                                    }
                                }
                            }
                        }
                    }
                    if (!overrides) {
                        addProblem(CDIValidationMessages.ILLEGAL_SPECIALIZING_PRODUCER_OVERRIDE,
                                CDIPreferences.ILLEGAL_SPECIALIZING_BEAN, sDeclaration, producer.getResource());
                    }
                    saveAllSuperTypesAsLinkedResources(producer.getClassBean());
                }
            }
        }
    } catch (JavaModelException e) {
        CDICorePlugin.getDefault().logError(e);
    }
}