Java Code Examples for com.squareup.javapoet.ParameterizedTypeName#get()

The following examples show how to use com.squareup.javapoet.ParameterizedTypeName#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceInterfaceGenerator.java    From spring-openapi with MIT License 6 votes vote down vote up
private ParameterSpec.Builder parseTypeBasedSchema(String parameterName, Schema innerSchema) {
	if (equalsIgnoreCase(innerSchema.getType(), "string")) {
		return ParameterSpec.builder(getStringGenericClassName(innerSchema), parameterName);
	} else if (equalsIgnoreCase(innerSchema.getType(), "integer") || equalsIgnoreCase(innerSchema.getType(), "number")) {
		return getNumberBasedSchemaParameter(parameterName, innerSchema);
	} else if (equalsIgnoreCase(innerSchema.getType(), "boolean")) {
		return createSimpleParameterSpec(JAVA_LANG_PKG, "Boolean", parameterName);
	} else if (equalsIgnoreCase(innerSchema.getType(), "array") && innerSchema instanceof ArraySchema) {
		ArraySchema arraySchema = (ArraySchema) innerSchema;
		ParameterizedTypeName listParameterizedTypeName = ParameterizedTypeName.get(
				ClassName.get("java.util", "List"),
				determineTypeName(arraySchema.getItems())
		);
		return ParameterSpec.builder(listParameterizedTypeName, parameterName);
	} else if (equalsIgnoreCase(innerSchema.getType(), "object") && isFile(innerSchema.getProperties())) {
		return ParameterSpec.builder(ClassName.get(File.class), parameterName);
	}
	return createSimpleParameterSpec(JAVA_LANG_PKG, "Object", parameterName);
}
 
Example 2
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
MethodSpec buildDefaultEventObservableFunction(
        String responseClassName,
        String functionName) {

    String generatedFunctionName =
            Strings.lowercaseFirstLetter(functionName) + "EventObservable";
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(rx
            .Observable.class), ClassName.get("", responseClassName));

    MethodSpec.Builder observableMethodBuilder =
            MethodSpec.methodBuilder(generatedFunctionName)
                    .addModifiers(Modifier.PUBLIC)
                    .addParameter(DefaultBlockParameter.class, START_BLOCK)
                    .addParameter(DefaultBlockParameter.class, END_BLOCK)
                    .returns(parameterizedTypeName);

    observableMethodBuilder.addStatement("$1T filter = new $1T($2L, $3L, "
            + "getContractAddress())", PlatonFilter.class, START_BLOCK, END_BLOCK)
            .addStatement("filter.addSingleTopic($T.encode("
                    + buildEventDefinitionName(functionName) + "))", EventEncoder.class)
            .addStatement("return " + generatedFunctionName + "(filter)");

    return observableMethodBuilder
            .build();
}
 
Example 3
Source File: WasmFunctionWrapper.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
MethodSpec buildDefaultEventObservableFunction(String responseClassName, String functionName) {

		String generatedFunctionName = Strings.lowercaseFirstLetter(functionName) + "EventObservable";
		ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(rx.Observable.class),
				ClassName.get("", responseClassName));

		MethodSpec.Builder observableMethodBuilder = MethodSpec.methodBuilder(generatedFunctionName).addModifiers(Modifier.PUBLIC)
				.addParameter(DefaultBlockParameter.class, START_BLOCK).addParameter(DefaultBlockParameter.class, END_BLOCK)
				.returns(parameterizedTypeName);

		observableMethodBuilder.addStatement("$1T filter = new $1T($2L, $3L, " + "getContractAddress())", PlatonFilter.class, START_BLOCK, END_BLOCK)
				.addStatement("filter.addSingleTopic($T.encode(" + buildEventDefinitionName(functionName) + "))", WasmEventEncoder.class)
				.addStatement("return " + generatedFunctionName + "(filter)");

		return observableMethodBuilder.build();
	}
 
Example 4
Source File: WasmFunctionWrapper.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
MethodSpec buildEventObservableFunction(String responseClassName, String functionName, List<NamedTypeName> indexedParameters,
		List<NamedTypeName> nonIndexedParameters) {

	String generatedFunctionName = Strings.lowercaseFirstLetter(functionName) + "EventObservable";
	ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(rx.Observable.class),
			ClassName.get("", responseClassName));

	MethodSpec.Builder observableMethodBuilder = MethodSpec.methodBuilder(generatedFunctionName).addModifiers(Modifier.PUBLIC)
			.addParameter(PlatonFilter.class, FILTER).returns(parameterizedTypeName);

	TypeSpec converter = TypeSpec.anonymousClassBuilder("")
			.addSuperinterface(
					ParameterizedTypeName.get(ClassName.get(Func1.class), ClassName.get(Log.class), ClassName.get("", responseClassName)))
			.addMethod(MethodSpec.methodBuilder("call").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(Log.class, "log")
					.returns(ClassName.get("", responseClassName))
					.addStatement("$T eventValues = extractEventParametersWithLog(" + buildEventDefinitionName(functionName) + ", log)",
							WasmContract.WasmEventValuesWithLog.class)
					.addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName))
					.addCode(buildTypedResponse("typedResponse", indexedParameters, nonIndexedParameters, true))
					.addStatement("return typedResponse").build())
			.build();

	observableMethodBuilder.addStatement("return web3j.platonLogObservable(filter).map($L)", converter);

	return observableMethodBuilder.build();
}
 
Example 5
Source File: WasmFunctionWrapper.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
MethodSpec buildEventTransactionReceiptFunction(String responseClassName, String functionName, List<NamedTypeName> indexedParameters,
		List<NamedTypeName> nonIndexedParameters) {

	ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(List.class), ClassName.get("", responseClassName));

	String generatedFunctionName = "get" + Strings.capitaliseFirstLetter(functionName) + "Events";
	MethodSpec.Builder transactionMethodBuilder = MethodSpec.methodBuilder(generatedFunctionName).addModifiers(Modifier.PUBLIC)
			.addParameter(TransactionReceipt.class, "transactionReceipt").returns(parameterizedTypeName);

	transactionMethodBuilder
			.addStatement("$T valueList = extractEventParametersWithLog(" + buildEventDefinitionName(functionName) + ", " + "transactionReceipt)",
					ParameterizedTypeName.get(List.class, WasmContract.WasmEventValuesWithLog.class))
			.addStatement("$1T responses = new $1T(valueList.size())",
					ParameterizedTypeName.get(ClassName.get(ArrayList.class), ClassName.get("", responseClassName)))
			.beginControlFlow("for ($T eventValues : valueList)", WasmContract.WasmEventValuesWithLog.class)
			.addStatement("$1T typedResponse = new $1T()", ClassName.get("", responseClassName))
			.addCode(buildTypedResponse("typedResponse", indexedParameters, nonIndexedParameters, false))
			.addStatement("responses.add(typedResponse)").endControlFlow();

	transactionMethodBuilder.addStatement("return responses");
	return transactionMethodBuilder.build();
}
 
Example 6
Source File: InjectProcessor.java    From customview-samples with Apache License 2.0 6 votes vote down vote up
private TypeSpec createRepotityTypeSpec(ClassName builderClassName,List<ClassName> factoryList) {
    ClassName list = ClassName.get("java.util", "List");
    ClassName arrayList = ClassName.get("java.util", "ArrayList");
    TypeName listType = ParameterizedTypeName.get(list, ClassName.get("java.lang","Class"));

    FieldSpec fieldSpec = FieldSpec.builder(listType, "sDataList")
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .initializer("new $T()", arrayList)
            .build();

    CodeBlock.Builder blockBuilder = CodeBlock.builder();
    for(int i =0; i< factoryList.size();i++){
        blockBuilder.addStatement("sDataList.add($T.class)",factoryList.get(i));
    }

    return TypeSpec
            .classBuilder(builderClassName)
            .addModifiers(Modifier.PUBLIC)
            .addStaticBlock(blockBuilder.build())
            .addField(fieldSpec)
            .build();
}
 
Example 7
Source File: InvokingMessageProcessor.java    From SmartEventBus with Apache License 2.0 5 votes vote down vote up
private String generateEventInterfaceClass(List<EventInfo> events, String originalPackageName, String originalClassName) {
    String interfaceName = generateClassName(originalClassName);
    TypeSpec.Builder builder = TypeSpec.interfaceBuilder(interfaceName)
            .addModifiers(Modifier.PUBLIC)
            .addSuperinterface(IEventsDefine.class)
            .addJavadoc("Auto generate code, do not modify!!!");
    for (EventInfo event : events) {
        ClassName lebClassName = ClassName.get(LEB_PACKAGE_NAME, LEB_CLASS_NAME);
        ClassName obsClassName = lebClassName.nestedClass(LEB_OBSERVER_CLASS_NAME);
        TypeName returnType;
        String eventTypeStr = event.getType();
        if (eventTypeStr == null || eventTypeStr.length() == 0) {
            returnType = ParameterizedTypeName.get(obsClassName, ClassName.get(Object.class));
        } else {
            Type eventType = getType(eventTypeStr);
            if (eventType != null) {
                returnType = ParameterizedTypeName.get(obsClassName, ClassName.get(eventType));
            } else {
                returnType = ParameterizedTypeName.get(obsClassName, TypeVariableName.get(eventTypeStr));
            }
        }
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(event.getName())
                .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
                .returns(returnType);
        builder.addMethod(methodBuilder.build());
    }
    TypeSpec typeSpec = builder.build();
    String packageName = originalPackageName + GEN_PKG;
    try {
        JavaFile.builder(packageName, typeSpec)
                .build()
                .writeTo(filer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String className = packageName + "." + interfaceName;
    System.out.println(TAG + "generateEventInterfaceClass: " + className);
    return className;
}
 
Example 8
Source File: SmartEventProcessor.java    From SmartEventBus with Apache License 2.0 5 votes vote down vote up
private void generateBusCode() {
    if (eventInfos == null || eventInfos.size() == 0) {
        return;
    }
    if (packageName == null || packageName.length() == 0) {
        packageName = defaultPackageName;
    }
    String className = (busName != null && busName.length() > 0) ? busName : DEFAULT_BUS_NAME;
    TypeSpec.Builder builder = TypeSpec.classBuilder(className)
            .addModifiers(Modifier.PUBLIC)
            .addJavadoc("Auto generate code, do not modify!!!");
    for (EventInfo eventInfo : eventInfos) {
        //添加每一个方法
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(eventInfo.getKey())
                .addModifiers(Modifier.PUBLIC, Modifier.STATIC);
        //添加return
        ClassName baseClassName = ClassName.get(OBSERVABLE_PACKAGE_NAME, OBSERVABLE_CLASS_NAME);
        TypeName typeName = getTypeName(eventInfo.getType());
        TypeName returnType = ParameterizedTypeName.get(baseClassName, typeName);
        methodBuilder.returns(returnType);
        //添加方法体
        ClassName lebClass = ClassName.get(EVENTBUS_PACKAGE_NAME, EVENTBUS_CLASS_NAME);
        methodBuilder.addStatement("return $T.get($S, $T.class)",
                lebClass, eventInfo.getKey(), typeName);

        builder.addMethod(methodBuilder.build());
    }

    TypeSpec typeSpec = builder.build();
    try {
        JavaFile.builder(packageName, typeSpec)
                .build()
                .writeTo(filer);
        isGenerateTargetClass = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static TypeName getNativeType(ParameterizedTypeName parameterizedTypeName) {
    List<TypeName> typeNames = parameterizedTypeName.typeArguments;
    List<TypeName> nativeTypeNames = new ArrayList<>(typeNames.size());
    for (TypeName enclosedTypeName : typeNames) {
        nativeTypeNames.add(getNativeType(enclosedTypeName));
    }
    return ParameterizedTypeName.get(
            ClassName.get(List.class),
            nativeTypeNames.toArray(new TypeName[nativeTypeNames.size()]));
}
 
Example 10
Source File: AnnotatedClass.java    From AndroidQuick with MIT License 5 votes vote down vote up
JavaFile generateFile() {
    //方法参数类型
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(
            ClassName.get(Map.class),
            ClassName.get(String.class),
            ClassName.get(TagInfo.class));
    ParameterSpec params = ParameterSpec.builder(parameterizedTypeName, "params").build();

    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("load")
            .addModifiers(Modifier.PUBLIC)
            .addAnnotation(Override.class)
            .addParameter(params);

    for (BindTagField item : mFields) {
        String key = item.getTypeName().toString();
        TagInfo.Type type = item.getType();
        String[] value = item.getTag();
        String description = item.getDescription();
        // 添加方法内容
        methodBuilder.addStatement("params.put($S,$T.build($T.$L,$S,$S,$S))",
                key,
                ClassName.get(TagInfo.class),
                ClassName.get(TagInfo.Type.class),
                type,
                key,
                Arrays.toString(value),
                description);
    }
    //生成类
    TypeSpec finderClass = TypeSpec.classBuilder("TagService")
            .addSuperinterface(ClassName.get("com.androidwind.annotation.core", "ILoad"))
            .addModifiers(Modifier.PUBLIC)
            .addMethod(methodBuilder.build())
            .build();

    return JavaFile.builder("com.androidwind.annotation", finderClass).build();
}
 
Example 11
Source File: ReactPropertyProcessor.java    From react-native-GPay with MIT License 5 votes vote down vote up
private static TypeName getSuperType(ClassInfo classInfo) {
  switch (classInfo.getType()) {
    case VIEW_MANAGER:
      return ParameterizedTypeName.get(
          VIEW_MANAGER_SETTER_TYPE,
          classInfo.mClassName,
          classInfo.mViewType);
    case SHADOW_NODE:
      return ParameterizedTypeName.get(SHADOW_NODE_SETTER_TYPE, classInfo.mClassName);
    default:
      throw new IllegalArgumentException();
  }
}
 
Example 12
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
MethodSpec buildEventTransactionReceiptFunction(
        String responseClassName,
        String functionName,
        List<NamedTypeName> indexedParameters,
        List<NamedTypeName> nonIndexedParameters) {

    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(
            ClassName.get(List.class), ClassName.get("", responseClassName));

    String generatedFunctionName = "get" + Strings.capitaliseFirstLetter(functionName)
            + "Events";
    MethodSpec.Builder transactionMethodBuilder = MethodSpec
            .methodBuilder(generatedFunctionName)
            .addModifiers(Modifier.PUBLIC)
            .addParameter(TransactionReceipt.class, "transactionReceipt")
            .returns(parameterizedTypeName);

    transactionMethodBuilder.addStatement("$T valueList = extractEventParametersWithLog("
            + buildEventDefinitionName(functionName) + ", "
            + "transactionReceipt)", ParameterizedTypeName.get(List.class,
            Contract.EventValuesWithLog.class))
            .addStatement("$1T responses = new $1T(valueList.size())",
                    ParameterizedTypeName.get(ClassName.get(ArrayList.class),
                            ClassName.get("", responseClassName)))
            .beginControlFlow("for ($T eventValues : valueList)",
                    Contract.EventValuesWithLog.class)
            .addStatement("$1T typedResponse = new $1T()",
                    ClassName.get("", responseClassName))
            .addCode(buildTypedResponse("typedResponse", indexedParameters,
                    nonIndexedParameters, false))
            .addStatement("responses.add(typedResponse)")
            .endControlFlow();


    transactionMethodBuilder.addStatement("return responses");
    return transactionMethodBuilder.build();
}
 
Example 13
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
private void buildTupleResultContainer(
        MethodSpec.Builder methodBuilder, ParameterizedTypeName tupleType,
        List<TypeName> outputParameterTypes)
        throws ClassNotFoundException {

    List<TypeName> typeArguments = tupleType.typeArguments;

    CodeBlock.Builder tupleConstructor = CodeBlock.builder();
    tupleConstructor.addStatement(
            "$T results = executeCallMultipleValueReturn(function)",
            ParameterizedTypeName.get(List.class, Type.class))
            .add("return new $T(", tupleType)
            .add("$>$>");

    String resultStringSimple = "\n($T) results.get($L)";
    if (useNativeJavaTypes) {
        resultStringSimple += ".getValue()";
    }

    String resultStringNativeList =
            "\nconvertToNative(($T) results.get($L).getValue())";

    int size = typeArguments.size();
    ClassName classList = ClassName.get(List.class);

    for (int i = 0; i < size; i++) {
        TypeName param = outputParameterTypes.get(i);
        TypeName convertTo = typeArguments.get(i);

        String resultString = resultStringSimple;

        // If we use native java types we need to convert
        // elements of arrays to native java types too
        if (useNativeJavaTypes && param instanceof ParameterizedTypeName) {
            ParameterizedTypeName oldContainer = (ParameterizedTypeName)param;
            ParameterizedTypeName newContainer = (ParameterizedTypeName)convertTo;
            if (newContainer.rawType.compareTo(classList) == 0
                    && newContainer.typeArguments.size() == 1) {
                convertTo = ParameterizedTypeName.get(classList,
                        oldContainer.typeArguments.get(0));
                resultString = resultStringNativeList;
            }
        }

        tupleConstructor
            .add(resultString, convertTo, i);
        tupleConstructor.add(i < size - 1 ? ", " : ");\n");
    }
    tupleConstructor.add("$<$<");

    TypeSpec callableType = TypeSpec.anonymousClassBuilder("")
            .addSuperinterface(ParameterizedTypeName.get(
                    ClassName.get(Callable.class), tupleType))
            .addMethod(MethodSpec.methodBuilder("call")
                    .addAnnotation(Override.class)
                    .addModifiers(Modifier.PUBLIC)
                    .addException(Exception.class)
                    .returns(tupleType)
                    .addCode(tupleConstructor.build())
                    .build())
            .build();

    methodBuilder.addStatement(
            "return new $T(\n$L)", buildRemoteCall(tupleType), callableType);
}
 
Example 14
Source File: HashSetClassDescriptor.java    From aircon with MIT License 4 votes vote down vote up
private static TypeName getTypeName(final TypeName typeName) {
	return ParameterizedTypeName.get(CLASS_NAME, typeName);
}
 
Example 15
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
MethodSpec buildEventObservableFunction(
        String responseClassName,
        String functionName,
        List<org.web3j.codegen.SolidityFunctionWrapper.NamedTypeName> indexedParameters,
        List<org.web3j.codegen.SolidityFunctionWrapper.NamedTypeName> nonIndexedParameters)
        throws ClassNotFoundException {

    String generatedFunctionName =
            Strings.lowercaseFirstLetter(functionName) + "EventObservable";
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(rx
            .Observable.class), ClassName.get("", responseClassName));

    MethodSpec.Builder observableMethodBuilder =
            MethodSpec.methodBuilder(generatedFunctionName)
                    .addModifiers(Modifier.PUBLIC)
                    .addParameter(EthFilter.class, FILTER)
                    .returns(parameterizedTypeName);

    TypeSpec converter = TypeSpec.anonymousClassBuilder("")
            .addSuperinterface(ParameterizedTypeName.get(
                    ClassName.get(Func1.class),
                    ClassName.get(Log.class),
                    ClassName.get("", responseClassName)))
            .addMethod(MethodSpec.methodBuilder("call")
                    .addAnnotation(Override.class)
                    .addModifiers(Modifier.PUBLIC)
                    .addParameter(Log.class, "log")
                    .returns(ClassName.get("", responseClassName))
                    .addStatement("$T eventValues = extractEventParametersWithLog("
                                    + buildEventDefinitionName(functionName) + ", log)",
                            Contract.EventValuesWithLog.class)
                    .addStatement("$1T typedResponse = new $1T()",
                            ClassName.get("", responseClassName))
                    .addCode(buildTypedResponse("typedResponse", indexedParameters,
                            nonIndexedParameters, true))
                    .addStatement("return typedResponse")
                    .build())
            .build();

    observableMethodBuilder
            .addStatement("return web3j.ethLogObservable(filter).map($L)", converter);

    return observableMethodBuilder
            .build();
}
 
Example 16
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
private void addAddressesSupport(TypeSpec.Builder classBuilder,
                                 Map<String, String> addresses) {
    if (addresses != null) {

        ClassName stringType = ClassName.get(String.class);
        ClassName mapType = ClassName.get(HashMap.class);
        TypeName mapStringString = ParameterizedTypeName.get(mapType, stringType, stringType);
        FieldSpec addressesStaticField = FieldSpec
                .builder(mapStringString, "_addresses",
                        Modifier.PROTECTED, Modifier.STATIC, Modifier.FINAL)
                .build();
        classBuilder.addField(addressesStaticField);

        final CodeBlock.Builder staticInit = CodeBlock.builder();
        staticInit.addStatement("_addresses = new HashMap<String, String>()");
        addresses.forEach((k, v) ->
                staticInit.addStatement(String.format("_addresses.put(\"%1s\", \"%2s\")",
                        k, v))
        );
        classBuilder.addStaticBlock(staticInit.build());

        // See org.web3j.tx.Contract#getStaticDeployedAddress(String)
        MethodSpec getAddress = MethodSpec
                .methodBuilder("getStaticDeployedAddress")
                .addModifiers(Modifier.PROTECTED)
                .returns(stringType)
                .addParameter(stringType, "networkId")
                .addCode(
                        CodeBlock
                                .builder()
                                .addStatement("return _addresses.get(networkId)")
                                .build())
                .build();
        classBuilder.addMethod(getAddress);

        MethodSpec getPreviousAddress = MethodSpec
                .methodBuilder("getPreviouslyDeployedAddress")
                .addModifiers(Modifier.PUBLIC)
                .addModifiers(Modifier.STATIC)
                .returns(stringType)
                .addParameter(stringType, "networkId")
                .addCode(
                        CodeBlock
                                .builder()
                                .addStatement("return _addresses.get(networkId)")
                                .build())
                .build();
        classBuilder.addMethod(getPreviousAddress);

    }
}
 
Example 17
Source File: FlapProcessor.java    From Flap with Apache License 2.0 4 votes vote down vote up
/**
 * 为 Component 生成 ComponentProxy
 *
 * @param roundEnvironment     环境
 * @param typeElement          @Component
 * @param flapComponentElement 被 FlapComponent 注解了的那个类
 * @param componentProxy       注解了目标类的 注解,可以获取值
 *
 * @return ComponentProxy TypeSpec
 */
private TypeSpec createComponentProxyTypeSpec(final RoundEnvironment roundEnvironment, final TypeElement typeElement, final TypeElement flapComponentElement, final Component componentProxy) {

    ClassName flapItemClass = (ClassName) ClassName.get(flapComponentElement.asType());

    //要生成的类的名字
    String targetClassName = flapComponentElement.getSimpleName().toString() + NAME_SUFFIX;

    int layoutId = componentProxy.layoutId();
    boolean autoRegister = componentProxy.autoRegister();

    boolean dataBinding = componentProxy.useDataBinding();

    DeclaredType declaredType = flapComponentElement.getSuperclass().accept(new FlapItemModelVisitor(), null);
    List<? extends TypeMirror> args = declaredType.getTypeArguments();
    TypeElement itemModelType = (TypeElement) types.asElement(args.get(0));

    ClassName itemModelClass = ClassName.get(itemModelType);

    ClassName layoutInflater = ClassName.get("android.view", "LayoutInflater");
    ClassName viewGroup = ClassName.get("android.view", "ViewGroup");

    MethodSpec.Builder onCreateViewHolderMethodBuilder = MethodSpec.methodBuilder("createComponent")
            .addAnnotation(Override.class)
            .addModifiers(Modifier.PUBLIC)
            .addParameter(layoutInflater, "inflater")
            .addParameter(viewGroup, "parent")
            .addParameter(TypeName.INT, "layoutId")
            .returns(flapItemClass);

    if (dataBinding) {
        onCreateViewHolderMethodBuilder.addStatement("return new $T(androidx.databinding.DataBindingUtil.inflate(inflater,layoutId,parent,false))",flapItemClass);
    } else {
        onCreateViewHolderMethodBuilder.addStatement("return new $T(inflater.inflate(layoutId,parent,false))", flapItemClass);
    }

    MethodSpec onCreateViewHolderMethod = onCreateViewHolderMethodBuilder.build();

    MethodSpec getItemViewTypeMethod = MethodSpec.methodBuilder("getItemViewType")
            .addAnnotation(Override.class)
            .addModifiers(Modifier.PUBLIC)
            .addParameter(itemModelClass, "model")
            .returns(Integer.TYPE)
            .addStatement("return " + layoutId)
            .build();

    MethodSpec getComponentModelClass = MethodSpec.methodBuilder("getComponentModelClass")
            .addAnnotation(Override.class)
            .addModifiers(Modifier.PUBLIC)
            .returns(Class.class)
            .addStatement("return " + itemModelClass + ".class")
            .build();

    ParameterizedTypeName name = ParameterizedTypeName.get(CLASS_COMPONENT_PROXY, itemModelClass, flapItemClass);

    TypeSpec.Builder builder =
            TypeSpec.classBuilder(targetClassName)
                    .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
                    .addAnnotation(CLASS_KEEP)
                    .addMethod(onCreateViewHolderMethod)
                    .addMethod(getItemViewTypeMethod)
                    .addMethod(getComponentModelClass)
                    .addSuperinterface(name);

    if (autoRegister) {
        builder.addAnnotation(AutoRegister.class);
    }
    return builder.build();
}
 
Example 18
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
private static ParameterizedTypeName buildRemoteCall(TypeName typeName) {
    return ParameterizedTypeName.get(
            ClassName.get(RemoteCall.class), typeName);
}
 
Example 19
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
MethodSpec buildEventObservableFunction(
        String responseClassName,
        String functionName,
        List<org.web3j.codegen.SolidityFunctionWrapper.NamedTypeName> indexedParameters,
        List<org.web3j.codegen.SolidityFunctionWrapper.NamedTypeName> nonIndexedParameters)
        throws ClassNotFoundException {

    String generatedFunctionName =
            Strings.lowercaseFirstLetter(functionName) + "EventObservable";
    ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(ClassName.get(rx
            .Observable.class), ClassName.get("", responseClassName));

    MethodSpec.Builder observableMethodBuilder =
            MethodSpec.methodBuilder(generatedFunctionName)
                    .addModifiers(Modifier.PUBLIC)
                    .addParameter(PlatonFilter.class, FILTER)
                    .returns(parameterizedTypeName);

    TypeSpec converter = TypeSpec.anonymousClassBuilder("")
            .addSuperinterface(ParameterizedTypeName.get(
                    ClassName.get(Func1.class),
                    ClassName.get(Log.class),
                    ClassName.get("", responseClassName)))
            .addMethod(MethodSpec.methodBuilder("call")
                    .addAnnotation(Override.class)
                    .addModifiers(Modifier.PUBLIC)
                    .addParameter(Log.class, "log")
                    .returns(ClassName.get("", responseClassName))
                    .addStatement("$T eventValues = extractEventParametersWithLog("
                                    + buildEventDefinitionName(functionName) + ", log)",
                            Contract.EventValuesWithLog.class)
                    .addStatement("$1T typedResponse = new $1T()",
                            ClassName.get("", responseClassName))
                    .addCode(buildTypedResponse("typedResponse", indexedParameters,
                            nonIndexedParameters, true))
                    .addStatement("return typedResponse")
                    .build())
            .build();

    observableMethodBuilder
            .addStatement("return web3j.platonLogObservable(filter).map($L)", converter);

    return observableMethodBuilder
            .build();
}
 
Example 20
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
private void buildConstantFunction(
        AbiDefinition functionDefinition,
        MethodSpec.Builder methodBuilder,
        List<TypeName> outputParameterTypes,
        String inputParams) throws ClassNotFoundException {

    String functionName = functionDefinition.getName();

    if (outputParameterTypes.isEmpty()) {
        throw new RuntimeException("Only transactional methods should have void return types");
    } else if (outputParameterTypes.size() == 1) {

        TypeName typeName = outputParameterTypes.get(0);
        TypeName nativeReturnTypeName;
        if (useNativeJavaTypes) {
            nativeReturnTypeName = getWrapperRawType(typeName);
        } else {
            nativeReturnTypeName = getWrapperType(typeName);
        }
        methodBuilder.returns(buildRemoteCall(nativeReturnTypeName));

        methodBuilder.addStatement("final $T function = "
                        + "new $T($S, \n$T.<$T>asList($L), "
                        + "\n$T.<$T<?>>asList(new $T<$T>() {}))",
                Function.class, Function.class, functionName,
                Arrays.class, Type.class, inputParams,
                Arrays.class, TypeReference.class,
                TypeReference.class, typeName);

        if (useNativeJavaTypes) {
            if (nativeReturnTypeName.equals(ClassName.get(List.class))) {
                // We return list. So all the list elements should
                // also be converted to native types
                TypeName listType = ParameterizedTypeName.get(List.class, Type.class);

                CodeBlock.Builder callCode = CodeBlock.builder();
                callCode.addStatement(
                        "$T result = "
                        + "($T) executeCallSingleValueReturn(function, $T.class)",
                        listType, listType, nativeReturnTypeName);
                callCode.addStatement("return convertToNative(result)");

                TypeSpec callableType = TypeSpec.anonymousClassBuilder("")
                        .addSuperinterface(ParameterizedTypeName.get(
                                ClassName.get(Callable.class), nativeReturnTypeName))
                        .addMethod(MethodSpec.methodBuilder("call")
                                .addAnnotation(Override.class)
                                .addAnnotation(AnnotationSpec.builder(SuppressWarnings.class)
                                        .addMember("value", "$S", "unchecked")
                                        .build())
                                .addModifiers(Modifier.PUBLIC)
                                .addException(Exception.class)
                                .returns(nativeReturnTypeName)
                                .addCode(callCode.build())
                                .build())
                        .build();

                methodBuilder.addStatement("return new $T(\n$L)",
                        buildRemoteCall(nativeReturnTypeName), callableType);
            } else {
                methodBuilder.addStatement(
                        "return executeRemoteCallSingleValueReturn(function, $T.class)",
                        nativeReturnTypeName);
            }
        } else {
            methodBuilder.addStatement("return executeRemoteCallSingleValueReturn(function)");
        }
    } else {
        List<TypeName> returnTypes = buildReturnTypes(outputParameterTypes);

        ParameterizedTypeName parameterizedTupleType = ParameterizedTypeName.get(
                ClassName.get(
                        "org.web3j.tuples.generated",
                        "Tuple" + returnTypes.size()),
                returnTypes.toArray(
                        new TypeName[returnTypes.size()]));

        methodBuilder.returns(buildRemoteCall(parameterizedTupleType));

        buildVariableLengthReturnFunctionConstructor(
                methodBuilder, functionName, inputParams, outputParameterTypes);

        buildTupleResultContainer(methodBuilder, parameterizedTupleType, outputParameterTypes);
    }
}