Search in sources :

Example 1 with Parameterizable

use of javax.lang.model.element.Parameterizable in project immutables by immutables.

the class FactoryMethodAttributesCollector method collect.

void collect() {
    ExecutableElement factoryMethodElement = (ExecutableElement) protoclass.sourceElement();
    Parameterizable element = (Parameterizable) (factoryMethodElement.getKind() == ElementKind.CONSTRUCTOR ? factoryMethodElement.getEnclosingElement() : type.element);
    for (VariableElement parameter : factoryMethodElement.getParameters()) {
        TypeMirror returnType = parameter.asType();
        ValueAttribute attribute = new ValueAttribute();
        attribute.isGenerateAbstract = true;
        attribute.reporter = reporter;
        attribute.returnType = returnType;
        attribute.element = parameter;
        String parameterName = parameter.getSimpleName().toString();
        attribute.names = styles.forAccessorWithRaw(parameterName, parameterName);
        attribute.containingType = type;
        attributes.add(attribute);
    }
    Instantiator encodingInstantiator = protoclass.encodingInstantiator();
    @Nullable InstantiationCreator instantiationCreator = encodingInstantiator.creatorFor(element);
    for (ValueAttribute attribute : attributes) {
        attribute.initAndValidate(instantiationCreator);
    }
    if (instantiationCreator != null) {
        type.additionalImports(instantiationCreator.imports);
    }
    type.attributes.addAll(attributes);
    type.throwing = extractThrowsClause(factoryMethodElement);
}
Also used : InstantiationCreator(org.immutables.value.processor.encode.Instantiator.InstantiationCreator) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) Parameterizable(javax.lang.model.element.Parameterizable) Instantiator(org.immutables.value.processor.encode.Instantiator) VariableElement(javax.lang.model.element.VariableElement) Nullable(javax.annotation.Nullable)

Example 2 with Parameterizable

use of javax.lang.model.element.Parameterizable in project immutables by immutables.

the class Generics method computeParameters.

private static Parameter[] computeParameters(final Protoclass protoclass, final Element element) {
    if (!(element instanceof Parameterizable)) {
        return NO_PARAMETERS;
    }
    final List<? extends TypeParameterElement> typeParameters = ((Parameterizable) element).getTypeParameters();
    if (typeParameters.isEmpty()) {
        return NO_PARAMETERS;
    }
    class Creator {

        final String[] vars = collectVars(typeParameters);

        final DeclaringType declaringType = protoclass.environment().round().inferDeclaringTypeFor(element);

        Parameter[] create() {
            final Parameter[] parameters = new Parameter[typeParameters.size()];
            int i = 0;
            for (TypeParameterElement e : typeParameters) {
                parameters[i] = new Parameter(i, e.getSimpleName().toString(), boundsFrom(e));
                i++;
            }
            return parameters;
        }

        String[] boundsFrom(TypeParameterElement e) {
            List<? extends TypeMirror> boundMirrors = e.getBounds();
            if (boundMirrors.isEmpty()) {
                return NO_STRINGS;
            }
            String[] bounds = new String[boundMirrors.size()];
            int c = 0;
            for (TypeMirror m : boundMirrors) {
                TypeStringProvider provider = newProvider(m);
                provider.process();
                bounds[c++] = provider.returnTypeName();
            }
            if (bounds.length == 1 && bounds[0].equals(Object.class.getName())) {
                return NO_STRINGS;
            }
            return bounds;
        }

        TypeStringProvider newProvider(TypeMirror type) {
            return new TypeStringProvider(protoclass.report(), element, type, new ImportsTypeStringResolver(declaringType, declaringType), vars, null);
        }
    }
    return new Creator().create();
}
Also used : TypeParameterElement(javax.lang.model.element.TypeParameterElement) TypeMirror(javax.lang.model.type.TypeMirror) Parameterizable(javax.lang.model.element.Parameterizable) DeclaringType(org.immutables.value.processor.meta.Proto.DeclaringType)

Example 3 with Parameterizable

use of javax.lang.model.element.Parameterizable in project revapi by revapi.

the class FormalTypeParametersChanged method doVisit.

private void doVisit(@Nullable JavaModelElement oldElement, @Nullable JavaModelElement newElement) {
    if (!isBothAccessible(oldElement, newElement)) {
        return;
    }
    assert oldElement != null;
    assert newElement != null;
    Parameterizable oldEl = (Parameterizable) oldElement.getDeclaringElement();
    Parameterizable newEl = (Parameterizable) newElement.getDeclaringElement();
    List<? extends TypeParameterElement> oldPars = oldEl.getTypeParameters();
    List<? extends TypeParameterElement> newPars = newEl.getTypeParameters();
    if (oldPars.size() == 0 && oldPars.size() == newPars.size()) {
        return;
    }
    List<TypeParameterElement> added = new ArrayList<>();
    List<TypeParameterElement> removed = new ArrayList<>();
    Map<TypeParameterElement, TypeParameterElement> changed = new LinkedHashMap<>();
    Iterator<? extends TypeParameterElement> oldIt = oldPars.iterator();
    Iterator<? extends TypeParameterElement> newIt = newPars.iterator();
    while (oldIt.hasNext() && newIt.hasNext()) {
        TypeParameterElement oldT = oldIt.next();
        TypeParameterElement newT = newIt.next();
        String oldS = Util.toUniqueString(oldT.asType());
        String newS = Util.toUniqueString(newT.asType());
        if (!oldS.equals(newS)) {
            changed.put(oldT, newT);
        }
    }
    while (oldIt.hasNext()) {
        removed.add(oldIt.next());
    }
    while (newIt.hasNext()) {
        added.add(newIt.next());
    }
    if (!added.isEmpty() || !removed.isEmpty() || !changed.isEmpty()) {
        pushActive(oldElement, newElement, added, removed, changed);
    }
}
Also used : ArrayList(java.util.ArrayList) Parameterizable(javax.lang.model.element.Parameterizable) TypeParameterElement(javax.lang.model.element.TypeParameterElement) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with Parameterizable

use of javax.lang.model.element.Parameterizable in project revapi by revapi.

the class FormalTypeParametersChanged method doEnd.

@Nullable
@Override
protected List<Difference> doEnd() {
    ActiveElements<JavaModelElement> els = popIfActive();
    if (els == null) {
        return null;
    }
    @SuppressWarnings("unchecked") List<TypeParameterElement> added = (List<TypeParameterElement>) els.context[0];
    @SuppressWarnings("unchecked") List<TypeParameterElement> removed = (List<TypeParameterElement>) els.context[1];
    @SuppressWarnings("unchecked") Map<TypeParameterElement, TypeParameterElement> changed = (Map<TypeParameterElement, TypeParameterElement>) els.context[2];
    Parameterizable oldT = (Parameterizable) els.oldElement.getDeclaringElement();
    List<Difference> diffs = new ArrayList<>();
    if (oldT.getTypeParameters().isEmpty()) {
        diffs.add(createDifference(Code.GENERICS_ELEMENT_NOW_PARAMETERIZED, Code.attachmentsFor(els.oldElement, els.newElement)));
    }
    for (TypeParameterElement e : added) {
        diffs.add(createDifferenceWithExplicitParams(Code.GENERICS_FORMAL_TYPE_PARAMETER_ADDED, Code.attachmentsFor(els.oldElement, els.newElement), Util.toHumanReadableString(e)));
    }
    for (TypeParameterElement e : removed) {
        diffs.add(createDifferenceWithExplicitParams(Code.GENERICS_FORMAL_TYPE_PARAMETER_REMOVED, Code.attachmentsFor(els.oldElement, els.newElement), Util.toHumanReadableString(e)));
    }
    for (Map.Entry<TypeParameterElement, TypeParameterElement> e : changed.entrySet()) {
        String oldP = Util.toHumanReadableString(e.getKey());
        String newP = Util.toHumanReadableString(e.getValue());
        diffs.add(createDifferenceWithExplicitParams(Code.GENERICS_FORMAL_TYPE_PARAMETER_CHANGED, Code.attachmentsFor(els.oldElement, els.newElement, "oldTypeParameter", oldP, "newTypeParameter", newP), oldP, newP));
    }
    return diffs;
}
Also used : JavaModelElement(org.revapi.java.spi.JavaModelElement) ArrayList(java.util.ArrayList) Difference(org.revapi.Difference) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Parameterizable(javax.lang.model.element.Parameterizable) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Nullable(javax.annotation.Nullable)

Aggregations

Parameterizable (javax.lang.model.element.Parameterizable)4 TypeParameterElement (javax.lang.model.element.TypeParameterElement)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Nullable (javax.annotation.Nullable)2 TypeMirror (javax.lang.model.type.TypeMirror)2 List (java.util.List)1 Map (java.util.Map)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 VariableElement (javax.lang.model.element.VariableElement)1 Instantiator (org.immutables.value.processor.encode.Instantiator)1 InstantiationCreator (org.immutables.value.processor.encode.Instantiator.InstantiationCreator)1 DeclaringType (org.immutables.value.processor.meta.Proto.DeclaringType)1 Difference (org.revapi.Difference)1 JavaModelElement (org.revapi.java.spi.JavaModelElement)1