Search in sources :

Example 6 with Expression

use of com.yahoo.aptutils.writer.expressions.Expression in project squidb by yahoo.

the class ViewModelFileWriter method emitPropertyReferenceArrayBody.

private boolean emitPropertyReferenceArrayBody(boolean alias) throws IOException {
    for (PropertyGenerator propertyGenerator : modelSpec.getPropertyGenerators()) {
        Expression reference = Expressions.staticReference(modelSpec.getModelSpecName(), propertyGenerator.getPropertyName());
        if (alias) {
            VariableElement field = propertyGenerator.getField();
            if (field != null) {
                Alias aliasAnnotation = field.getAnnotation(Alias.class);
                if (aliasAnnotation != null && !AptUtils.isEmpty(aliasAnnotation.value().trim())) {
                    reference = reference.callMethod("as", "\"" + aliasAnnotation.value().trim() + "\"");
                }
            }
        }
        writer.writeExpression(reference);
        writer.appendString(",\n");
    }
    return !AptUtils.isEmpty(modelSpec.getPropertyGenerators());
}
Also used : Expression(com.yahoo.aptutils.writer.expressions.Expression) Alias(com.yahoo.squidb.annotations.Alias) PropertyGenerator(com.yahoo.squidb.processor.plugins.defaults.properties.generators.PropertyGenerator) VariableElement(javax.lang.model.element.VariableElement)

Example 7 with Expression

use of com.yahoo.aptutils.writer.expressions.Expression in project squidb by yahoo.

the class ViewModelFileWriter method emitUnaliasedPropertyArray.

private void emitUnaliasedPropertyArray() throws IOException {
    writer.writeComment("--- unaliased property references");
    Expression basePropertiesInit = Expressions.block(new Expression() {

        @Override
        public boolean writeExpression(JavaFileWriter writer) throws IOException {
            return emitPropertyReferenceArrayBody(false);
        }
    }, false, false, false, false);
    writer.writeFieldDeclaration(TypeConstants.PROPERTY_ARRAY, BASE_PROPERTY_ARRAY_NAME, basePropertiesInit, TypeConstants.PRIVATE_STATIC_FINAL).writeNewline();
}
Also used : Expression(com.yahoo.aptutils.writer.expressions.Expression) JavaFileWriter(com.yahoo.aptutils.writer.JavaFileWriter) IOException(java.io.IOException)

Example 8 with Expression

use of com.yahoo.aptutils.writer.expressions.Expression in project squidb by yahoo.

the class ViewModelFileWriter method emitAliasedPropertyArray.

private void emitAliasedPropertyArray() throws IOException {
    writer.writeComment("--- aliased property references");
    Expression aliasedPropertiesInit = Expressions.block(new Expression() {

        @Override
        public boolean writeExpression(JavaFileWriter writer) throws IOException {
            return emitPropertyReferenceArrayBody(true);
        }
    }, false, false, false, false);
    writer.writeFieldDeclaration(TypeConstants.PROPERTY_ARRAY, ALIASED_PROPERTY_ARRAY_NAME, aliasedPropertiesInit, TypeConstants.PUBLIC_STATIC_FINAL).writeNewline();
    writer.beginInitializerBlock(true, true);
    writer.writeStatement(Expressions.callMethod("validateAliasedProperties", ALIASED_PROPERTY_ARRAY_NAME));
    writer.finishInitializerBlock(false, true);
    writer.writeNewline();
}
Also used : Expression(com.yahoo.aptutils.writer.expressions.Expression) JavaFileWriter(com.yahoo.aptutils.writer.JavaFileWriter) IOException(java.io.IOException)

Example 9 with Expression

use of com.yahoo.aptutils.writer.expressions.Expression in project squidb by yahoo.

the class ViewModelFileWriter method emitSinglePropertyDeclaration.

private void emitSinglePropertyDeclaration(PropertyGenerator generator, int index) throws IOException {
    modelSpec.getPluginBundle().beforeEmitPropertyDeclaration(writer, generator);
    DeclaredTypeName type = generator.getPropertyType();
    String fieldToQualify = ALIASED_PROPERTY_ARRAY_NAME + "[" + index + "]";
    Expression expressionToCast;
    if (modelSpec.getQueryElement() != null) {
        String callOn = modelSpec.getSpecAnnotation().isSubquery() ? SUBQUERY_NAME : VIEW_NAME;
        expressionToCast = Expressions.callMethodOn(callOn, "qualifyField", fieldToQualify);
    } else {
        expressionToCast = Expressions.reference(fieldToQualify);
    }
    writer.writeFieldDeclaration(type, generator.getPropertyName(), expressionToCast.cast(type), TypeConstants.PUBLIC_STATIC_FINAL).writeNewline();
    modelSpec.getPluginBundle().afterEmitPropertyDeclaration(writer, generator);
}
Also used : DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) Expression(com.yahoo.aptutils.writer.expressions.Expression)

Example 10 with Expression

use of com.yahoo.aptutils.writer.expressions.Expression in project squidb by yahoo.

the class EnumPropertyGenerator method writeGetterBody.

@Override
protected void writeGetterBody(JavaFileWriter writer, MethodDeclarationParameters params) throws IOException {
    final String value = "value";
    writer.writeFieldDeclaration(CoreTypes.JAVA_STRING, value, Expressions.callMethod("get", propertyName));
    Expression condition = Expressions.fromString(value + " == null");
    Expression ifTrue = Expressions.fromString("null");
    Expression ifFalse = Expressions.staticMethod(enumType, "valueOf", value);
    TernaryExpression ternary = new TernaryExpression(condition, ifTrue, ifFalse);
    writer.writeStatement(ternary.returnExpr());
}
Also used : Expression(com.yahoo.aptutils.writer.expressions.Expression)

Aggregations

Expression (com.yahoo.aptutils.writer.expressions.Expression)12 DeclaredTypeName (com.yahoo.aptutils.model.DeclaredTypeName)3 JavaFileWriter (com.yahoo.aptutils.writer.JavaFileWriter)2 MethodDeclarationParameters (com.yahoo.aptutils.writer.parameters.MethodDeclarationParameters)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 TypeName (com.yahoo.aptutils.model.TypeName)1 Alias (com.yahoo.squidb.annotations.Alias)1 ModelMethod (com.yahoo.squidb.annotations.ModelMethod)1 PropertyGenerator (com.yahoo.squidb.processor.plugins.defaults.properties.generators.PropertyGenerator)1 VariableElement (javax.lang.model.element.VariableElement)1