use of com.squareup.javapoet.ParameterSpec in project conjure-java by palantir.
the class UnionGenerator method generateEquals.
private static MethodSpec generateEquals(ClassName unionClass) {
ParameterSpec other = ParameterSpec.builder(ClassName.OBJECT, "other").build();
CodeBlock.Builder codeBuilder = CodeBlock.builder().add("return this == $1N || ($1N instanceof $2T && equalTo(($2T) $1N))", other, unionClass);
return MethodSpec.methodBuilder("equals").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(other).returns(TypeName.BOOLEAN).addStatement("$L", codeBuilder.build()).build();
}
use of com.squareup.javapoet.ParameterSpec in project conjure-java by palantir.
the class UnionGenerator method createWrapperAcceptMethod.
private static MethodSpec createWrapperAcceptMethod(ClassName visitorClass, String visitMethodName, String valueName, boolean isDeprecated, Options options) {
ParameterizedTypeName parameterizedVisitorClass = ParameterizedTypeName.get(visitorClass, TYPE_VARIABLE);
ParameterSpec visitor = ParameterSpec.builder(parameterizedVisitorClass, "visitor").build();
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("accept").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addTypeVariable(TYPE_VARIABLE).addParameter(visitor).returns(TYPE_VARIABLE);
if (visitMethodName.equals(VISIT_UNKNOWN_METHOD_NAME) && options.unionsWithUnknownValues()) {
methodBuilder.addStatement("return $N.$N($N, $L)", visitor, visitMethodName, "type", CodeBlock.of("$N.get($N)", VALUE_FIELD_NAME, "type"));
} else {
methodBuilder.addStatement("return $N.$N($N)", visitor, visitMethodName, valueName);
}
if (isDeprecated) {
methodBuilder.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build());
}
return methodBuilder.build();
}
use of com.squareup.javapoet.ParameterSpec in project dialogue by palantir.
the class ServiceImplementationGenerator method clientImpl.
private MethodSpec clientImpl(EndpointDefinition def) {
List<ParameterSpec> params = def.arguments().stream().map(arg -> ParameterSpec.builder(ArgumentTypes.caseOf(arg.argType()).primitive((javaTypeName, _parameterSerializerMethodName, _isList) -> javaTypeName).rawRequestBody(typeName -> typeName).optional((optionalJavaType, _unused) -> optionalJavaType).mapType(typeName -> typeName).customType(typeName -> typeName), arg.argName().get()).build()).collect(Collectors.toList());
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(def.endpointName().get()).addModifiers(Modifier.PUBLIC).addParameters(params).addAnnotation(Override.class);
methodBuilder.addCode("$T $L = $T.builder();", Request.Builder.class, REQUEST, Request.class);
def.arguments().forEach(arg -> methodBuilder.addCode(generateParam(arg)));
methodBuilder.returns(def.returns().returnType());
boolean isAsync = def.returns().asyncInnerType().isPresent();
String executeCode = isAsync ? "$L.clients().call($L, $L.build(), $L);" : "$L.clients().callBlocking($L, $L.build(), $L);";
CodeBlock execute = CodeBlock.of(executeCode, serviceDefinition.conjureRuntimeArgName(), def.channelFieldName(), REQUEST, def.returns().deserializerFieldName());
methodBuilder.addCode(!def.returns().isVoid() || isAsync ? "return $L" : "$L", execute);
return methodBuilder.build();
}
use of com.squareup.javapoet.ParameterSpec in project pnc by project-ncl.
the class PatchDtoGenerator method createReplaceMethod.
private void createReplaceMethod(List<MethodSpec> methods, VariableElement dtoField, String patchBuilderClassName) {
String fieldName = dtoField.getSimpleName().toString();
String methodName = "replace" + StringUtils.firstCharToUpperCase(fieldName);
MethodSpec.Builder methodBuilder = beginMethod(methodName);
ParameterSpec parameterSpec = ParameterSpec.builder(ParameterizedTypeName.get(dtoField.asType()), "element").build();
methodBuilder.addParameter(parameterSpec);
methodBuilder.returns(ClassName.get("org.jboss.pnc.client.patch", patchBuilderClassName));
methodBuilder.addStatement("return replace(element, \"" + fieldName + "\")");
MethodSpec methodSpec = completeMethod(methodBuilder);
methods.add(methodSpec);
}
use of com.squareup.javapoet.ParameterSpec in project AndroidLife by CaMnter.
the class RouteProcessor method parseRoutes.
/**
* 处理 @Route 元素
*
* 0. 准备一下生成 ARouter$$Group$$???,ARouter$$Providers$$??? 和 ARouter$$Root$$???
* - java class 需要的 TypeMirror,TypeElement,ClassName,ParameterizedTypeName,ParameterSpec
* - 以及 MethodSpec
*
* 1. 遍历每一个 @Route 元素
* 2. 拿到每一个 @Route 元素 的 TypeMirror
*
* 3.1 判断是否是 Activity 的子类,是的话
* - 3.1.1 获取该 Element 下的所有 子元素,提取出所有 @Autowired 的 子元素
* - 3.1.2 根据 所有 @Autowired 的 子元素,拿到 所有 @Autowired 的 子元素 的 TypeMirror
* - 3.1.3 根据 所有 @Autowired 的 TypeMirror,获取到 所有 @Autowired 的 子元素 的 TypeKind enum 类型
* - 3.1.4 key = @Autowired 的 name,value = TypeKind enum 类型。缓存成一个 HashMap
* - 3.1.5 用该 HashMap 生成一个 RouteType = Activity 的 RouteMeta
* 3.2 判断是否是 IProvider 的子类,是的话,生成一个 RouteType = IProvider 的 RouteMeta
* 3.3 判断是否是 Service 的子类,是的话,生成一个 RouteType = Service 的 RouteMeta
* 3.4 判断是否是 Fragment 的子类,是的话,生成一个 RouteType = Fragment 的 RouteMeta
*
* 4. 将 RouteMeta 归类
*
* 5. 生成 ARouter$$Group$$??? java class 及其中内容
* 6. 生成 ARouter$$Providers$$??? java class 及其中内容
* 7. 生成 ARouter$$Root$$??? java class 及其中内容
*
* @param routeElements Set<? extends Element>
* @throws IOException IOException
*/
private void parseRoutes(Set<? extends Element> routeElements) throws IOException {
if (CollectionUtils.isNotEmpty(routeElements)) {
// Perpare the type an so on.
logger.info(">>> Found routes, size is " + routeElements.size() + " <<<");
rootMap.clear();
/**
* 实例化 android.app.Activity 的 TypeMirror
* 实例化 android.app.Service 的 TypeMirror
* 实例化 android.app.Fragment 的 TypeMirror
* 实例化 android.support.v4.app.Fragment 的 TypeMirror
*/
TypeMirror type_Activity = elements.getTypeElement(ACTIVITY).asType();
TypeMirror type_Service = elements.getTypeElement(SERVICE).asType();
TypeMirror fragmentTm = elements.getTypeElement(FRAGMENT).asType();
TypeMirror fragmentTmV4 = elements.getTypeElement(Consts.FRAGMENT_V4).asType();
/**
* Interface of ARouter
*
* 实例化 IRouteGroup 的 TypeElement
* 实例化 IProviderGroup 的 TypeElement
* 实例化 RouteMeta 的 ClassName
* 实例化 RouteType 的 ClassName
*/
TypeElement type_IRouteGroup = elements.getTypeElement(IROUTE_GROUP);
TypeElement type_IProviderGroup = elements.getTypeElement(IPROVIDER_GROUP);
ClassName routeMetaCn = ClassName.get(RouteMeta.class);
ClassName routeTypeCn = ClassName.get(RouteType.class);
/**
* Build input type, format as :
*
* ```Map<String, Class<? extends IRouteGroup>>```
*
* ( 用于 ARouter$$Root$$??? )
* @Override
* public void loadInto(Map<String, Class<? extends IRouteGroup>> routes)
* 中的参数类型
* Map<String, Class<? extends IRouteGroup>>
*/
ParameterizedTypeName inputMapTypeOfRoot = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(ClassName.get(type_IRouteGroup))));
/**
* ```Map<String, RouteMeta>```
*
* ( 用于 ARouter$$Group$$??? )
* @Override
* public void loadInto(Map<String, RouteMeta> atlas)
* 中的参数类型
* Map<String, RouteMeta>
*/
ParameterizedTypeName inputMapTypeOfGroup = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(String.class), ClassName.get(RouteMeta.class));
/**
* Build input param name.
*
* 实例化参数:
*
* Map<String, Class<? extends IRouteGroup>> routes( 用于 ARouter$$Root$$??? )
* Map<String, RouteMeta> atlas( 用于 ARouter$$Group$$??? )
* Map<String, RouteMeta> providers( 用于 ARouter$$Providers$$??? )
*/
ParameterSpec rootParamSpec = ParameterSpec.builder(inputMapTypeOfRoot, "routes").build();
ParameterSpec groupParamSpec = ParameterSpec.builder(inputMapTypeOfGroup, "atlas").build();
ParameterSpec providerParamSpec = ParameterSpec.builder(inputMapTypeOfGroup, "providers").build();
/**
* Build method : 'loadInto'
*
* 整个 loadInto 方法( 用于 ARouter$$Root$$??? )
* @Override
* public void loadInto(Map<String, Class<? extends IRouteGroup>> routes)
*/
MethodSpec.Builder loadIntoMethodOfRootBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO).addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(rootParamSpec);
/**
* Follow a sequence, find out metas of group first, generate java file, then statistics them as root.
*
* 1. 遍历每一个 @Route 元素
* 2. 拿到每一个 @Route 元素 的 TypeMirror
*
* 3.1 判断是否是 Activity 的子类,是的话
* - 3.1.1 获取该 Element 下的所有 子元素,提取出所有 @Autowired 的 子元素
* - 3.1.2 根据 所有 @Autowired 的 子元素,拿到 所有 @Autowired 的 子元素 的 TypeMirror
* - 3.1.3 根据 所有 @Autowired 的 TypeMirror,获取到 所有 @Autowired 的 子元素 的 TypeKind enum 类型
* - 3.1.4 key = @Autowired 的 name,value = TypeKind enum 类型。缓存成一个 HashMap
* - 3.1.5 用该 HashMap 生成一个 RouteType = Activity 的 RouteMeta
* 3.2 判断是否是 IProvider 的子类,是的话,生成一个 RouteType = IProvider 的 RouteMeta
* 3.3 判断是否是 Service 的子类,是的话,生成一个 RouteType = Service 的 RouteMeta
* 3.4 判断是否是 Fragment 的子类,是的话,生成一个 RouteType = Fragment 的 RouteMeta
*
* 4. 将 RouteMeta 归类
*
* 5. 生成 ARouter$$Group$$??? java class 及其中内容
* 6. 生成 ARouter$$Providers$$??? java class 及其中内容
* 7. 生成 ARouter$$Root$$??? java class 及其中内容
*/
for (Element element : routeElements) {
TypeMirror tm = element.asType();
Route route = element.getAnnotation(Route.class);
RouteMeta routeMete = null;
if (types.isSubtype(tm, type_Activity)) {
// Activity
logger.info(">>> Found activity route: " + tm.toString() + " <<<");
// Get all fields annotation by @Autowired
Map<String, Integer> paramsType = new HashMap<>();
for (Element field : element.getEnclosedElements()) {
if (field.getKind().isField() && field.getAnnotation(Autowired.class) != null && !types.isSubtype(field.asType(), iProvider)) {
// It must be field, then it has annotation, but it not be provider.
Autowired paramConfig = field.getAnnotation(Autowired.class);
paramsType.put(StringUtils.isEmpty(paramConfig.name()) ? field.getSimpleName().toString() : paramConfig.name(), typeUtils.typeExchange(field));
}
}
routeMete = new RouteMeta(route, element, RouteType.ACTIVITY, paramsType);
} else if (types.isSubtype(tm, iProvider)) {
// IProvider
logger.info(">>> Found provider route: " + tm.toString() + " <<<");
routeMete = new RouteMeta(route, element, RouteType.PROVIDER, null);
} else if (types.isSubtype(tm, type_Service)) {
// Service
logger.info(">>> Found service route: " + tm.toString() + " <<<");
routeMete = new RouteMeta(route, element, RouteType.parse(SERVICE), null);
} else if (types.isSubtype(tm, fragmentTm) || types.isSubtype(tm, fragmentTmV4)) {
logger.info(">>> Found fragment route: " + tm.toString() + " <<<");
routeMete = new RouteMeta(route, element, RouteType.parse(FRAGMENT), null);
}
categories(routeMete);
// if (StringUtils.isEmpty(moduleName)) { // Hasn't generate the module name.
// moduleName = ModuleUtils.generateModuleName(element, logger);
// }
}
/**
* Build method : 'loadInto'
*
* 整个 loadInto 方法( 用于 ARouter$$Providers$$??? )
* @Override
* public void loadInto(Map<String, RouteMeta> providers)
*/
MethodSpec.Builder loadIntoMethodOfProviderBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO).addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(providerParamSpec);
/**
* Start generate java source, structure is divided into upper and lower levels, used for demand initialization.
*/
for (Map.Entry<String, Set<RouteMeta>> entry : groupMap.entrySet()) {
String groupName = entry.getKey();
/**
* 整个 loadInto 方法( 用于 ARouter$$Group$$??? )
* @Override
* public void loadInto(Map<String, RouteMeta> atlas)
*/
MethodSpec.Builder loadIntoMethodOfGroupBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO).addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(groupParamSpec);
// Build group method body
Set<RouteMeta> groupData = entry.getValue();
for (RouteMeta routeMeta : groupData) {
switch(routeMeta.getType()) {
/**
* 在 ARouter$$Providers$$??? # loadInto(Map<String, RouteMeta> providers)
* 添加 缓存语句
* eg:
* providers.put("com.alibaba.android.arouter.demo.testservice.HelloService", RouteMeta.build(RouteType.PROVIDER, HelloServiceImpl.class, "/service/hello", "service", null, -1, -2147483648));
*/
case // Need cache provider's super class
PROVIDER:
List<? extends TypeMirror> interfaces = ((TypeElement) routeMeta.getRawType()).getInterfaces();
for (TypeMirror tm : interfaces) {
if (types.isSameType(tm, iProvider)) {
// Its implements iProvider interface himself.
// This interface extend the IProvider, so it can be used for mark provider
loadIntoMethodOfProviderBuilder.addStatement("providers.put($S, $T.build($T." + routeMeta.getType() + ", $T.class, $S, $S, null, " + routeMeta.getPriority() + ", " + routeMeta.getExtra() + "))", (routeMeta.getRawType()).toString(), routeMetaCn, routeTypeCn, ClassName.get((TypeElement) routeMeta.getRawType()), routeMeta.getPath(), routeMeta.getGroup());
} else if (types.isSubtype(tm, iProvider)) {
// This interface extend the IProvider, so it can be used for mark provider
loadIntoMethodOfProviderBuilder.addStatement("providers.put($S, $T.build($T." + routeMeta.getType() + ", $T.class, $S, $S, null, " + routeMeta.getPriority() + ", " + routeMeta.getExtra() + "))", tm.toString(), // So stupid, will duplicate only save class name.
routeMetaCn, routeTypeCn, ClassName.get((TypeElement) routeMeta.getRawType()), routeMeta.getPath(), routeMeta.getGroup());
}
}
break;
default:
break;
}
/**
* Make map body for paramsType
*
* 在 ARouter$$Group$$??? # loadInto(Map<String, RouteMeta> atlas)
* 中的 缓存语句 键值对语句
* eg:
* atlas.put("/test/activity1", RouteMeta.build(RouteType.ACTIVITY, Test1Activity.class, "/test/activity1", "test", new java.util.HashMap<String, Integer>(){{put("pac", 9); put("obj", 10); put("name", 8); put("boy", 0); put("age", 3); put("url", 8); }}, -1, -2147483648));
* 键值对语句:
* put("pac", 9); put("obj", 10); put("name", 8); put("boy", 0); put("age", 3); put("url", 8);
*/
StringBuilder mapBodyBuilder = new StringBuilder();
Map<String, Integer> paramsType = routeMeta.getParamsType();
if (MapUtils.isNotEmpty(paramsType)) {
for (Map.Entry<String, Integer> types : paramsType.entrySet()) {
mapBodyBuilder.append("put(\"").append(types.getKey()).append("\", ").append(types.getValue()).append("); ");
}
}
String mapBody = mapBodyBuilder.toString();
/**
* 在 ARouter$$Group$$??? # loadInto(Map<String, RouteMeta> atlas)
* 添加 缓存语句
* eg:
* atlas.put("/test/activity1", RouteMeta.build(RouteType.ACTIVITY, Test1Activity.class, "/test/activity1", "test", new java.util.HashMap<String, Integer>(){{put("pac", 9); put("obj", 10); put("name", 8); put("boy", 0); put("age", 3); put("url", 8); }}, -1, -2147483648));
*/
loadIntoMethodOfGroupBuilder.addStatement("atlas.put($S, $T.build($T." + routeMeta.getType() + ", $T.class, $S, $S, " + (StringUtils.isEmpty(mapBody) ? null : ("new java.util.HashMap<String, Integer>(){{" + mapBodyBuilder.toString() + "}}")) + ", " + routeMeta.getPriority() + ", " + routeMeta.getExtra() + "))", routeMeta.getPath(), routeMetaCn, routeTypeCn, ClassName.get((TypeElement) routeMeta.getRawType()), routeMeta.getPath().toLowerCase(), routeMeta.getGroup().toLowerCase());
}
/**
* Generate groups
*
* 生成 java class
* public class ARouter$$Group$$??? implements IRouteGroup
*/
String groupFileName = NAME_OF_GROUP + groupName;
JavaFile.builder(PACKAGE_OF_GENERATE_FILE, TypeSpec.classBuilder(groupFileName).addJavadoc(WARNING_TIPS).addSuperinterface(ClassName.get(type_IRouteGroup)).addModifiers(PUBLIC).addMethod(loadIntoMethodOfGroupBuilder.build()).build()).build().writeTo(mFiler);
logger.info(">>> Generated group: " + groupName + "<<<");
rootMap.put(groupName, groupFileName);
}
/**
* 在 ARouter$$Root$$??? # (Map<String, Class<? extends IRouteGroup>> routes)
* 添加 缓存语句
* eg:
* routes.put("service", ARouter$$Group$$service.class);
*/
if (MapUtils.isNotEmpty(rootMap)) {
// Generate root meta by group name, it must be generated before root, then I can find out the class of group.
for (Map.Entry<String, String> entry : rootMap.entrySet()) {
loadIntoMethodOfRootBuilder.addStatement("routes.put($S, $T.class)", entry.getKey(), ClassName.get(PACKAGE_OF_GENERATE_FILE, entry.getValue()));
}
}
/**
* Write provider into disk
*
* 生成 java class
* public class ARouter$$Providers$$??? implements IProviderGroup
*/
String providerMapFileName = NAME_OF_PROVIDER + SEPARATOR + moduleName;
JavaFile.builder(PACKAGE_OF_GENERATE_FILE, TypeSpec.classBuilder(providerMapFileName).addJavadoc(WARNING_TIPS).addSuperinterface(ClassName.get(type_IProviderGroup)).addModifiers(PUBLIC).addMethod(loadIntoMethodOfProviderBuilder.build()).build()).build().writeTo(mFiler);
logger.info(">>> Generated provider map, name is " + providerMapFileName + " <<<");
/**
* Write provider into disk
*
* 生成 java class
* public class ARouter$$Root$$??? implements IRouteRoot
*/
String rootFileName = NAME_OF_ROOT + SEPARATOR + moduleName;
JavaFile.builder(PACKAGE_OF_GENERATE_FILE, TypeSpec.classBuilder(rootFileName).addJavadoc(WARNING_TIPS).addSuperinterface(ClassName.get(elements.getTypeElement(ITROUTE_ROOT))).addModifiers(PUBLIC).addMethod(loadIntoMethodOfRootBuilder.build()).build()).build().writeTo(mFiler);
logger.info(">>> Generated root, name is " + rootFileName + " <<<");
}
}
Aggregations