Search in sources :

Example 1 with JvmDeclaredType

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

the class StratumBreakpointAdapterFactory method getClassNamePattern.

protected String getClassNamePattern(XtextResource state) {
    TreeIterator<Object> contents = EcoreUtil.getAllContents(state, false);
    StringBuilder sb = new StringBuilder();
    while (contents.hasNext()) {
        Object next = contents.next();
        if (next instanceof JvmDeclaredType) {
            JvmDeclaredType type = (JvmDeclaredType) next;
            sb.append(type.getQualifiedName()).append("*");
            sb.append(",");
        }
    }
    if (sb.length() == 0)
        return null;
    else
        return sb.substring(0, sb.length() - 1);
}
Also used : JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType)

Example 2 with JvmDeclaredType

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

the class JdtTypeProvider method findTypeBySignature.

private JvmType findTypeBySignature(String signature, TypeResource resource, boolean traverseNestedTypes) {
    // TODO: Maybe iterate the resource without computing a fragment
    String fragment = typeUriHelper.getFragment(signature);
    JvmType result = (JvmType) resource.getEObject(fragment);
    if (result != null || !traverseNestedTypes)
        return result;
    List<EObject> contents = resource.getContents();
    if (contents.isEmpty()) {
        return null;
    }
    String rootTypeName = resource.getURI().segment(1);
    String nestedTypeName = fragment.substring(rootTypeName.length() + 1);
    List<String> segments = Strings.split(nestedTypeName, "$");
    EObject rootType = contents.get(0);
    if (rootType instanceof JvmDeclaredType) {
        result = findNestedType((JvmDeclaredType) rootType, segments, 0);
    }
    return result;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 3 with JvmDeclaredType

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

the class JvmMemberRenameStrategy method setName.

protected JvmMember setName(URI targetURI, ResourceSet resourceSet, final String newName) {
    final JvmMember member = (JvmMember) resourceSet.getEObject(targetURI, true);
    // clear all cached identifiers of contained members
    for (JvmMember containedMember : EcoreUtil2.eAllOfType(member, JvmMember.class)) containedMember.internalSetIdentifier(null);
    member.setSimpleName(newName);
    if (member instanceof JvmDeclaredType && ((InternalEObject) member).eDirectResource() != null) {
        Resource typeResource = member.eResource();
        if (typeResource instanceof TypeResource) {
            // rename the resource
            String originalURI = typeResource.getURI().toString();
            int lastIndexOf = Math.max(originalURI.lastIndexOf('.'), originalURI.lastIndexOf('/')) + 1;
            URI typeResourceNewURI = URI.createURI(originalURI.substring(0, lastIndexOf) + newName);
            typeResource.setURI(typeResourceNewURI);
            // disconnect the mirrored IJavaElement as it is invalid now
            ((TypeResource) typeResource).setMirror(new AbstractClassMirror() {

                @Override
                public boolean isSealed() {
                    return false;
                }

                @Override
                public void initialize(TypeResource typeResource) {
                }

                @Override
                protected String getTypeName() {
                    return member.getIdentifier();
                }
            });
        }
    }
    return member;
}
Also used : TypeResource(org.eclipse.xtext.common.types.access.TypeResource) AbstractClassMirror(org.eclipse.xtext.common.types.access.impl.AbstractClassMirror) Resource(org.eclipse.emf.ecore.resource.Resource) TypeResource(org.eclipse.xtext.common.types.access.TypeResource) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmMember(org.eclipse.xtext.common.types.JvmMember) URI(org.eclipse.emf.common.util.URI) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 4 with JvmDeclaredType

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

the class JdtBasedTypeFactory method createType.

/**
 * @since 2.4
 */
protected JvmDeclaredType createType(ITypeBinding typeBinding, String handleIdentifier, List<String> path, StringBuilder fqn) {
    if (typeBinding.isAnonymous() || typeBinding.isSynthetic())
        throw new IllegalStateException("Cannot create type for anonymous or synthetic classes");
    // Creates the right type of instance based on the type of binding.
    // 
    JvmGenericType jvmGenericType;
    JvmDeclaredType result;
    if (typeBinding.isAnnotation()) {
        jvmGenericType = null;
        result = TypesFactory.eINSTANCE.createJvmAnnotationType();
    } else if (typeBinding.isEnum()) {
        jvmGenericType = null;
        result = TypesFactory.eINSTANCE.createJvmEnumerationType();
    } else {
        result = jvmGenericType = TypesFactory.eINSTANCE.createJvmGenericType();
        jvmGenericType.setInterface(typeBinding.isInterface());
    }
    // Populate the information computed from the modifiers.
    // 
    int modifiers = typeBinding.getModifiers();
    setTypeModifiers(result, modifiers);
    result.setDeprecated(typeBinding.isDeprecated());
    setVisibility(result, modifiers);
    // Determine the simple name and compose the fully qualified name and path, remembering the fqn length and path size so we can reset them.
    // 
    String simpleName = typeBinding.getName();
    fqn.append(simpleName);
    int length = fqn.length();
    int size = path.size();
    path.add(simpleName);
    String qualifiedName = fqn.toString();
    result.internalSetIdentifier(qualifiedName);
    result.setSimpleName(simpleName);
    // Traverse the nested types using '$' as the qualified name separator.
    // 
    fqn.append('$');
    createNestedTypes(typeBinding, result, handleIdentifier, path, fqn);
    // Traverse the methods using '.'as the qualifed name separator.
    // 
    fqn.setLength(length);
    fqn.append('.');
    createMethods(typeBinding, handleIdentifier, path, fqn, result);
    createFields(typeBinding, fqn, result);
    // Set the super types.
    // 
    setSuperTypes(typeBinding, qualifiedName, result);
    // 
    if (jvmGenericType != null) {
        ITypeBinding[] typeParameterBindings = typeBinding.getTypeParameters();
        if (typeParameterBindings.length > 0) {
            InternalEList<JvmTypeParameter> typeParameters = (InternalEList<JvmTypeParameter>) jvmGenericType.getTypeParameters();
            for (ITypeBinding variable : typeParameterBindings) {
                typeParameters.addUnique(createTypeParameter(variable, result));
            }
        }
    }
    // Populate the annotation values.
    // 
    createAnnotationValues(typeBinding, result);
    // Restore the path.
    // 
    path.remove(size);
    return result;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) InternalEList(org.eclipse.emf.ecore.util.InternalEList) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint)

Example 5 with JvmDeclaredType

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

the class JdtBasedTypeFactory method createType.

/**
 * @since 2.7
 */
protected JvmDeclaredType createType(IType jdtType, IJavaProject javaProject) {
    if (jdtType.getDeclaringType() != null)
        throw new IllegalArgumentException("Cannot create type from non-toplevel-type: '" + jdtType.getFullyQualifiedName() + "'.");
    IBinding binding = null;
    IllegalArgumentException cause = null;
    try {
        binding = resolveBindings(jdtType, javaProject);
    } catch (IllegalArgumentException e) {
        // might be caused by missing source content in ASTNode#setSourceRange
        cause = e;
    }
    if (binding == null) {
        IJavaProject fallbackProject = jdtType.getJavaProject();
        // fallback to the project of the given jdtType if it is different from the explicitly given project
        if (!fallbackProject.equals(javaProject)) {
            try {
                binding = resolveBindings(jdtType, fallbackProject);
            } catch (IllegalArgumentException e) {
                cause = e;
            }
            if (binding == null) {
                throw new IllegalStateException("Could not create binding for '" + jdtType.getFullyQualifiedName() + "' in context of projects '" + javaProject.getElementName() + "' and '" + fallbackProject.getElementName() + "'.", cause);
            }
        } else {
            throw new IllegalStateException("Could not create binding for '" + jdtType.getFullyQualifiedName() + "' in context of project '" + javaProject.getElementName() + "'.");
        }
    }
    if (binding instanceof ITypeBinding) {
        createType.start();
        ITypeBinding typeBinding = (ITypeBinding) binding;
        setMayTolerateMissingType(typeBinding);
        JvmDeclaredType result = createType(jdtType, typeBinding);
        // Clear the cached information.
        // 
        clearCache();
        createType.stop();
        return result;
    } else {
        throw new IllegalStateException("Expected ITypeBinding for '" + jdtType.getFullyQualifiedName() + "', but got '" + binding.toString() + "'.");
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IBinding(org.eclipse.jdt.core.dom.IBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) 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