Search in sources :

Example 71 with TypeName

use of com.squareup.javapoet.TypeName in project CoreData by FangCloud-Android.

the class CreateConvertStatement method createSerializableListField.

private static FieldSpec createSerializableListField(Element convertElement) {
    TypeName typeName = ClassName.get(convertElement.asType());
    if (typeName instanceof ParameterizedTypeName) {
        List<TypeName> typeArguments = ((ParameterizedTypeName) typeName).typeArguments;
        typeName = typeArguments.get(0);
    }
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(SERIALIZABLE_LIST_CLASS_NAME, typeName);
    return FieldSpec.builder(parameterizedTypeName, Utils.converterName(convertElement, SERIALIZABLE_LIST_CLASS_NAME), Modifier.PRIVATE, Modifier.FINAL).initializer("new $T()", parameterizedTypeName).build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 72 with TypeName

use of com.squareup.javapoet.TypeName in project CoreData by FangCloud-Android.

the class Utils method methodGet.

public static String methodGet(Element element, String prefix) {
    TypeName typeName = ClassName.get(element.asType());
    String elementName = element.getSimpleName().toString();
    Set<Modifier> modifiers = element.getModifiers();
    StringBuilder sb = new StringBuilder();
    if (!TextUtils.isEmpty(prefix)) {
        sb.append(prefix);
        sb.append(".");
    }
    if (modifiers.contains(Modifier.PUBLIC)) {
        sb.append(elementName);
    } else {
        if (typeName == TypeName.BOOLEAN) {
            if (elementName.startsWith("is")) {
                return sb.append(element).append("()").toString();
            } else {
                sb.append("is");
            }
        } else {
            sb.append("get");
        }
        sb.append(elementName.substring(0, 1).toUpperCase()).append(elementName.substring(1)).append("()");
    }
    return sb.toString();
}
Also used : TypeName(com.squareup.javapoet.TypeName) Modifier(javax.lang.model.element.Modifier)

Example 73 with TypeName

use of com.squareup.javapoet.TypeName in project CoreData by FangCloud-Android.

the class Utils method methodSetFormat.

public static String methodSetFormat(Element element, String prefix) {
    TypeName typeName = ClassName.get(element.asType());
    String elementName = element.getSimpleName().toString();
    Set<Modifier> modifiers = element.getModifiers();
    StringBuilder sb = new StringBuilder();
    if (!TextUtils.isEmpty(prefix)) {
        sb.append(prefix).append(".");
    }
    if (modifiers.contains(Modifier.PUBLIC)) {
        sb.append(elementName).append(" = $N");
    } else {
        sb.append("set");
        if (typeName == TypeName.BOOLEAN && elementName.startsWith("is")) {
            sb.append(elementName.substring(2));
        } else {
            sb.append(elementName.substring(0, 1).toUpperCase()).append(elementName.substring(1));
        }
        sb.append("($N)");
    }
    return sb.toString();
}
Also used : TypeName(com.squareup.javapoet.TypeName) Modifier(javax.lang.model.element.Modifier)

Example 74 with TypeName

use of com.squareup.javapoet.TypeName in project CoreData by FangCloud-Android.

the class Utils method getProperties.

public static List<Property> getProperties(ProcessingEnvironment env, List<Element> elements, Element primaryKey) {
    Elements elementUtils = env.getElementUtils();
    Types typeUtils = env.getTypeUtils();
    List<Property> propertyList = new ArrayList<>();
    for (Element element : elements) {
        String columnName = Utils.getColumnName(element);
        TypeName dbBaseType;
        TypeMirror typeMirror = element.asType();
        TypeName typeNameElement = ClassName.get(typeMirror);
        if (Utils.isBaseType(typeNameElement)) {
            dbBaseType = typeNameElement;
        } else {
            TypeElement elementFieldType = (TypeElement) typeUtils.asElement(typeMirror);
            Relation annotationRelation = element.getAnnotation(Relation.class);
            if (annotationRelation != null) {
                // 关联数据
                Entity relationEntity = elementFieldType.getAnnotation(Entity.class);
                if (relationEntity == null) {
                    throw new IllegalStateException("@Relation 添加的属性必须是 @Entity 的类");
                }
                EntityDetail relationEntityDetail = EntityDetail.parse(env, elementFieldType);
                Element relationEntityPrimaryKey = relationEntityDetail.getPrimaryKey();
                if (relationEntityPrimaryKey == null) {
                    throw new IllegalStateException(relationEntityDetail.getEntityName() + "是关联类型,@Relation 属性必须有主键");
                }
                dbBaseType = ClassName.get(relationEntityPrimaryKey.asType());
            } else {
                Embedded embedded = element.getAnnotation(Embedded.class);
                if (embedded != null) {
                    // 内嵌数据, 循环解析内嵌结构的字段
                    List<Element> embeddedEleList = new ArrayList<>();
                    fillElementsForDbAndReturnPrimaryKey(env, embeddedEleList, (TypeElement) typeUtils.asElement(typeMirror));
                    propertyList.addAll(getProperties(env, embeddedEleList, null));
                    continue;
                } else {
                    Convert convert = element.getAnnotation(Convert.class);
                    if (convert != null) {
                        TypeName classConvertDb = Utils.getConvertDbType(convert);
                        if (Utils.isBaseType(classConvertDb)) {
                            dbBaseType = classConvertDb;
                        } else {
                            throw new IllegalStateException(element.getSimpleName() + "converter dbtype is not a base type");
                        }
                    } else {
                        throw new IllegalStateException(element.getSimpleName() + " is a complex structure field," + "must indicate it as @Relation or @Embedded, " + "or supply a @Convert for it");
                    }
                }
            }
        }
        propertyList.add(new Property(columnName, Utils.getTypeByTypeName(dbBaseType), element == primaryKey));
    }
    return propertyList;
}
Also used : Types(javax.lang.model.util.Types) Entity(com.coredata.annotation.Entity) TypeName(com.squareup.javapoet.TypeName) Convert(com.coredata.annotation.Convert) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) Elements(javax.lang.model.util.Elements) Relation(com.coredata.annotation.Relation) TypeMirror(javax.lang.model.type.TypeMirror) Embedded(com.coredata.annotation.Embedded) EntityDetail(com.coredata.compiler.EntityDetail) Property(com.coredata.db.Property)

Example 75 with TypeName

use of com.squareup.javapoet.TypeName in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method buildTypeName.

static TypeName buildTypeName(String typeDeclaration) {
    String type = trimStorageDeclaration(typeDeclaration);
    if (type.endsWith("]")) {
        String[] splitType = type.split("\\[");
        Class<?> baseType = AbiTypes.getType(splitType[0]);
        TypeName typeName;
        if (splitType[1].length() == 1) {
            typeName = ParameterizedTypeName.get(DynamicArray.class, baseType);
        } else {
            // Unfortunately we can't encode it's length as a type
            typeName = ParameterizedTypeName.get(StaticArray.class, baseType);
        }
        return typeName;
    } else {
        Class<?> cls = AbiTypes.getType(type);
        return ClassName.get(cls);
    }
}
Also used : StaticArray(org.bcos.web3j.abi.datatypes.StaticArray) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) DynamicArray(org.bcos.web3j.abi.datatypes.DynamicArray)

Aggregations

TypeName (com.squareup.javapoet.TypeName)75 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)62 ClassName (com.squareup.javapoet.ClassName)35 MethodSpec (com.squareup.javapoet.MethodSpec)27 TypeSpec (com.squareup.javapoet.TypeSpec)22 TypeElement (javax.lang.model.element.TypeElement)21 TypeMirror (javax.lang.model.type.TypeMirror)19 ArrayList (java.util.ArrayList)15 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)14 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)10 CodeBlock (com.squareup.javapoet.CodeBlock)10 Map (java.util.Map)10 FieldSpec (com.squareup.javapoet.FieldSpec)9 WireField (com.squareup.wire.WireField)9 Field (com.squareup.wire.schema.Field)9 List (java.util.List)9 HashSet (java.util.HashSet)7 Set (java.util.Set)7 Element (javax.lang.model.element.Element)7 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)6