use of com.squareup.javapoet.FieldSpec in project T-MVP by north2016.
the class RouterProcessor method process.
@Override
public void process(RoundEnvironment roundEnv, AnnotationProcessor mAbstractProcessor) {
String CLASS_NAME = "TRouter";
TypeSpec.Builder tb = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL).addJavadoc("@ 全局路由器 此类由apt自动生成");
FieldSpec extraField = FieldSpec.builder(ParameterizedTypeName.get(HashMap.class, String.class, Object.class), "mCurActivityExtra").addModifiers(Modifier.PUBLIC, Modifier.STATIC).build();
tb.addField(extraField);
MethodSpec.Builder methodBuilder1 = MethodSpec.methodBuilder("go").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(String.class, "name").addParameter(HashMap.class, "extra").addParameter(ClassName.get("android.view", "View"), "view");
MethodSpec.Builder methodBuilder2 = MethodSpec.methodBuilder("bind").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(ClassName.get("android.app", "Activity"), "mContext");
List<ClassName> mList = new ArrayList<>();
CodeBlock.Builder blockBuilderGo = CodeBlock.builder();
CodeBlock.Builder blockBuilderBind = CodeBlock.builder();
ClassName appClassName = ClassName.get("com", "App");
blockBuilderGo.addStatement("mCurActivityExtra=extra");
blockBuilderGo.addStatement("Activity mContext=$T.getAppContext().getCurActivity()", appClassName);
// 括号开始
blockBuilderGo.beginControlFlow(" switch (name)");
blockBuilderBind.addStatement("if(mCurActivityExtra==null) return");
// 括号开始
blockBuilderBind.beginControlFlow(" switch (mContext.getClass().getSimpleName())");
List<RouterActivityModel> mRouterActivityModels = new ArrayList<>();
try {
for (TypeElement element : ElementFilter.typesIn(roundEnv.getElementsAnnotatedWith(Router.class))) {
ClassName currentType = ClassName.get(element);
if (mList.contains(currentType))
continue;
mList.add(currentType);
RouterActivityModel mRouterActivityModel = new RouterActivityModel();
mRouterActivityModel.setElement(element);
mRouterActivityModel.setActionName(element.getAnnotation(Router.class).value());
List<Element> mExtraElements = new ArrayList<>();
List<String> mExtraElementKeys = new ArrayList<>();
for (Element childElement : element.getEnclosedElements()) {
SceneTransition mSceneTransitionAnnotation = childElement.getAnnotation(SceneTransition.class);
if (mSceneTransitionAnnotation != null) {
mRouterActivityModel.setSceneTransitionElementName(mSceneTransitionAnnotation.value());
mRouterActivityModel.setSceneTransitionElement(childElement);
}
Extra mExtraAnnotation = childElement.getAnnotation(Extra.class);
if (mExtraAnnotation != null) {
mExtraElementKeys.add(mExtraAnnotation.value());
mExtraElements.add(childElement);
}
}
mRouterActivityModel.setExtraElementKeys(mExtraElementKeys);
mRouterActivityModel.setExtraElements(mExtraElements);
boolean isNeedBind = (mExtraElementKeys != null && mExtraElementKeys.size() > 0 || mRouterActivityModel.getSceneTransitionElement() != null);
mRouterActivityModel.setNeedBind(isNeedBind);
mRouterActivityModels.add(mRouterActivityModel);
}
ClassName mActivityCompatName = ClassName.get("android.support.v4.app", "ActivityCompat");
ClassName mIntentClassName = ClassName.get("android.content", "Intent");
ClassName mActivityOptionsCompatName = ClassName.get("android.support.v4.app", "ActivityOptionsCompat");
for (RouterActivityModel item : mRouterActivityModels) {
// 1
blockBuilderGo.add("case $S: \n", item.getActionName());
if (item.isNeedBind())
// 1
blockBuilderBind.add("case $S: \n", item.getElement().getSimpleName());
if (item.getExtraElements() != null && item.getExtraElements().size() > 0) {
for (int i = 0; i < item.getExtraElements().size(); i++) {
Element mFiled = item.getExtraElements().get(i);
blockBuilderBind.add(// 1
"(($T)mContext)." + // 2
"$L" + // 3
"= ($T) " + // 4
"mCurActivityExtra.get(" + // 5
"$S);\n", // 1
item.getElement(), // 2
mFiled, // 3
mFiled, // 5
item.getExtraElementKeys().get(i));
// 5
}
}
if (item.getSceneTransitionElement() != null) {
blockBuilderGo.add(// 2
"$L.startActivity(mContext," + // 3
"\nnew $L(mContext," + // 4
"\n$L.class)," + // 5
"\n$T.makeSceneTransitionAnimation(" + // 6
"\nmContext,view," + // 7
"\n$S).toBundle());", // 2
mActivityCompatName, // 3
mIntentClassName, // 4
item.getElement(), // 5
mActivityOptionsCompatName, // 6
item.getSceneTransitionElementName());
blockBuilderBind.add(// 2
"$T.setTransitionName(" + // 3
"(($T)mContext).mViewBinding." + // 4
"$L, " + // 5
"$S);\n", // 2
ClassName.get("android.support.v4.view", "ViewCompat"), // 3
item.getElement(), // 4
item.getSceneTransitionElement(), // 5
item.getSceneTransitionElementName());
} else {
blockBuilderGo.add(// 2
"mContext.startActivity(" + // 3
"\nnew $L(mContext," + // 7
"\n$L.class));", // 3
mIntentClassName, // 4
item.getElement());
}
// 1
blockBuilderGo.addStatement("\nbreak");
// 1
if (item.isNeedBind())
blockBuilderBind.addStatement("break");
}
blockBuilderGo.addStatement("default: break");
blockBuilderGo.endControlFlow();
methodBuilder1.addCode(blockBuilderGo.build());
blockBuilderBind.addStatement("default: break");
blockBuilderBind.endControlFlow();
methodBuilder2.addCode(blockBuilderBind.build());
tb.addMethod(methodBuilder1.build());
tb.addMethod(methodBuilder2.build());
// 增加go(action)和go(action,extra):两个重载方法
tb.addMethod(MethodSpec.methodBuilder("go").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(String.class, "name").addParameter(HashMap.class, "extra").addCode("go(name,extra,null);\n").build());
tb.addMethod(MethodSpec.methodBuilder("go").addJavadoc("@此方法由apt自动生成").addModifiers(PUBLIC, STATIC).addParameter(String.class, "name").addCode("go(name,null,null);\n").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();
}
}
use of com.squareup.javapoet.FieldSpec in project web3sdk by FISCO-BCOS.
the class AbiTypesGenerator method generateFixedTypes.
private <T extends Type> void generateFixedTypes(Class<T> superclass, Path path) throws IOException {
String packageName = createPackageName(superclass);
ClassName className;
for (int mBitSize = 8; mBitSize < Type.MAX_BIT_LENGTH; mBitSize += 8) {
inner: for (int nBitSize = 8; nBitSize < Type.MAX_BIT_LENGTH; nBitSize += 8) {
if (mBitSize + nBitSize > Type.MAX_BIT_LENGTH) {
break inner;
}
MethodSpec constructorSpec1 = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(BigInteger.class, "value").addStatement("super($L, $L, $N)", mBitSize, nBitSize, "value").build();
MethodSpec constructorSpec2 = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(int.class, "mBitSize").addParameter(int.class, "nBitSize").addParameter(BigInteger.class, "m").addParameter(BigInteger.class, "n").addStatement("super($L, $L, $N, $N)", mBitSize, nBitSize, "m", "n").build();
className = ClassName.get(packageName, superclass.getSimpleName() + mBitSize + "x" + nBitSize);
FieldSpec defaultFieldSpec = FieldSpec.builder(className, DEFAULT, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("new $T(BigInteger.ZERO)", className).build();
TypeSpec fixedType = TypeSpec.classBuilder(className.simpleName()).addJavadoc(CODEGEN_WARNING).superclass(superclass).addModifiers(Modifier.PUBLIC).addField(defaultFieldSpec).addMethod(constructorSpec1).addMethod(constructorSpec2).build();
write(packageName, fixedType, path);
}
}
}
use of com.squareup.javapoet.FieldSpec in project web3sdk by FISCO-BCOS.
the class AbiTypesGenerator method generateBytesTypes.
private <T extends Type> void generateBytesTypes(Class<T> superclass, Path path) throws IOException {
String packageName = createPackageName(superclass);
ClassName className;
for (int byteSize = 1; byteSize <= 32; byteSize++) {
MethodSpec constructorSpec = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(byte[].class, "value").addStatement("super($L, $N)", byteSize, "value").build();
className = ClassName.get(packageName, superclass.getSimpleName() + byteSize);
FieldSpec defaultFieldSpec = FieldSpec.builder(className, DEFAULT, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("new $T(new byte[$L])", className, byteSize).build();
TypeSpec bytesType = TypeSpec.classBuilder(className.simpleName()).addJavadoc(CODEGEN_WARNING).superclass(superclass).addModifiers(Modifier.PUBLIC).addField(defaultFieldSpec).addMethod(constructorSpec).build();
write(packageName, bytesType, path);
}
}
use of com.squareup.javapoet.FieldSpec in project web3sdk by FISCO-BCOS.
the class AbiTypesGenerator method generateIntTypes.
private <T extends Type> void generateIntTypes(Class<T> superclass, Path path) throws IOException {
String packageName = createPackageName(superclass);
ClassName className;
for (int bitSize = 8; bitSize <= Type.MAX_BIT_LENGTH; bitSize += 8) {
className = ClassName.get(packageName, superclass.getSimpleName() + bitSize);
MethodSpec constructorSpec = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(BigInteger.class, "value").addStatement("super($L, $N)", bitSize, "value").build();
MethodSpec overideConstructorSpec = MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(long.class, "value").addStatement("this(BigInteger.valueOf(value))").build();
FieldSpec defaultFieldSpec = FieldSpec.builder(className, DEFAULT, Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL).initializer("new $T(BigInteger.ZERO)", className).build();
TypeSpec intType = TypeSpec.classBuilder(className.simpleName()).addJavadoc(CODEGEN_WARNING).superclass(superclass).addModifiers(Modifier.PUBLIC).addField(defaultFieldSpec).addMethods(Arrays.asList(constructorSpec, overideConstructorSpec)).build();
write(packageName, intType, path);
}
}
use of com.squareup.javapoet.FieldSpec in project potato by eyeem.
the class CodeWriter method process.
private void process() throws ClassNotFoundException, IOException {
// Build fields
ArrayList<FieldSpec> fields = new ArrayList<>();
FieldSpec field;
//FieldSpec.builder(String.class, "instance", Modifier.PRIVATE, Modifier.STATIC);
field = FieldSpec.builder(generatedClass, "instance").addModifiers(Modifier.PRIVATE, Modifier.STATIC).build();
fields.add(field);
// Build methods
ArrayList<MethodSpec> methods = new ArrayList<>();
MethodSpec method;
method = MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).addParameter(context, "context").addStatement("super(context)").build();
methods.add(method);
method = MethodSpec.methodBuilder("id").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(dataClass, "object").returns(String.class).addStatement("return object." + id).build();
methods.add(method);
method = MethodSpec.methodBuilder("classname").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(Class.class).addStatement("return $T.class", dataClass).build();
methods.add(method);
method = MethodSpec.methodBuilder("get").addModifiers(Modifier.PUBLIC, Modifier.STATIC).addParameter(context, "context").returns(generatedClass).addStatement("if(instance == null) initialise(context)").addStatement("return instance").build();
methods.add(method);
method = MethodSpec.methodBuilder("initialise").addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.SYNCHRONIZED).addParameter(context, "context").beginControlFlow("if(instance == null)").addStatement("instance = new $T(context)", generatedClass).addStatement("instance.init()").endControlFlow().build();
methods.add(method);
// build class
TypeSpec.Builder builder = TypeSpec.classBuilder(generatedClassName).addModifiers(Modifier.PUBLIC).superclass(ParameterizedTypeName.get(ClassName.get("com.eyeem.storage", "Storage"), dataClass));
for (FieldSpec fieldSpec : fields) builder.addField(fieldSpec);
for (MethodSpec methodSpec : methods) builder.addMethod(methodSpec);
TypeSpec typeSpec = builder.build();
JavaFile javaFile = JavaFile.builder(packageName, typeSpec).build();
Writer writer = filer.createSourceFile(packageName + "." + generatedClassName).openWriter();
javaFile.writeTo(writer);
writer.close();
}
Aggregations