Search in sources :

Example 1 with Expression

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

the class ConstructorPlugin method emitConstructors.

@Override
public void emitConstructors(JavaFileWriter writer) throws IOException {
    writer.writeComment("--- default constructors");
    MethodDeclarationParameters params = new MethodDeclarationParameters().setModifiers(Modifier.PUBLIC).setConstructorName(modelSpec.getGeneratedClassName());
    writer.beginConstructorDeclaration(params).writeStringStatement("super()").finishMethodDefinition();
    DeclaredTypeName squidCursorType = TypeConstants.SQUID_CURSOR.clone();
    squidCursorType.setTypeArgs(Collections.singletonList(modelSpec.getGeneratedClassName()));
    params.setArgumentTypes(squidCursorType).setArgumentNames("cursor");
    writer.beginConstructorDeclaration(params).writeStringStatement("this()").writeStringStatement("readPropertiesFromCursor(cursor)").finishMethodDefinition();
    String valuesName = "values";
    DeclaredTypeName valuesType = TypeConstants.MAP_VALUES;
    params.setArgumentTypes(Collections.singletonList(valuesType)).setArgumentNames(valuesName);
    writer.beginConstructorDeclaration(params).writeStatement(Expressions.callMethod("this", valuesName, ModelFileWriter.PROPERTIES_ARRAY_NAME)).finishMethodDefinition();
    String methodName = "readPropertiesFromMap";
    params.setArgumentTypes(Arrays.asList(valuesType, TypeConstants.PROPERTY_VARARGS)).setArgumentNames(valuesName, "withProperties");
    writer.beginConstructorDeclaration(params).writeStringStatement("this()").writeStringStatement(methodName + "(" + valuesName + ", withProperties)").finishMethodDefinition();
    MethodDeclarationParameters cloneParams = new MethodDeclarationParameters().setModifiers(Modifier.PUBLIC).setMethodName("clone").setReturnType(modelSpec.getGeneratedClassName());
    Expression cloneBody = Expressions.callMethodOn("super", "clone").cast(modelSpec.getGeneratedClassName()).returnExpr();
    writer.writeAnnotation(CoreTypes.OVERRIDE);
    writer.beginMethodDefinition(cloneParams).writeStatement(cloneBody).finishMethodDefinition();
}
Also used : DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) Expression(com.yahoo.aptutils.writer.expressions.Expression) MethodDeclarationParameters(com.yahoo.aptutils.writer.parameters.MethodDeclarationParameters)

Example 2 with Expression

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

the class JSONPropertyGenerator method getTypeExpression.

private Expression getTypeExpression(DeclaredTypeName fieldType) {
    List<? extends TypeName> typeArgs = fieldType.getTypeArgs();
    if (AptUtils.isEmpty(typeArgs)) {
        return Expressions.classObject(fieldType);
    } else {
        List<Expression> parameterizedTypeBuilderArgs = new ArrayList<>();
        parameterizedTypeBuilderArgs.add(Expressions.classObject(fieldType));
        for (TypeName typeArg : typeArgs) {
            // The cast to DeclaredTypeName is safe because we recursively check all type args before constructing
            // an instance of this property generator
            parameterizedTypeBuilderArgs.add(getTypeExpression((DeclaredTypeName) typeArg));
        }
        return Expressions.staticMethod(JSONTypes.PARAMETERIZED_TYPE_BUILDER, "build", parameterizedTypeBuilderArgs);
    }
}
Also used : DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) TypeName(com.yahoo.aptutils.model.TypeName) DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) Expression(com.yahoo.aptutils.writer.expressions.Expression) ArrayList(java.util.ArrayList)

Example 3 with Expression

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

the class JSONPropertyGenerator method writeSetterBody.

@Override
protected void writeSetterBody(JavaFileWriter writer, MethodDeclarationParameters params) throws IOException {
    Expression typeExpression = getTypeExpression(fieldType);
    writer.writeStatement(Expressions.staticMethod(JSONTypes.JSON_PROPERTY_SUPPORT, "setValueAsJSON", "this", propertyName, params.getArgumentNames().get(0), typeExpression));
    writer.writeStringStatement("return this");
}
Also used : Expression(com.yahoo.aptutils.writer.expressions.Expression)

Example 4 with Expression

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

the class JSONPropertyGenerator method writeGetterBody.

@Override
protected void writeGetterBody(JavaFileWriter writer, MethodDeclarationParameters params) throws IOException {
    Expression typeExpression = getTypeExpression(fieldType);
    writer.writeStatement(Expressions.staticMethod(JSONTypes.JSON_PROPERTY_SUPPORT, "getValueFromJSON", "this", propertyName, typeExpression).returnExpr());
}
Also used : Expression(com.yahoo.aptutils.writer.expressions.Expression)

Example 5 with Expression

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

the class ViewModelFileWriter method emitSqlTableDeclaration.

private void emitSqlTableDeclaration(boolean view) throws IOException {
    writer.writeComment("--- " + (view ? "view" : "subquery") + " declaration");
    String name = "\"" + modelSpec.getSpecAnnotation().viewName().trim() + "\"";
    if (modelSpec.getQueryElement() != null) {
        Expression queryReference = Expressions.staticReference(modelSpec.getModelSpecName(), modelSpec.getQueryElement().getSimpleName().toString()).callMethod("selectMore", ALIASED_PROPERTY_ARRAY_NAME);
        if (modelSpec.getViewQueryAnnotation().freeze()) {
            queryReference = queryReference.callMethod("freeze");
        }
        writer.writeFieldDeclaration(TypeConstants.QUERY, QUERY_NAME, queryReference, TypeConstants.PUBLIC_STATIC_FINAL);
        Expression initializer = constructInitializer(name, view);
        writer.writeFieldDeclaration(view ? TypeConstants.VIEW : TypeConstants.SUBQUERY_TABLE, view ? VIEW_NAME : SUBQUERY_NAME, initializer, TypeConstants.PUBLIC_STATIC_FINAL);
    } else {
        writer.writeFieldDeclaration(CoreTypes.JAVA_STRING, view ? "VIEW_NAME" : "SUBQUERY_NAME", Expressions.fromString(name), TypeConstants.PUBLIC_STATIC_FINAL);
    }
    writer.writeNewline();
}
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