com.squareup.javapoet.NameAllocator Java Examples

The following examples show how to use com.squareup.javapoet.NameAllocator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AutoParcelProcessor.java    From auto-parcel with Apache License 2.0 6 votes vote down vote up
private ImmutableMap<TypeMirror, FieldSpec> getTypeAdapters(ImmutableList<Property> properties) {
    Map<TypeMirror, FieldSpec> typeAdapters = new LinkedHashMap<>();
    NameAllocator nameAllocator = new NameAllocator();
    nameAllocator.newName("CREATOR");
    for (Property property : properties) {
        if (property.typeAdapter != null && !typeAdapters.containsKey(property.typeAdapter)) {
            ClassName typeName = (ClassName) TypeName.get(property.typeAdapter);
            String name = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, typeName.simpleName());
            name = nameAllocator.newName(name, typeName);

            typeAdapters.put(property.typeAdapter, FieldSpec.builder(
                    typeName, NameAllocator.toJavaIdentifier(name), PRIVATE, STATIC, FINAL)
                    .initializer("new $T()", typeName).build());
        }
    }
    return ImmutableMap.copyOf(typeAdapters);
}
 
Example #2
Source File: AutoValueCursorExtension.java    From auto-value-cursor with Apache License 2.0 5 votes vote down vote up
public static ImmutableMap<ClassName, String> addColumnAdaptersToMethod(
        MethodSpec.Builder method,
        List<ColumnProperty> properties) {
    Map<ClassName, String> columnAdapters = new LinkedHashMap<>();
    NameAllocator nameAllocator = new NameAllocator();
    for (ColumnProperty property : properties) {
        ClassName adapter = property.columnAdapter();
        if (adapter != null && !columnAdapters.containsKey(adapter)) {
            String name = nameAllocator.newName(toLowerCase(adapter.simpleName()));
            method.addStatement("$1T $2L = new $1T()", adapter, name);
            columnAdapters.put(adapter, name);
        }
    }
    return ImmutableMap.copyOf(columnAdapters);
}
 
Example #3
Source File: ParameterList.java    From OnActivityResult with Apache License 2.0 5 votes vote down vote up
public String toString(final NameAllocator nameAllocator) {
    final StringBuilder stringBuilder = new StringBuilder();

    if (list.size() > 0) {
        for (final Parameter parameter : list) {
            stringBuilder.append(nameAllocator.get(parameter.hashCode())).append(", ");
        }

        stringBuilder.setLength(stringBuilder.length() - 2);
    }

    return stringBuilder.toString();
}
 
Example #4
Source File: ActivityResultClass.java    From OnActivityResult with Apache License 2.0 5 votes vote down vote up
private void addMethodCalls(final MethodSpec.Builder result, final Set<Parameter> existingParameters, final List<MethodCall> methodCalls) {
    final NameAllocator nameAllocator = new NameAllocator();

    for (final MethodCall methodCall : methodCalls) {
        final ParameterList parameterList = methodCall.getParameterList();

        this.addNecessaryParameterVariables(nameAllocator, result, existingParameters, parameterList);

        result.addStatement("$L.$L($L)", TARGET_VARIABLE_NAME, methodCall.getMethodName(), parameterList.toString(nameAllocator));
    }
}
 
Example #5
Source File: ActivityResultClass.java    From OnActivityResult with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("PMD.CyclomaticComplexity") private void addNecessaryParameterVariables(final NameAllocator nameAllocator, final MethodSpec.Builder result, final Set<Parameter> existingParameters, final ParameterList parameterList) {
    for (final Parameter parameter : parameterList) {
        final boolean isNotPresent = !existingParameters.contains(parameter);

        if (isNotPresent) {
            String parameterName;

            try {
                parameterName = nameAllocator.get(parameter.hashCode()); // The only way to know whether a name has already been generated for that hashCode or not
            } catch (final IllegalArgumentException ignore) {
                parameterName = nameAllocator.newName(parameter.getName(), parameter.hashCode());
            }

            if (AnnotatedParameter.INTENT_DATA == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getIntentData$L($L)", AnnotatedParameter.INTENT_DATA.type, parameterName, INTENT_HELPER, parameter.preCondition.getSuffix(), Parameter.INTENT);
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.BOOLEAN == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraBoolean($L, $S, $L)", AnnotatedParameter.BOOLEAN.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.BYTE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraByte($L, $S, (byte) $L)", AnnotatedParameter.BYTE.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.CHAR == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraChar($L, $S, (char) $L)", AnnotatedParameter.CHAR.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.DOUBLE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraDouble($L, $S, $L)", AnnotatedParameter.DOUBLE.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.FLOAT == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraFloat($L, $S, $Lf)", AnnotatedParameter.FLOAT.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.INT == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraInt($L, $S, $L)", AnnotatedParameter.INT.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.LONG == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraLong($L, $S, $LL)", AnnotatedParameter.LONG.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.SHORT == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraShort($L, $S, (short) $L)", AnnotatedParameter.SHORT.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.STRING == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraString($L, $S, $S)", AnnotatedParameter.STRING.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.CHAR_SEQUENCE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraCharSequence($L, $S, $L)", AnnotatedParameter.CHAR_SEQUENCE.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.BUNDLE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraBundle($L, $S, $L)", AnnotatedParameter.BUNDLE.type, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.SERIALIZABLE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraSerializable($L, $S, $L)", parameter.className, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            } else if (AnnotatedParameter.PARCELABLE == parameter.annotatedParameter) {
                result.addStatement("final $T $L = $T.getExtraParcelable($L, $S, $L)", parameter.className, parameterName, INTENT_HELPER, Parameter.INTENT, parameter.getKey(), parameter.getDefaultValue());
                existingParameters.add(parameter);
            }
        }
    }
}