Search in sources :

Example 6 with VariableElement

use of javax.lang.model.element.VariableElement in project epoxy by airbnb.

the class ProcessorUtils method areParamsTheSame.

private static boolean areParamsTheSame(ExecutableElement method1, MethodSpec method2) {
    List<? extends VariableElement> params1 = method1.getParameters();
    List<ParameterSpec> params2 = method2.parameters;
    if (params1.size() != params2.size()) {
        return false;
    }
    for (int i = 0; i < params1.size(); i++) {
        VariableElement param1 = params1.get(i);
        ParameterSpec param2 = params2.get(i);
        if (!TypeName.get(param1.asType()).equals(param2.type)) {
            return false;
        }
    }
    return true;
}
Also used : ParameterSpec(com.squareup.javapoet.ParameterSpec) VariableElement(javax.lang.model.element.VariableElement)

Example 7 with VariableElement

use of javax.lang.model.element.VariableElement in project immutables by immutables.

the class FactoryMethodAttributesCollector method collect.

void collect() {
    ExecutableElement factoryMethodElement = (ExecutableElement) protoclass.sourceElement();
    Parameterizable element = (Parameterizable) (factoryMethodElement.getKind() == ElementKind.CONSTRUCTOR ? factoryMethodElement.getEnclosingElement() : type.element);
    for (VariableElement parameter : factoryMethodElement.getParameters()) {
        TypeMirror returnType = parameter.asType();
        ValueAttribute attribute = new ValueAttribute();
        attribute.isGenerateAbstract = true;
        attribute.reporter = reporter;
        attribute.returnType = returnType;
        attribute.element = parameter;
        String parameterName = parameter.getSimpleName().toString();
        attribute.names = styles.forAccessorWithRaw(parameterName, parameterName);
        attribute.containingType = type;
        attributes.add(attribute);
    }
    Instantiator encodingInstantiator = protoclass.encodingInstantiator();
    @Nullable InstantiationCreator instantiationCreator = encodingInstantiator.creatorFor(element);
    for (ValueAttribute attribute : attributes) {
        attribute.initAndValidate(instantiationCreator);
    }
    if (instantiationCreator != null) {
        type.additionalImports(instantiationCreator.imports);
    }
    type.attributes.addAll(attributes);
    type.throwing = extractThrowsClause(factoryMethodElement);
}
Also used : InstantiationCreator(org.immutables.value.processor.encode.Instantiator.InstantiationCreator) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) Parameterizable(javax.lang.model.element.Parameterizable) Instantiator(org.immutables.value.processor.encode.Instantiator) VariableElement(javax.lang.model.element.VariableElement) Nullable(javax.annotation.Nullable)

Example 8 with VariableElement

use of javax.lang.model.element.VariableElement in project T-MVP by north2016.

the class ApiFactoryProcessor method process.

@Override
public void process(RoundEnvironment roundEnv, AnnotationProcessor mAbstractProcessor) {
    String CLASS_NAME = "ApiFactory";
    String DATA_ARR_CLASS = "DataArr";
    TypeSpec.Builder tb = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL).addJavadoc("@ API工厂 此类由apt自动生成");
    try {
        for (TypeElement element : ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(ApiFactory.class))) {
            mAbstractProcessor.mMessager.printMessage(Diagnostic.Kind.NOTE, "正在处理: " + element.toString());
            for (Element e : element.getEnclosedElements()) {
                ExecutableElement executableElement = (ExecutableElement) e;
                MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(e.getSimpleName().toString()).addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC);
                if (TypeName.get(executableElement.getReturnType()).toString().contains(DATA_ARR_CLASS)) {
                    //返回列表数据
                    methodBuilder.returns(ClassName.get("rx", "Observable"));
                    Map<String, Object> params = new HashMap<>();
                    methodBuilder.addParameter(params.getClass(), "param");
                    ClassName apiUtil = ClassName.get("com.base.util", "ApiUtil");
                    ClassName C = ClassName.get("com", "C");
                    CodeBlock.Builder blockBuilder = CodeBlock.builder();
                    int len = executableElement.getParameters().size();
                    for (int i = 0; i < len; i++) {
                        VariableElement ep = executableElement.getParameters().get(i);
                        boolean isLast = i == len - 1;
                        String split = (isLast ? "" : ",");
                        switch(ep.getSimpleName().toString()) {
                            case "include":
                                blockBuilder.add("$L.getInclude(param)" + split, apiUtil);
                                break;
                            case "where":
                                blockBuilder.add("$L.getWhere(param)" + split, apiUtil);
                                break;
                            case "skip":
                                blockBuilder.add("$L.getSkip(param)" + split, apiUtil);
                                break;
                            case "limit":
                                blockBuilder.add("$L.PAGE_COUNT" + split, C);
                                break;
                            case "order":
                                blockBuilder.add("$L._CREATED_AT" + split, C);
                                break;
                        }
                    }
                    methodBuilder.addStatement("return $T.getInstance()" + ".service.$L($L)" + ".compose($T.io_main())", ClassName.get("com.api", "Api"), e.getSimpleName().toString(), blockBuilder.build().toString(), ClassName.get("com.base.util.helper", "RxSchedulers"));
                    tb.addMethod(methodBuilder.build());
                } else {
                    //返回普通数据
                    methodBuilder.returns(TypeName.get(executableElement.getReturnType()));
                    String paramsString = "";
                    for (VariableElement ep : executableElement.getParameters()) {
                        methodBuilder.addParameter(TypeName.get(ep.asType()), ep.getSimpleName().toString());
                        paramsString += ep.getSimpleName().toString() + ",";
                    }
                    methodBuilder.addStatement("return $T.getInstance()" + ".service.$L($L)" + ".compose($T.io_main())", ClassName.get("com.api", "Api"), e.getSimpleName().toString(), paramsString.substring(0, paramsString.length() - 1), ClassName.get("com.base.util.helper", "RxSchedulers"));
                    tb.addMethod(methodBuilder.build());
                }
            }
        }
        // 生成源代码
        JavaFile javaFile = JavaFile.builder(Utils.PackageName, tb.build()).build();
        // 在 app module/build/generated/source/apt 生成一份源代码
        javaFile.writeTo(mAbstractProcessor.mFiler);
    } catch (FilerException e) {
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ApiFactory(com.app.annotation.apt.ApiFactory) MethodSpec(com.squareup.javapoet.MethodSpec) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) CodeBlock(com.squareup.javapoet.CodeBlock) VariableElement(javax.lang.model.element.VariableElement) IOException(java.io.IOException) IOException(java.io.IOException) FilerException(javax.annotation.processing.FilerException) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) FilerException(javax.annotation.processing.FilerException) TypeSpec(com.squareup.javapoet.TypeSpec)

Example 9 with VariableElement

use of javax.lang.model.element.VariableElement in project storio by pushtorefresh.

the class StorIOTypeMeta method getOrderedColumns.

@NotNull
public Collection<ColumnMeta> getOrderedColumns() {
    if (needCreator) {
        List<String> params = new ArrayList<String>(columns.size());
        List<ColumnMeta> orderedColumns = new ArrayList<ColumnMeta>(Collections.<ColumnMeta>nCopies(columns.size(), null));
        //noinspection ConstantConditions
        for (VariableElement param : creator.getParameters()) {
            params.add(param.getSimpleName().toString());
        }
        for (ColumnMeta column : columns.values()) {
            orderedColumns.set(params.indexOf(column.getRealElementName()), column);
        }
        return orderedColumns;
    } else {
        return columns.values();
    }
}
Also used : ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with VariableElement

use of javax.lang.model.element.VariableElement in project querydsl by querydsl.

the class TypeElementHandler method handleEntityType.

public EntityType handleEntityType(TypeElement element) {
    EntityType entityType = typeFactory.getEntityType(element.asType(), true);
    List<? extends Element> elements = element.getEnclosedElements();
    VisitorConfig config = configuration.getConfig(element, elements);
    Set<String> blockedProperties = new HashSet<String>();
    Map<String, TypeMirror> propertyTypes = new HashMap<String, TypeMirror>();
    Map<String, TypeMirror> fixedTypes = new HashMap<String, TypeMirror>();
    Map<String, Annotations> propertyAnnotations = new HashMap<String, Annotations>();
    // constructors
    if (config.visitConstructors()) {
        handleConstructors(entityType, elements);
    }
    // fields
    if (config.visitFieldProperties()) {
        for (VariableElement field : ElementFilter.fieldsIn(elements)) {
            String name = field.getSimpleName().toString();
            if (configuration.isBlockedField(field)) {
                blockedProperties.add(name);
            } else if (configuration.isValidField(field)) {
                Annotations annotations = new Annotations();
                configuration.inspect(field, annotations);
                annotations.addAnnotation(field.getAnnotation(QueryType.class));
                annotations.addAnnotation(field.getAnnotation(QueryInit.class));
                propertyAnnotations.put(name, annotations);
                propertyTypes.put(name, field.asType());
                TypeMirror fixedType = configuration.getRealType(field);
                if (fixedType != null) {
                    fixedTypes.put(name, fixedType);
                }
            }
        }
    }
    // methods
    if (config.visitMethodProperties()) {
        for (ExecutableElement method : ElementFilter.methodsIn(elements)) {
            String name = method.getSimpleName().toString();
            if (name.startsWith("get") && name.length() > 3 && method.getParameters().isEmpty()) {
                name = BeanUtils.uncapitalize(name.substring(3));
            } else if (name.startsWith("is") && name.length() > 2 && method.getParameters().isEmpty()) {
                name = BeanUtils.uncapitalize(name.substring(2));
            } else {
                continue;
            }
            if (configuration.isBlockedGetter(method)) {
                blockedProperties.add(name);
            } else if (configuration.isValidGetter(method) && !blockedProperties.contains(name)) {
                Annotations annotations = propertyAnnotations.get(name);
                if (annotations == null) {
                    annotations = new Annotations();
                    propertyAnnotations.put(name, annotations);
                }
                configuration.inspect(method, annotations);
                annotations.addAnnotation(method.getAnnotation(QueryType.class));
                annotations.addAnnotation(method.getAnnotation(QueryInit.class));
                propertyTypes.put(name, method.getReturnType());
                TypeMirror fixedType = configuration.getRealType(method);
                if (fixedType != null) {
                    fixedTypes.put(name, fixedType);
                }
            }
        }
    }
    // fixed types override property types
    propertyTypes.putAll(fixedTypes);
    for (Map.Entry<String, Annotations> entry : propertyAnnotations.entrySet()) {
        Property property = toProperty(entityType, entry.getKey(), propertyTypes.get(entry.getKey()), entry.getValue());
        if (property != null) {
            entityType.addProperty(property);
        }
    }
    return entityType;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) EntityType(com.querydsl.codegen.EntityType) Annotations(com.querydsl.core.util.Annotations) TypeMirror(javax.lang.model.type.TypeMirror) Property(com.querydsl.codegen.Property)

Aggregations

VariableElement (javax.lang.model.element.VariableElement)248 ExecutableElement (javax.lang.model.element.ExecutableElement)75 TypeElement (javax.lang.model.element.TypeElement)75 TypeMirror (javax.lang.model.type.TypeMirror)72 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)35 Element (javax.lang.model.element.Element)35 Expression (com.google.devtools.j2objc.ast.Expression)30 SimpleName (com.google.devtools.j2objc.ast.SimpleName)28 JVar (com.helger.jcodemodel.JVar)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)23 ArrayList (java.util.ArrayList)23 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)16 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)16 JInvocation (com.helger.jcodemodel.JInvocation)16 DeclaredType (javax.lang.model.type.DeclaredType)16 JBlock (com.helger.jcodemodel.JBlock)15 AbstractJClass (com.helger.jcodemodel.AbstractJClass)14 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)13 Elements (javax.lang.model.util.Elements)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)12