Search in sources :

Example 81 with JvmDeclaredType

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

the class SourceBasedJdtTypeProviderTest method testClassAnnotationValue_08.

@Test
public void testClassAnnotationValue_08() throws Exception {
    IJavaProject project = projectProvider.getJavaProject(null);
    String typeName = EmptyAbstractClass.class.getName();
    IFile javaFile = (IFile) project.getProject().findMember(new Path("src/" + typeName.replace('.', '/') + ".java"));
    assertNotNull(javaFile);
    String content = Files.readStreamIntoString(javaFile.getContents());
    try {
        String newContent = content.replace("public abstract ", "@TestAnnotation( classArray = { String.class, DoesNotExist.class, String.class } ) public abstract ");
        javaFile.setContents(new StringInputStream(newContent), IResource.NONE, new NullProgressMonitor());
        JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
        List<JvmAnnotationReference> annotations = type.getAnnotations();
        assertEquals(1, annotations.size());
        JvmAnnotationReference annotation = annotations.get(0);
        assertEquals(1, annotation.getExplicitValues().size());
        JvmAnnotationValue value = annotation.getExplicitValues().get(0);
        assertTrue(value instanceof JvmTypeAnnotationValue);
        List<JvmTypeReference> typeLiterals = ((JvmTypeAnnotationValue) value).getValues();
        assertEquals(2, typeLiterals.size());
    } finally {
        javaFile.setContents(new StringInputStream(content), IResource.NONE, new NullProgressMonitor());
    }
}
Also used : Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JvmTypeAnnotationValue(org.eclipse.xtext.common.types.JvmTypeAnnotationValue) IFile(org.eclipse.core.resources.IFile) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmAnnotationReference(org.eclipse.xtext.common.types.JvmAnnotationReference) JvmAnnotationValue(org.eclipse.xtext.common.types.JvmAnnotationValue) StringInputStream(org.eclipse.xtext.util.StringInputStream) IJavaProject(org.eclipse.jdt.core.IJavaProject) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) Test(org.junit.Test)

Example 82 with JvmDeclaredType

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

the class AbstractTypeProviderPerformanceTest method testLoadTypesAndResolveAllParameterNames.

@Test
public void testLoadTypesAndResolveAllParameterNames() throws Exception {
    for (String name : getClassNamesToLoad()) {
        JvmDeclaredType type = loadAndResolve(name, true, true, true, true, true);
        TreeIterator<Object> iterator = EcoreUtil.getAllContents(type.eResource().getResourceSet(), true);
        while (iterator.hasNext()) {
            Object next = iterator.next();
            if (next instanceof JvmFormalParameter) {
                ((JvmFormalParameter) next).getName();
            }
        }
    }
}
Also used : JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) Test(org.junit.Test)

Example 83 with JvmDeclaredType

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

the class AbstractTypeProviderPerformanceTest method loadAndResolve.

protected JvmDeclaredType loadAndResolve(String name, boolean accessMembers, boolean accessAnnotations, boolean accessTypeParams, boolean accessParameter, boolean accessParameterNames) {
    JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(name);
    EcoreUtil.resolveAll(type.eResource());
    EcoreUtil.resolveAll(type.eResource().getResourceSet());
    Assert.assertNotNull(name, type);
    if (accessAnnotations) {
        type.getAnnotations();
    }
    if (accessMembers) {
        EList<JvmMember> members = type.getMembers();
        for (JvmMember member : members) {
            if (accessAnnotations) {
                member.getAnnotations();
            }
            if (member instanceof JvmExecutable) {
                JvmExecutable operation = (JvmExecutable) member;
                if (accessParameter) {
                    EList<JvmFormalParameter> parameters = operation.getParameters();
                    for (JvmFormalParameter jvmFormalParameter : parameters) {
                        if (accessAnnotations) {
                            jvmFormalParameter.getAnnotations();
                        }
                        if (accessParameterNames) {
                            jvmFormalParameter.getName();
                        }
                    }
                }
            }
        }
    }
    return type;
}
Also used : JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmMember(org.eclipse.xtext.common.types.JvmMember)

Example 84 with JvmDeclaredType

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

the class XbaseReferenceUpdater method resolveNameConflict.

@Override
protected String resolveNameConflict(EObject referringElement, EReference reference, EObject newTargetElement, IRefactoringUpdateAcceptor updateAcceptor) {
    if (EcoreUtil2.isAssignableFrom(TypesPackage.Literals.JVM_IDENTIFIABLE_ELEMENT, reference.getEReferenceType())) {
        if (newTargetElement instanceof JvmType) {
            JvmType type = (JvmType) newTargetElement;
            if (canLinkJvmType(referringElement, type)) {
                return toString(qualifiedNameProvider.getFullyQualifiedName(type));
            }
        }
        if (newTargetElement instanceof JvmFeature) {
            JvmFeature feature = (JvmFeature) newTargetElement;
            if (feature.isStatic() && !isStaticExtensionFeatureCall(referringElement, reference, newTargetElement)) {
                JvmDeclaredType declaringType = feature.getDeclaringType();
                if (canLinkJvmType(referringElement, declaringType)) {
                    JvmDeclaredType parentType = declaringType;
                    List<String> segments = new LinkedList<String>();
                    segments.add(feature.getSimpleName());
                    segments.add(0, parentType.getSimpleName());
                    while (!hasImportedType(updateAcceptor, parentType)) {
                        parentType = parentType.getDeclaringType();
                        if (parentType == null) {
                            return toString(qualifiedNameProvider.getFullyQualifiedName(feature));
                        }
                        segments.add(0, parentType.getSimpleName());
                    }
                    return toString(QualifiedName.create(segments));
                }
            }
        }
    }
    return super.resolveNameConflict(referringElement, reference, newTargetElement, updateAcceptor);
}
Also used : JvmFeature(org.eclipse.xtext.common.types.JvmFeature) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType) LinkedList(java.util.LinkedList)

Example 85 with JvmDeclaredType

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

the class XbaseReferenceUpdater method getImportedTypeAndRelativeName.

/**
 * @return the currently imported type containing the newTargetType and the element's name relative to that import.
 */
protected Pair<JvmDeclaredType, QualifiedName> getImportedTypeAndRelativeName(JvmType newTargetType, RewritableImportSection section) {
    if (!(newTargetType instanceof JvmDeclaredType) || !section.isEnabled()) {
        return null;
    }
    JvmDeclaredType importedType = (JvmDeclaredType) newTargetType;
    StringBuffer relativeName = new StringBuffer(importedType.getSimpleName());
    while (importedType.getDeclaringType() != null && !section.hasImportedType(importedType)) {
        importedType = importedType.getDeclaringType();
        relativeName.insert(0, ".");
        relativeName.insert(0, importedType.getSimpleName());
    }
    return Tuples.create(importedType, qualifiedNameConverter.toQualifiedName(relativeName.toString()));
}
Also used : JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

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