Search in sources :

Example 11 with Expression

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

the class EnumPropertyGenerator method writeSetterBody.

@Override
protected void writeSetterBody(JavaFileWriter writer, MethodDeclarationParameters params) throws IOException {
    String argName = params.getArgumentNames().get(0);
    final String argAsString = argName + "AsString";
    Expression condition = Expressions.fromString(argName + " == null");
    Expression ifTrue = Expressions.fromString("null");
    Expression ifFalse = Expressions.callMethodOn(argName, "name");
    writer.writeFieldDeclaration(CoreTypes.JAVA_STRING, argAsString, new TernaryExpression(condition, ifTrue, ifFalse));
    writer.writeStatement(Expressions.callMethod("set", propertyName, argAsString));
    writer.writeStringStatement("return this");
}
Also used : Expression(com.yahoo.aptutils.writer.expressions.Expression)

Example 12 with Expression

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

the class ModelMethodPlugin method emitModelMethod.

private void emitModelMethod(JavaFileWriter writer, ExecutableElement e, Modifier... modifiers) throws IOException {
    MethodDeclarationParameters params = utils.methodDeclarationParamsFromExecutableElement(e, modifiers);
    ModelMethod methodAnnotation = e.getAnnotation(ModelMethod.class);
    List<Object> arguments = new ArrayList<>();
    if (methodAnnotation != null) {
        String name = methodAnnotation.name();
        if (!AptUtils.isEmpty(name)) {
            params.setMethodName(name);
        }
        params.getArgumentTypes().remove(0);
        params.getArgumentNames().remove(0);
        arguments.add(0, "this");
    }
    arguments.addAll(params.getArgumentNames());
    Expression methodCall = Expressions.staticMethod(modelSpec.getModelSpecName(), e.getSimpleName().toString(), arguments);
    if (!CoreTypes.VOID.equals(params.getReturnType())) {
        methodCall = methodCall.returnExpr();
    }
    JavadocPlugin.writeJavadocFromElement(pluginEnv, writer, e);
    writer.beginMethodDefinition(params).writeStatement(methodCall).finishMethodDefinition();
}
Also used : ModelMethod(com.yahoo.squidb.annotations.ModelMethod) Expression(com.yahoo.aptutils.writer.expressions.Expression) MethodDeclarationParameters(com.yahoo.aptutils.writer.parameters.MethodDeclarationParameters) ArrayList(java.util.ArrayList)

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