use of com.squareup.javapoet.ParameterSpec in project AndroidLife by CaMnter.
the class InterceptorProcessor method parseInterceptors.
/**
* Parse tollgate.
*
* 处理 @Interceptor 元素
*
* 1. 编译每个 @Interceptor 元素
* 2. 验证每个 @Interceptor 元素
* 3. 根据优先级 = key,@Interceptor 元素 为 value,缓存到 TreeMap 内
* 4. JavaPoet 生成 java class
*
* @param elements elements of tollgate.
* @throws IOException IOException
*/
private void parseInterceptors(Set<? extends Element> elements) throws IOException {
if (CollectionUtils.isNotEmpty(elements)) {
logger.info(">>> Found interceptors, size is " + elements.size() + " <<<");
// Verify and cache, sort incidentally.
for (Element element : elements) {
if (verify(element)) {
// Check the interceptor meta
logger.info("A interceptor verify over, its " + element.asType());
Interceptor interceptor = element.getAnnotation(Interceptor.class);
Element lastInterceptor = interceptors.get(interceptor.priority());
if (null != lastInterceptor) {
// Added, throw exceptions
throw new IllegalArgumentException(String.format(Locale.getDefault(), "More than one interceptors use same priority [%d], They are [%s] and [%s].", interceptor.priority(), lastInterceptor.getSimpleName(), element.getSimpleName()));
}
interceptors.put(interceptor.priority(), element);
} else {
logger.error("A interceptor verify failed, its " + element.asType());
}
}
/**
* 获取 IInterceptor 的 TypeElement
* 获取 IInterceptorGroup 的 TypeElement
*/
// Interface of ARouter.
TypeElement type_ITollgate = elementUtil.getTypeElement(IINTERCEPTOR);
TypeElement type_ITollgateGroup = elementUtil.getTypeElement(IINTERCEPTOR_GROUP);
/**
* Build input type, format as :
*
* ```Map<Integer, Class<? extends ITollgate>>```
*
* @Override
* public void loadInto(Map<Integer, Class<? extends IInterceptor>> interceptors)
* 中的参数类型
* Map<Integer, Class<? extends IInterceptor>>
*/
ParameterizedTypeName inputMapTypeOfTollgate = ParameterizedTypeName.get(ClassName.get(Map.class), ClassName.get(Integer.class), ParameterizedTypeName.get(ClassName.get(Class.class), WildcardTypeName.subtypeOf(ClassName.get(type_ITollgate))));
/**
* @Override
* public void loadInto(Map<Integer, Class<? extends IInterceptor>> interceptors)
* 中的参数
* Map<Integer, Class<? extends IInterceptor>> interceptors
*/
// Build input param name.
ParameterSpec tollgateParamSpec = ParameterSpec.builder(inputMapTypeOfTollgate, "interceptors").build();
/**
* 整个 loadInto 方法
* @Override
* public void loadInto(Map<Integer, Class<? extends IInterceptor>> interceptors)
*/
// Build method : 'loadInto'
MethodSpec.Builder loadIntoMethodOfTollgateBuilder = MethodSpec.methodBuilder(METHOD_LOAD_INTO).addAnnotation(Override.class).addModifiers(PUBLIC).addParameter(tollgateParamSpec);
// Generate
if (null != interceptors && interceptors.size() > 0) {
// Build method body
for (Map.Entry<Integer, Element> entry : interceptors.entrySet()) {
loadIntoMethodOfTollgateBuilder.addStatement("interceptors.put(" + entry.getKey() + ", $T.class)", ClassName.get((TypeElement) entry.getValue()));
}
}
/**
* 生成 java class
*
* public class ???$$Interceptors$$app implements IInterceptorGroup
*/
// Write to disk(Write file even interceptors is empty.)
JavaFile.builder(PACKAGE_OF_GENERATE_FILE, TypeSpec.classBuilder(NAME_OF_INTERCEPTOR + SEPARATOR + moduleName).addModifiers(PUBLIC).addJavadoc(WARNING_TIPS).addMethod(loadIntoMethodOfTollgateBuilder.build()).addSuperinterface(ClassName.get(type_ITollgateGroup)).build()).build().writeTo(mFiler);
logger.info(">>> Interceptor group write over. <<<");
}
}
use of com.squareup.javapoet.ParameterSpec in project arez by arez.
the class ComponentGenerator method buildInverseUnsetMethod.
@Nonnull
private static MethodSpec buildInverseUnsetMethod(@Nonnull final InverseDescriptor inverse) throws ProcessorException {
final ObservableDescriptor observable = inverse.getObservable();
final String methodName = Multiplicity.ONE == inverse.getMultiplicity() ? getInverseUnsetMethodName(observable.getName()) : getInverseZUnsetMethodName(observable.getName());
final MethodSpec.Builder builder = MethodSpec.methodBuilder(methodName);
if (ElementsUtil.areTypesInDifferentPackage(inverse.getTargetType(), inverse.getComponent().getElement())) {
builder.addModifiers(Modifier.PUBLIC);
}
final String otherName = inverse.getOtherName();
final ParameterSpec parameter = ParameterSpec.builder(TypeName.get(inverse.getTargetType().asType()), otherName, Modifier.FINAL).addAnnotation(GeneratorUtil.NONNULL_CLASSNAME).build();
builder.addParameter(parameter);
generateNotDisposedInvariant(builder, methodName);
builder.addStatement("this.$N.preReportChanged()", observable.getFieldName());
final CodeBlock.Builder block = CodeBlock.builder();
block.beginControlFlow("if ( this.$N == $N )", observable.getDataFieldName(), otherName);
for (final ExecutableElement hook : inverse.getPreInverseRemoveHooks()) {
block.addStatement("$N( $N )", hook.getSimpleName(), otherName);
}
block.addStatement("this.$N = null", observable.getDataFieldName());
block.addStatement("this.$N.reportChanged()", observable.getFieldName());
block.endControlFlow();
builder.addCode(block.build());
return builder.build();
}
use of com.squareup.javapoet.ParameterSpec in project AndroidQuick by ddnosh.
the class AnnotatedClass method generateFile.
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();
}
use of com.squareup.javapoet.ParameterSpec in project celesta by CourseOrchestra.
the class CursorGenerator method buildConstructors.
private static List<MethodSpec> buildConstructors(GrainElement ge, TypeName columnsClassType) {
List<MethodSpec> results = new ArrayList<>();
ParameterSpec contextParam = ParameterSpec.builder(CallContext.class, "context").build();
ParameterSpec fieldsParam = ParameterSpec.builder(ParameterizedTypeName.get(Set.class, String.class), "fields").build();
ParameterSpec columnsParam = ParameterSpec.builder(ArrayTypeName.of(ParameterizedTypeName.get(ClassName.get(ColumnMeta.class), WildcardTypeName.subtypeOf(Object.class))), "columns").build();
ParameterSpec parametersParam = ParameterSpec.builder(ParameterizedTypeName.get(Map.class, String.class, Object.class), "parameters").build();
Supplier<MethodSpec.Builder> msp = () -> MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(contextParam);
// Common constructor
MethodSpec.Builder builder = msp.get();
if (ge instanceof ParameterizedView) {
builder.addParameter(parametersParam);
builder.addStatement("super(context, parameters)");
} else {
builder.addStatement("super(context)");
}
results.add(builder.build());
if (ge instanceof SequenceElement) {
return results;
}
// Constructor with columns limitation
builder = msp.get();
if (ge instanceof ParameterizedView) {
builder.addParameter(parametersParam);
builder.addParameter(columnsParam).varargs();
builder.addStatement("super(context, parameters, columns)");
} else {
builder.addParameter(columnsParam).varargs();
builder.addStatement("super(context, columns)");
}
results.add(builder.build());
// Deprecated constructor with fields limitation
builder = msp.get();
if (ge instanceof ParameterizedView) {
builder.addParameter(fieldsParam);
builder.addParameter(parametersParam);
builder.addStatement("super(context, fields, parameters)");
} else {
builder.addParameter(fieldsParam);
builder.addStatement("super(context, fields)");
}
results.add(builder.addAnnotation(Deprecated.class).build());
// ParameterizedView constructors
if (ge instanceof ParameterizedView) {
ParameterizedView pv = (ParameterizedView) ge;
builder = msp.get();
for (Parameter parameter : pv.getParameters().values()) {
builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
}
String spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "))";
builder.addStatement(spec, pv.getParameters().keySet().toArray());
results.add(builder.build());
builder = msp.get();
for (Parameter parameter : pv.getParameters().values()) {
builder.addParameter(ParameterSpec.builder(parameter.getJavaClass(), parameter.getName()).build());
}
builder.addParameter(columnsParam).varargs();
spec = "super (context, paramsMap(" + pv.getParameters().values().stream().map(c -> "$N").collect(Collectors.joining(", ")) + "), columns)";
builder.addStatement(spec, pv.getParameters().keySet().toArray());
results.add(builder.build());
results.add(getParameterizedViewTypedConstructorHelper(pv));
}
return results;
}
use of com.squareup.javapoet.ParameterSpec in project celesta by CourseOrchestra.
the class CursorGenerator method buildClearBuffer.
private MethodSpec buildClearBuffer(Map<String, ? extends ColumnMeta<?>> columns, Set<Column<?>> pk) {
ParameterSpec param = ParameterSpec.builder(boolean.class, "withKeys").build();
MethodSpec.Builder builder = MethodSpec.methodBuilder("_clearBuffer").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(param);
if (!pk.isEmpty()) {
builder.beginControlFlow("if ($N)", param.name);
pk.stream().forEach(c -> builder.addStatement("this.$N = null", camelize(c.getName())));
builder.endControlFlow();
}
columns.entrySet().stream().filter(e -> !pk.contains(e.getValue())).forEach(e -> builder.addStatement("this.$N = null", camelize(e.getKey())));
return builder.build();
}
Aggregations