Search in sources :

Example 1 with DeclaredTypeName

use of com.yahoo.aptutils.model.DeclaredTypeName in project squidb by yahoo.

the class AndroidModelPlugin method emitConstructors.

@Override
public void emitConstructors(JavaFileWriter writer) throws IOException {
    if (generateConstructors) {
        String valuesName = "contentValues";
        DeclaredTypeName valuesType = TypeConstants.CONTENT_VALUES;
        MethodDeclarationParameters params = new MethodDeclarationParameters().setModifiers(Modifier.PUBLIC).setConstructorName(modelSpec.getGeneratedClassName());
        params.setArgumentTypes(Collections.singletonList(valuesType)).setArgumentNames(valuesName);
        writer.beginConstructorDeclaration(params).writeStatement(Expressions.callMethod("this", valuesName, ModelFileWriter.PROPERTIES_ARRAY_NAME)).finishMethodDefinition();
        String methodName = "readPropertiesFromContentValues";
        params.setArgumentTypes(Arrays.asList(valuesType, TypeConstants.PROPERTY_VARARGS)).setArgumentNames(valuesName, "withProperties");
        writer.beginConstructorDeclaration(params).writeStringStatement("this()").writeStringStatement(methodName + "(" + valuesName + ", withProperties)").finishMethodDefinition();
    }
}
Also used : DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) MethodDeclarationParameters(com.yahoo.aptutils.writer.parameters.MethodDeclarationParameters)

Example 2 with DeclaredTypeName

use of com.yahoo.aptutils.model.DeclaredTypeName in project squidb by yahoo.

the class ConstantCopyingPlugin method afterProcessVariableElements.

@Override
public void afterProcessVariableElements() {
    // Look for additional constants in @Constant annotated inner classes
    List<? extends Element> elements = modelSpec.getModelSpecElement().getEnclosedElements();
    for (Element element : elements) {
        if (element instanceof TypeElement && element.getAnnotation(Constants.class) != null) {
            if (!element.getModifiers().containsAll(Arrays.asList(Modifier.PUBLIC, Modifier.STATIC))) {
                utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "@Constants annotated class is not " + "public static, will be ignored", element);
                continue;
            }
            TypeElement constantClass = (TypeElement) element;
            List<VariableElement> constantList = new ArrayList<>();
            innerClassConstants.put(constantClass.getSimpleName().toString(), constantList);
            for (Element e : constantClass.getEnclosedElements()) {
                if (e instanceof VariableElement && e.getAnnotation(Ignore.class) == null) {
                    TypeName typeName = utils.getTypeNameFromTypeMirror(e.asType());
                    if (!(typeName instanceof DeclaredTypeName)) {
                        utils.getMessager().printMessage(Diagnostic.Kind.WARNING, "Element type " + typeName + " is not a concrete type, will be ignored", e);
                    } else {
                        processVariableElement((VariableElement) e, constantList);
                    }
                }
            }
        }
    }
}
Also used : DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) TypeName(com.yahoo.aptutils.model.TypeName) DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Example 3 with DeclaredTypeName

use of com.yahoo.aptutils.model.DeclaredTypeName in project squidb by yahoo.

the class ConstantCopyingPlugin method afterEmitSchema.

@Override
public void afterEmitSchema(JavaFileWriter writer) throws IOException {
    writer.writeComment("--- constants");
    for (VariableElement constant : constantElements) {
        writeConstantField(writer, modelSpec.getModelSpecName(), constant);
    }
    for (Map.Entry<String, List<VariableElement>> innerClassConstant : innerClassConstants.entrySet()) {
        String classNameString = innerClassConstant.getKey();
        DeclaredTypeName constClassName = new DeclaredTypeName(null, modelSpec.getModelSpecName().getSimpleName() + "." + classNameString);
        for (VariableElement element : innerClassConstant.getValue()) {
            writeConstantField(writer, constClassName, element);
        }
    }
    writer.writeNewline();
}
Also used : DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) ArrayList(java.util.ArrayList) List(java.util.List) VariableElement(javax.lang.model.element.VariableElement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with DeclaredTypeName

use of com.yahoo.aptutils.model.DeclaredTypeName 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 5 with DeclaredTypeName

use of com.yahoo.aptutils.model.DeclaredTypeName in project squidb by yahoo.

the class ImplementsPlugin method parseInterfaces.

private void parseInterfaces() {
    TypeElement modelSpecElement = modelSpec.getModelSpecElement();
    if (modelSpecElement.getAnnotation(Implements.class) != null) {
        List<DeclaredTypeName> typeNames = utils.getTypeNamesFromAnnotationValue(utils.getAnnotationValue(modelSpecElement, Implements.class, "interfaceClasses"));
        if (!AptUtils.isEmpty(typeNames)) {
            interfaces.addAll(typeNames);
        }
        AnnotationValue value = utils.getAnnotationValue(modelSpecElement, Implements.class, "interfaceDefinitions");
        List<AnnotationMirror> interfaceSpecs = utils.getValuesFromAnnotationValue(value, AnnotationMirror.class);
        for (AnnotationMirror spec : interfaceSpecs) {
            AnnotationValue interfaceClassValue = utils.getAnnotationValueFromMirror(spec, "interfaceClass");
            List<DeclaredTypeName> interfaceClassList = utils.getTypeNamesFromAnnotationValue(interfaceClassValue);
            if (!AptUtils.isEmpty(interfaceClassList)) {
                DeclaredTypeName interfaceClass = interfaceClassList.get(0);
                AnnotationValue interfaceTypeArgsValue = utils.getAnnotationValueFromMirror(spec, "interfaceTypeArgs");
                List<DeclaredTypeName> typeArgs = utils.getTypeNamesFromAnnotationValue(interfaceTypeArgsValue);
                if (AptUtils.isEmpty(typeArgs)) {
                    List<String> typeArgNames = utils.getValuesFromAnnotationValue(utils.getAnnotationValueFromMirror(spec, "interfaceTypeArgNames"), String.class);
                    for (String typeArgName : typeArgNames) {
                        typeArgs.add(new DeclaredTypeName(typeArgName));
                    }
                }
                interfaceClass.setTypeArgs(typeArgs);
                interfaces.add(interfaceClass);
            }
        }
    }
}
Also used : DeclaredTypeName(com.yahoo.aptutils.model.DeclaredTypeName) AnnotationMirror(javax.lang.model.element.AnnotationMirror) Implements(com.yahoo.squidb.annotations.Implements) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Aggregations

DeclaredTypeName (com.yahoo.aptutils.model.DeclaredTypeName)12 Expression (com.yahoo.aptutils.writer.expressions.Expression)3 ArrayList (java.util.ArrayList)3 TypeName (com.yahoo.aptutils.model.TypeName)2 MethodDeclarationParameters (com.yahoo.aptutils.writer.parameters.MethodDeclarationParameters)2 TypeElement (javax.lang.model.element.TypeElement)2 VariableElement (javax.lang.model.element.VariableElement)2 TypeDeclarationParameters (com.yahoo.aptutils.writer.parameters.TypeDeclarationParameters)1 Implements (com.yahoo.squidb.annotations.Implements)1 ErrorInfo (com.yahoo.squidb.processor.data.ErrorInfo)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 Element (javax.lang.model.element.Element)1