JavaPoet

Javapoet superclass generic

若如初见. 提交于 2019-12-04 20:36:56
问题 Anyone know how I can do the following using javapoet public class MyClassGenerated extends MyMapper<OtherClass>{ } My code of generation: TypeSpec generateClass() { return classBuilder("MyClassGenerated") .addModifiers(PUBLIC) .superclass(???????????????) .build(); } 回答1: The ParameterizedTypeName class allows you to specify generic type arguments when declaring the super class. For instance, if your MyClassGenerated class is a subclass of the MyMapper class, you can set a generic type

How to add static section in to java class in javapoet

╄→гoц情女王★ 提交于 2019-12-04 04:47:39
问题 Is there a anyway to add static code block into java class using javapoet library static { // whatever code is needed for initialization goes here } 回答1: Use TypeSpec.Builder::addStaticBlock. See test case. 来源: https://stackoverflow.com/questions/34319246/how-to-add-static-section-in-to-java-class-in-javapoet

Android 学习使用annotationprocessor自动生成java文件

泄露秘密 提交于 2019-12-04 02:04:37
最近看glide源码,发现里面有个类必须用到的,没在源码里面,居然在build/generated目录下,这里面是自动生成的Java文件,比如R文件。 奇了个怪了,通过查阅大神文章知道了原来是利用了annotationprocessor编译器,在编译期间创建的,用到这个的出名框架比如:Butter Knife、Glide 。 注意:android-apt这个插件官方已经宣布不再维护,插件gradle2.2以上的版本使用annotationprocessor,所以我们这里跟着官方走。 否则报错: 现在我们来生成一个超级简单的Java文件吧 首先创建一个安卓项目如图: 主工程build.gradle文件如下 apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.example.alex.annotationprocessordemo" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile

Javapoet superclass generic

谁说胖子不能爱 提交于 2019-12-03 12:44:33
Anyone know how I can do the following using javapoet public class MyClassGenerated extends MyMapper<OtherClass>{ } My code of generation: TypeSpec generateClass() { return classBuilder("MyClassGenerated") .addModifiers(PUBLIC) .superclass(???????????????) .build(); } El Hoss The ParameterizedTypeName class allows you to specify generic type arguments when declaring the super class. For instance, if your MyClassGenerated class is a subclass of the MyMapper class, you can set a generic type parameter of MyMapper like so: TypeSpec classSpec = classBuilder("MyClassGenerated") .addModifiers(PUBLIC

How to add static section in to java class in javapoet

时光总嘲笑我的痴心妄想 提交于 2019-12-02 00:03:49
Is there a anyway to add static code block into java class using javapoet library static { // whatever code is needed for initialization goes here } Use TypeSpec.Builder::addStaticBlock . See test case . 来源: https://stackoverflow.com/questions/34319246/how-to-add-static-section-in-to-java-class-in-javapoet

how to generate symbol Class<?> with javapoet

泪湿孤枕 提交于 2019-11-30 20:33:14
i want to generate a field like this: public static Map<String, Class<?>> ID_MAP = new HashMap<String, Class<?>>(); WildcardTypeName.subtypeOf(Object.class) can give '?' WildcardTypeName.subtypeOf(Class.class) can give 'Class' If you break down that type into its component parts you get: ? Class Class<?> String Map Map<String, Class<?>> You can then build up these component parts in the same way using JavaPoet's APIs: TypeName wildcard = WildcardTypeName.subtypeOf(Object.class); TypeName cls = ClassName.get(Class.class); TypeName clsWildcard = ParameterizedTypeName.create(cls, wildcard);

how to generate symbol Class<?> with javapoet

冷暖自知 提交于 2019-11-30 05:17:14
问题 i want to generate a field like this: public static Map<String, Class<?>> ID_MAP = new HashMap<String, Class<?>>(); WildcardTypeName.subtypeOf(Object.class) can give '?' WildcardTypeName.subtypeOf(Class.class) can give 'Class' 回答1: If you break down that type into its component parts you get: ? Class Class<?> String Map Map<String, Class<?>> You can then build up these component parts in the same way using JavaPoet's APIs: TypeName wildcard = WildcardTypeName.subtypeOf(Object.class); TypeName

Android注解的理解与使用

南笙酒味 提交于 2019-11-30 03:47:12
为什么是注解 作为 Android 开发人员,先看一些熟悉的代码: setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.xxx, parent, false))); 像这样的不可省略代码,大量出现在各个地方,一遍一遍的被复制,复制过程中改变很少,或者根本没有改变。在计算机编程中称之为 样板代码 。经常使用类似的 样板 ,直接后果导致程序员编写更多代码,做更少的工作。这显然是不可以忍受的。 从 《重构改善既有代码的设计》章节三:代码的坏味道 中,我们学到,多个相同的程序结构,将他们合二为一,程序会变得更好。最直观体现就是使用工具类抽取公共方法各处调用。这样就可以更少的代码,更高的效率了。类似的,使用注解解决 Android 中的样板代码问题。 上面的引子的目的是要直观地说明,为什么需要注解?术语话一些如下: > 每当你创建描述符性质的类和接口的时,一旦其中包含重复性工作,就可以考虑简化与自动化该过程。 > 如果你想为应用设置很多的常量或参数,xml是一个很好的选择,因为它不会同特定的代码相连。如果你想把某个方法声明为服务