Search in sources :

Example 6 with DeclaringType

use of org.immutables.value.processor.meta.Proto.DeclaringType in project immutables by immutables.

the class Constitution method typeImmutableEnclosingSimpleName.

/**
   * Walks to the enclosing type's simple names and applies naming convention.
   * This shortcut/fix shows deficiency of model (it's probably more complicated than needed).
   * @return enclosing immutable name
   */
@Value.Lazy
String typeImmutableEnclosingSimpleName() {
    DeclaringType declaringType = protoclass().enclosingOf().get();
    String enclosingSimpleName = declaringType.element().getSimpleName().toString();
    String enclosingRawName = names().rawFromAbstract(enclosingSimpleName);
    // Here we checking for having both enclosing and value
    // if we had protoclass it would be kind().isEnclosing() && kind().isValue()
    Naming naming = declaringType.isImmutable() ? names().namings.typeImmutable : names().namings.typeImmutableEnclosing;
    return naming.apply(enclosingRawName);
}
Also used : DeclaringType(org.immutables.value.processor.meta.Proto.DeclaringType) PackageNaming(org.immutables.value.processor.meta.Styles.PackageNaming) Naming(org.immutables.generator.Naming)

Example 7 with DeclaringType

use of org.immutables.value.processor.meta.Proto.DeclaringType 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 8 with DeclaringType

use of org.immutables.value.processor.meta.Proto.DeclaringType in project immutables by immutables.

the class Round method collectValues.

public Multimap<DeclaringPackage, ValueType> collectValues() {
    ImmutableList<Protoclass> protoclasses = collectProtoclasses();
    Map<DeclaringType, ValueType> enclosingTypes = Maps.newHashMap();
    ImmutableMultimap.Builder<DeclaringPackage, ValueType> builder = ImmutableMultimap.builder();
    // Collect enclosing
    for (Protoclass protoclass : protoclasses) {
        if (protoclass.kind().isEnclosing()) {
            ValueType type = composeValue(protoclass);
            enclosingTypes.put(protoclass.declaringType().get(), type);
        }
    }
    // Collect remaining and attach if nested
    for (Protoclass protoclass : protoclasses) {
        @Nullable ValueType current = null;
        if (protoclass.kind().isNested()) {
            @Nullable ValueType enclosing = enclosingTypes.get(protoclass.enclosingOf().get());
            if (enclosing != null) {
                current = composeValue(protoclass);
                // Attach nested to enclosing
                enclosing.addNested(current);
            }
        }
        // getting the ValueType if it was alredy created and put into enclosingTypes
        if (current == null && protoclass.kind().isEnclosing()) {
            current = enclosingTypes.get(protoclass.declaringType().get());
        }
        // If none then we just create it
        if (current == null) {
            current = composeValue(protoclass);
        }
        // We put all enclosing and nested values by the package
        builder.put(protoclass.packageOf(), current);
    }
    return builder.build();
}
Also used : Protoclass(org.immutables.value.processor.meta.Proto.Protoclass) DeclaringType(org.immutables.value.processor.meta.Proto.DeclaringType) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Nullable(javax.annotation.Nullable) DeclaringPackage(org.immutables.value.processor.meta.Proto.DeclaringPackage)

Aggregations

DeclaringType (org.immutables.value.processor.meta.Proto.DeclaringType)8 Protoclass (org.immutables.value.processor.meta.Proto.Protoclass)3 Nullable (javax.annotation.Nullable)2 SourceExtraction (org.immutables.generator.SourceExtraction)2 AbstractDeclaring (org.immutables.value.processor.meta.Proto.AbstractDeclaring)2 ValueType (org.immutables.value.processor.meta.ValueType)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 Collection (java.util.Collection)1 Parameterizable (javax.lang.model.element.Parameterizable)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeParameterElement (javax.lang.model.element.TypeParameterElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Naming (org.immutables.generator.Naming)1 DeclaringPackage (org.immutables.value.processor.meta.Proto.DeclaringPackage)1 PackageNaming (org.immutables.value.processor.meta.Styles.PackageNaming)1