Search in sources :

Example 1 with JvmVisibility

use of org.eclipse.xtext.common.types.JvmVisibility in project xtext-xtend by eclipse.

the class XtendGenerator method isVisible.

/**
 * Determine whether the given member is visible without considering the class hierarchy.
 */
private boolean isVisible(final JvmMember member, final JvmDeclaredType context) {
    final JvmVisibility visibility = member.getVisibility();
    boolean _equals = Objects.equal(visibility, JvmVisibility.PUBLIC);
    if (_equals) {
        return true;
    }
    JvmDeclaredType _xifexpression = null;
    if ((member instanceof JvmDeclaredType)) {
        _xifexpression = ((JvmDeclaredType) member);
    } else {
        _xifexpression = member.getDeclaringType();
    }
    final JvmDeclaredType type = _xifexpression;
    if ((Objects.equal(type, context) || EcoreUtil.isAncestor(context, type))) {
        return true;
    }
    if (((type != null) && (Objects.equal(visibility, JvmVisibility.DEFAULT) || Objects.equal(visibility, JvmVisibility.PROTECTED)))) {
        if (((Strings.isEmpty(context.getPackageName()) && Strings.isEmpty(type.getPackageName())) || Objects.equal(context.getPackageName(), type.getPackageName()))) {
            return true;
        }
    }
    return false;
}
Also used : JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 2 with JvmVisibility

use of org.eclipse.xtext.common.types.JvmVisibility in project xtext-xtend by eclipse.

the class XtendJvmModelInferrer method deriveGenericDispatchOperationSignature.

/**
 * @return a {@link JvmOperation} with common denominator argument types of all given operations
 */
/* @Nullable */
protected JvmOperation deriveGenericDispatchOperationSignature(Iterable<JvmOperation> localOperations, JvmGenericType target) {
    final Iterator<JvmOperation> iterator = localOperations.iterator();
    if (!iterator.hasNext())
        return null;
    JvmOperation first = iterator.next();
    JvmOperation result = typesFactory.createJvmOperation();
    target.getMembers().add(result);
    for (int i = 0; i < first.getParameters().size(); i++) {
        JvmFormalParameter parameter = typesFactory.createJvmFormalParameter();
        result.getParameters().add(parameter);
        parameter.setParameterType(jvmTypesBuilder.inferredType());
        JvmFormalParameter parameter2 = first.getParameters().get(i);
        parameter.setName(parameter2.getName());
    }
    jvmTypesBuilder.setBody(result, compileStrategies.forDispatcher(result));
    JvmVisibility commonVisibility = null;
    boolean isFirst = true;
    boolean allStatic = true;
    for (JvmOperation jvmOperation : localOperations) {
        Iterable<XtendFunction> xtendFunctions = Iterables.filter(associations.getSourceElements(jvmOperation), XtendFunction.class);
        for (XtendFunction func : xtendFunctions) {
            JvmVisibility xtendVisibility = func.getDeclaredVisibility();
            if (isFirst) {
                commonVisibility = xtendVisibility;
                isFirst = false;
            } else if (commonVisibility != xtendVisibility) {
                commonVisibility = null;
            }
            associator.associate(func, result);
            if (!func.isStatic())
                allStatic = false;
        }
        for (JvmTypeReference declaredException : jvmOperation.getExceptions()) result.getExceptions().add(jvmTypesBuilder.cloneWithProxies(declaredException));
    }
    if (commonVisibility == null)
        result.setVisibility(JvmVisibility.PUBLIC);
    else
        result.setVisibility(commonVisibility);
    result.setStatic(allStatic);
    return result;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint)

Example 3 with JvmVisibility

use of org.eclipse.xtext.common.types.JvmVisibility in project xtext-xtend by eclipse.

the class XtendValidator method checkDispatchFunctions.

@Check
public void checkDispatchFunctions(XtendClass clazz) {
    JvmGenericType type = associations.getInferredType(clazz);
    if (type != null) {
        Multimap<DispatchHelper.DispatchSignature, JvmOperation> dispatchMethods = dispatchHelper.getDeclaredOrEnhancedDispatchMethods(type);
        checkDispatchNonDispatchConflict(clazz, dispatchMethods);
        for (DispatchHelper.DispatchSignature signature : dispatchMethods.keySet()) {
            Collection<JvmOperation> dispatchOperations = dispatchMethods.get(signature);
            JvmOperation syntheticDispatchMethod = dispatchHelper.getDispatcherOperation(type, signature);
            if (syntheticDispatchMethod != null) {
                JvmOperation overriddenOperation = overrideHelper.findOverriddenOperation(syntheticDispatchMethod);
                Boolean expectStatic = null;
                if (overriddenOperation != null) {
                    if (isMorePrivateThan(syntheticDispatchMethod.getVisibility(), overriddenOperation.getVisibility())) {
                        String msg = "Synthetic dispatch method reduces visibility of overridden method " + overriddenOperation.getIdentifier();
                        addDispatchError(type, dispatchOperations, msg, null, OVERRIDE_REDUCES_VISIBILITY);
                    }
                    expectStatic = overriddenOperation.isStatic();
                }
                LightweightTypeReference dispatchMethodReturnType = getActualType(clazz, syntheticDispatchMethod);
                if (dispatchOperations.size() == 1) {
                    JvmOperation singleOp = dispatchOperations.iterator().next();
                    XtendFunction function = associations.getXtendFunction(singleOp);
                    addIssue("Single dispatch method.", function, XTEND_MEMBER__MODIFIERS, function.getModifiers().indexOf("dispatch"), SINGLE_DISPATCH_FUNCTION);
                } else {
                    Multimap<List<JvmType>, JvmOperation> signatures = HashMultimap.create();
                    boolean[] allPrimitive = new boolean[signature.getArity()];
                    Arrays.fill(allPrimitive, true);
                    boolean isFirstLocalOperation = true;
                    JvmVisibility commonVisibility = null;
                    Boolean commonStatic = null;
                    for (JvmOperation jvmOperation : dispatchOperations) {
                        signatures.put(getParamTypes(jvmOperation, true), jvmOperation);
                        for (int i = 0; i < jvmOperation.getParameters().size(); i++) {
                            JvmFormalParameter parameter = jvmOperation.getParameters().get(i);
                            if (!(parameter.getParameterType().getType() instanceof JvmPrimitiveType)) {
                                allPrimitive[i] = false;
                            }
                        }
                        if (jvmOperation.getDeclaringType() == type) {
                            if (expectStatic != null) {
                                if (expectStatic && !jvmOperation.isStatic()) {
                                    String msg = "The dispatch method must be static because the dispatch methods in the superclass are static.";
                                    addDispatchError(jvmOperation, msg, "static", DISPATCH_FUNCTIONS_STATIC_EXPECTED);
                                }
                                if (!expectStatic && jvmOperation.isStatic()) {
                                    String msg = "The dispatch method must not be static because the dispatch methods in the superclass are not static.";
                                    addDispatchError(jvmOperation, msg, "static", DISPATCH_FUNCTIONS_NON_STATIC_EXPECTED);
                                }
                            }
                            if (isFirstLocalOperation) {
                                commonVisibility = jvmOperation.getVisibility();
                                commonStatic = jvmOperation.isStatic();
                                isFirstLocalOperation = false;
                            } else {
                                if (jvmOperation.getVisibility() != commonVisibility) {
                                    commonVisibility = null;
                                }
                                if (commonStatic != null && commonStatic != jvmOperation.isStatic()) {
                                    commonStatic = null;
                                }
                            }
                            // TODO move validation to type computation
                            if (dispatchMethodReturnType != null) {
                                XtendFunction function = associations.getXtendFunction(jvmOperation);
                                if (function != null) {
                                    LightweightTypeReference operationType = getActualType(function.getExpression(), jvmOperation);
                                    if (!dispatchMethodReturnType.isAssignableFrom(operationType)) {
                                        error("Incompatible return type of dispatch method. Expected " + dispatchMethodReturnType.getHumanReadableName() + " but was " + operationType.getHumanReadableName(), function, XtendPackage.Literals.XTEND_FUNCTION__RETURN_TYPE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INCOMPATIBLE_RETURN_TYPE);
                                    }
                                }
                            }
                        }
                    }
                    if (commonVisibility == null) {
                        addDispatchError(type, dispatchOperations, "All local dispatch methods must have the same visibility.", null, DISPATCH_FUNCTIONS_WITH_DIFFERENT_VISIBILITY);
                    }
                    if (expectStatic == null && commonStatic == null) {
                        addDispatchError(type, dispatchOperations, "Static and non-static dispatch methods can not be mixed.", "static", DISPATCH_FUNCTIONS_MIXED_STATIC_AND_NON_STATIC);
                    }
                    for (final List<JvmType> paramTypes : signatures.keySet()) {
                        Collection<JvmOperation> ops = signatures.get(paramTypes);
                        if (ops.size() > 1) {
                            if (Iterables.any(ops, new Predicate<JvmOperation>() {

                                @Override
                                public boolean apply(JvmOperation input) {
                                    return !getParamTypes(input, false).equals(paramTypes);
                                }
                            })) {
                                for (JvmOperation jvmOperation : ops) {
                                    XtendFunction function = associations.getXtendFunction(jvmOperation);
                                    error("Duplicate dispatch methods. Primitives cannot overload their wrapper types in dispatch methods.", function, null, DUPLICATE_METHOD);
                                }
                            }
                        }
                    }
                    for (int i = 0; i < allPrimitive.length; i++) {
                        if (allPrimitive[i]) {
                            Iterator<JvmOperation> operationIter = dispatchOperations.iterator();
                            JvmType paramType1 = operationIter.next().getParameters().get(i).getParameterType().getType();
                            while (operationIter.hasNext()) {
                                JvmType paramType2 = operationIter.next().getParameters().get(i).getParameterType().getType();
                                if (!paramType2.equals(paramType1)) {
                                    for (JvmOperation jvmOperation : dispatchOperations) {
                                        XtendFunction function = associations.getXtendFunction(jvmOperation);
                                        addIssue("Dispatch methods have arguments with different primitive types.", function, XTEND_EXECUTABLE__PARAMETERS, i, DISPATCH_FUNCTIONS_DIFFERENT_PRIMITIVE_ARGS);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) RichString(org.eclipse.xtend.core.xtend.RichString) JvmType(org.eclipse.xtext.common.types.JvmType) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmPrimitiveType(org.eclipse.xtext.common.types.JvmPrimitiveType) JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) DispatchHelper(org.eclipse.xtend.core.jvmmodel.DispatchHelper) Check(org.eclipse.xtext.validation.Check)

Example 4 with JvmVisibility

use of org.eclipse.xtext.common.types.JvmVisibility in project xtext-xtend by eclipse.

the class ExtractMethodUserInputPage method createAccessModifierSection.

protected void createAccessModifierSection(Composite result) {
    GridLayout layout;
    Label label = new Label(result, SWT.NONE);
    label.setText("Access modifier:");
    Composite group = new Composite(result, SWT.NONE);
    group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    layout = new GridLayout();
    layout.numColumns = 3;
    layout.marginWidth = 0;
    group.setLayout(layout);
    String[] labels = new String[] { "public", "protected", "private" };
    JvmVisibility[] data = new JvmVisibility[] { JvmVisibility.PUBLIC, JvmVisibility.PROTECTED, JvmVisibility.PRIVATE };
    JvmVisibility visibility = refactoring.getVisibility();
    for (int i = 0; i < labels.length; i++) {
        Button radio = new Button(group, SWT.RADIO);
        radio.setText(labels[i]);
        radio.setData(data[i]);
        if (data[i].equals(visibility))
            radio.setSelection(true);
        radio.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent event) {
                final JvmVisibility selectedModifier = (JvmVisibility) event.widget.getData();
                visibilityModified(selectedModifier);
                updatePreview();
            }
        });
    }
}
Also used : Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility)

Example 5 with JvmVisibility

use of org.eclipse.xtext.common.types.JvmVisibility in project xtext-xtend by eclipse.

the class AbstractFieldBuilder method getImage.

@Override
public String getImage() {
    String _switchResult = null;
    JvmVisibility _visibility = this.getVisibility();
    if (_visibility != null) {
        switch(_visibility) {
            case PRIVATE:
                _switchResult = "field_private_obj.gif";
                break;
            case PROTECTED:
                _switchResult = "field_protected_obj.gif";
                break;
            case PUBLIC:
                _switchResult = "field_public_obj.gif";
                break;
            default:
                _switchResult = "field_default_obj.gif";
                break;
        }
    } else {
        _switchResult = "field_default_obj.gif";
    }
    return _switchResult;
}
Also used : JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility)

Aggregations

JvmVisibility (org.eclipse.xtext.common.types.JvmVisibility)11 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)4 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)3 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)2 XtendParameter (org.eclipse.xtend.core.xtend.XtendParameter)2 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)2 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 GridData (org.eclipse.swt.layout.GridData)1 GridLayout (org.eclipse.swt.layout.GridLayout)1 Button (org.eclipse.swt.widgets.Button)1 Composite (org.eclipse.swt.widgets.Composite)1 Label (org.eclipse.swt.widgets.Label)1 DispatchHelper (org.eclipse.xtend.core.jvmmodel.DispatchHelper)1 CreateExtensionInfo (org.eclipse.xtend.core.xtend.CreateExtensionInfo)1 RichString (org.eclipse.xtend.core.xtend.RichString)1 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)1