Search in sources :

Example 1 with ModelMethod

use of com.yahoo.squidb.annotations.ModelMethod in project squidb by yahoo.

the class ModelMethodPlugin method checkExecutableElement.

private void checkExecutableElement(ExecutableElement e, List<ExecutableElement> modelMethods, List<ExecutableElement> staticModelMethods, DeclaredTypeName modelClass) {
    Set<Modifier> modifiers = e.getModifiers();
    if (e.getKind() == ElementKind.CONSTRUCTOR) {
        // Don't copy constructors
        return;
    }
    if (!modifiers.contains(Modifier.STATIC)) {
        utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "Model spec objects should never be instantiated, so non-static methods are meaningless. " + "Did you mean to make this a static method?", e);
        return;
    }
    ModelMethod methodAnnotation = e.getAnnotation(ModelMethod.class);
    // Don't assume error if method is private
    if (methodAnnotation == null) {
        if (modifiers.contains(Modifier.PUBLIC)) {
            staticModelMethods.add(e);
        } else if (!modifiers.contains(Modifier.PRIVATE)) {
            utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "This method will not be added to the model definition. " + "Did you mean to annotate this method with @ModelMethod?", e);
        }
    } else {
        List<? extends VariableElement> params = e.getParameters();
        if (params.size() == 0) {
            modelSpec.logError("@ModelMethod methods must have an abstract model as their first argument", e);
        } else {
            VariableElement firstParam = params.get(0);
            TypeMirror paramType = firstParam.asType();
            if (!checkFirstArgType(paramType, modelClass)) {
                modelSpec.logError("@ModelMethod methods must have an abstract model as their first argument", e);
            } else {
                modelMethods.add(e);
            }
        }
    }
}
Also used : ModelMethod(com.yahoo.squidb.annotations.ModelMethod) TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement) Modifier(javax.lang.model.element.Modifier)

Example 2 with ModelMethod

use of com.yahoo.squidb.annotations.ModelMethod 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

ModelMethod (com.yahoo.squidb.annotations.ModelMethod)2 Expression (com.yahoo.aptutils.writer.expressions.Expression)1 MethodDeclarationParameters (com.yahoo.aptutils.writer.parameters.MethodDeclarationParameters)1 ArrayList (java.util.ArrayList)1 Modifier (javax.lang.model.element.Modifier)1 VariableElement (javax.lang.model.element.VariableElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1