Search in sources :

Example 86 with JvmDeclaredType

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

the class XbaseReferenceUpdater method createTextChange.

@Override
protected void createTextChange(ITextRegion referenceTextRegion, String newReferenceText, EObject referringElement, EObject newTargetElement, EReference reference, URI referringResourceURI, IRefactoringUpdateAcceptor updateAcceptor) {
    if (newReferenceText != null && updateAcceptor instanceof ImportAwareUpdateAcceptor && isReferenceToJvmType(newTargetElement, reference)) {
        JvmType newTargetType = (JvmType) newTargetElement;
        ImportAwareUpdateAcceptor importAwareUpdateAcceptor = (ImportAwareUpdateAcceptor) updateAcceptor;
        Pair<JvmDeclaredType, QualifiedName> importedTypeAndRelativeName = getImportedTypeAndRelativeName(newTargetType, importAwareUpdateAcceptor.getImportSection());
        if (importedTypeAndRelativeName != null) {
            JvmDeclaredType importedType = importedTypeAndRelativeName.getFirst();
            QualifiedName importRelativeName = importedTypeAndRelativeName.getSecond();
            importAwareUpdateAcceptor.removeImport(importedType, false, false, null);
            if (isReferencedByQualifiedName(referringElement, newTargetType, importRelativeName)) {
                QualifiedName newTypeQualifiedName = qualifiedNameProvider.getFullyQualifiedName(newTargetElement);
                newReferenceText = getLinkText(newTypeQualifiedName, newReferenceText);
            } else {
                if (!isEmpty(importedType.getPackageName())) {
                    importAwareUpdateAcceptor.acceptImport(importedType, false, false, null);
                }
                newReferenceText = getLinkText(importRelativeName, newReferenceText);
            }
        }
    }
    super.createTextChange(referenceTextRegion, newReferenceText, referringElement, newTargetElement, reference, referringResourceURI, updateAcceptor);
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 87 with JvmDeclaredType

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

the class XbaseHoverDocumentationProvider method handleLink.

protected void handleLink(List<?> fragments) {
    if (fragments == null || fragments.isEmpty()) {
        return;
    }
    URI elementURI = null;
    String firstFragment = fragments.get(0).toString();
    int hashIndex = firstFragment.indexOf("#");
    if (hashIndex != -1) {
        JvmDeclaredType resolvedDeclarator = getResolvedDeclarator(firstFragment.substring(0, hashIndex));
        if (resolvedDeclarator != null && !resolvedDeclarator.eIsProxy()) {
            String signature = firstFragment.substring(hashIndex + 1);
            int indexOfOpenBracket = signature.indexOf("(");
            int indexOfClosingBracket = signature.indexOf(")");
            String simpleNameOfFeature = indexOfOpenBracket != -1 ? signature.substring(0, indexOfOpenBracket) : signature;
            Iterable<JvmFeature> possibleCandidates = resolvedDeclarator.findAllFeaturesByName(simpleNameOfFeature);
            List<String> parameterNames = null;
            if (indexOfOpenBracket != -1 && indexOfClosingBracket != -1) {
                parameterNames = Strings.split(signature.substring(indexOfOpenBracket + 1, indexOfClosingBracket), ",");
            }
            Iterator<JvmFeature> featureIterator = possibleCandidates.iterator();
            while (elementURI == null && featureIterator.hasNext()) {
                JvmFeature feature = featureIterator.next();
                boolean valid = false;
                if (feature instanceof JvmField) {
                    valid = true;
                } else if (feature instanceof JvmExecutable) {
                    JvmExecutable executable = (JvmExecutable) feature;
                    EList<JvmFormalParameter> parameters = executable.getParameters();
                    if (parameterNames == null) {
                        valid = true;
                    } else if (parameters.size() == parameterNames.size()) {
                        valid = true;
                        for (int i = 0; (i < parameterNames.size() && valid); i++) {
                            URI parameterTypeURI = EcoreUtil.getURI(parameters.get(i).getParameterType().getType());
                            IEObjectDescription expectedParameter = scopeProvider.getScope(context, new HoverReference(TypesPackage.Literals.JVM_TYPE)).getSingleElement(qualifiedNameConverter.toQualifiedName(parameterNames.get(i)));
                            if (expectedParameter == null || !expectedParameter.getEObjectURI().equals(parameterTypeURI)) {
                                valid = false;
                            }
                        }
                    }
                }
                if (valid)
                    elementURI = EcoreUtil.getURI(feature);
            }
        }
    } else {
        IScope scope = scopeProvider.getScope(context, new HoverReference(TypesPackage.Literals.JVM_TYPE));
        IEObjectDescription singleElement = scope.getSingleElement(qualifiedNameConverter.toQualifiedName(firstFragment));
        if (singleElement != null)
            elementURI = singleElement.getEObjectURI();
    }
    String label = "";
    if (fragments.size() > 1) {
        for (int i = 1; i < fragments.size(); i++) {
            String portentialLabel = fragments.get(i).toString();
            if (portentialLabel.trim().length() > 0)
                label += portentialLabel;
        }
    }
    if (label.length() == 0)
        label = firstFragment;
    if (elementURI == null)
        buffer.append(label);
    else {
        buffer.append(createLinkWithLabel(XtextElementLinks.XTEXTDOC_SCHEME, elementURI, label));
    }
}
Also used : JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) EList(org.eclipse.emf.common.util.EList) JvmFeature(org.eclipse.xtext.common.types.JvmFeature) IScope(org.eclipse.xtext.scoping.IScope) JvmField(org.eclipse.xtext.common.types.JvmField)

Example 88 with JvmDeclaredType

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

the class XbaseUIValidator method checkRestrictedType.

protected void checkRestrictedType(final EObject context, final EStructuralFeature feature, final JvmDeclaredType typeToCheck) {
    @SuppressWarnings("unchecked") Map<JvmDeclaredType, RestrictionKind> validationContext = (Map<JvmDeclaredType, RestrictionKind>) getContext().get(RestrictionKind.class);
    if (validationContext == null) {
        validationContext = Maps.newHashMap();
        getContext().put(RestrictionKind.class, validationContext);
    }
    RestrictionKind restriction = validationContext.get(typeToCheck);
    IJavaProject javaProject = null;
    if (restriction == null) {
        final IJavaElement javaElement = javaElementFinder.findElementFor(typeToCheck);
        if (javaElement == null || !(javaElement instanceof IType)) {
            validationContext.put(typeToCheck, RestrictionKind.VALID);
            return;
        }
        javaProject = javaElement.getJavaProject();
        restriction = computeRestriction(projectProvider.getJavaProject(context.eResource().getResourceSet()), (IType) javaElement);
        validationContext.put(typeToCheck, restriction);
    }
    if (restriction == RestrictionKind.FORBIDDEN) {
        if (javaProject == null)
            javaProject = projectProvider.getJavaProject(context.eResource().getResourceSet());
        addIssue("Access restriction: The type " + typeToCheck.getSimpleName() + " is not accessible due to restriction on required project " + javaProject.getElementName(), context, feature, IssueCodes.FORBIDDEN_REFERENCE);
    } else if (restriction == RestrictionKind.DISCOURAGED) {
        if (javaProject == null)
            javaProject = projectProvider.getJavaProject(context.eResource().getResourceSet());
        addIssue("Discouraged access: The type " + typeToCheck.getSimpleName() + " is not accessible due to restriction on required project " + javaProject.getElementName(), context, feature, IssueCodes.DISCOURAGED_REFERENCE);
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) Map(java.util.Map) IType(org.eclipse.jdt.core.IType)

Example 89 with JvmDeclaredType

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

the class AbstractTypeProviderTest method testInnerAnnotationType.

@Test
public void testInnerAnnotationType() throws Exception {
    JvmDeclaredType declaredType = (JvmDeclaredType) getTypeProvider().findTypeByName(TypeWithInnerAnnotation.class.getName());
    assertEquals(2, declaredType.getMembers().size());
    // default constructor
    assertTrue(Iterables.any(declaredType.getMembers(), new Predicate<JvmMember>() {

        @Override
        public boolean apply(JvmMember input) {
            return (input instanceof JvmConstructor) && input.getSimpleName().equals(TypeWithInnerAnnotation.class.getSimpleName());
        }
    }));
    // inner annotation type
    assertTrue(Iterables.any(declaredType.getMembers(), new Predicate<JvmMember>() {

        @Override
        public boolean apply(JvmMember input) {
            return (input instanceof JvmAnnotationType) && input.getIdentifier().equals(TypeWithInnerAnnotation.MyAnnotation.class.getName()) && input.getVisibility() == JvmVisibility.PUBLIC;
        }
    }));
}
Also used : JvmAnnotationType(org.eclipse.xtext.common.types.JvmAnnotationType) JvmConstructor(org.eclipse.xtext.common.types.JvmConstructor) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmMember(org.eclipse.xtext.common.types.JvmMember) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Example 90 with JvmDeclaredType

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

the class AbstractTypeProviderTest method getMethodParameterAnnotationValue.

public JvmAnnotationValue getMethodParameterAnnotationValue(String name, boolean defaultValue) {
    String typeName = TestAnnotation.Annotated.class.getName();
    JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
    JvmOperation method = getMethodFromType(type, TestAnnotation.Annotated.class, "method(java.lang.String,java.lang.String,java.lang.String)");
    JvmAnnotationTarget target = method.getParameters().get(0);
    JvmAnnotationValue result = getDefaultOrExplicitAnnotationValue(name, target);
    if (defaultValue) {
        if (isDefaultValueSupported()) {
            assertTrue(result.eContainer() instanceof JvmOperation);
        } else {
            assertFalse(result.eContainer() instanceof JvmOperation);
        }
    } else {
        assertFalse(result.eContainer() instanceof JvmOperation);
    }
    return result;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmAnnotationTarget(org.eclipse.xtext.common.types.JvmAnnotationTarget) TestAnnotation(org.eclipse.xtext.common.types.testSetups.TestAnnotation) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmAnnotationValue(org.eclipse.xtext.common.types.JvmAnnotationValue)

Aggregations

JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)138 Test (org.junit.Test)41 EObject (org.eclipse.emf.ecore.EObject)26 JvmType (org.eclipse.xtext.common.types.JvmType)24 JvmMember (org.eclipse.xtext.common.types.JvmMember)21 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)18 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)18 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)16 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)16 JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)15 JvmAnnotationType (org.eclipse.xtext.common.types.JvmAnnotationType)13 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)11 JvmAnnotationTarget (org.eclipse.xtext.common.types.JvmAnnotationTarget)10 TestAnnotation (org.eclipse.xtext.common.types.testSetups.TestAnnotation)10 Resource (org.eclipse.emf.ecore.resource.Resource)8 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)8 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)7 JvmArrayType (org.eclipse.xtext.common.types.JvmArrayType)7 JvmParameterizedTypeReference (org.eclipse.xtext.common.types.JvmParameterizedTypeReference)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)6