JavaPoet鍏ラ棬
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaPoet鍏ラ棬相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/final' title='final'>final
classname 鍚嶇О dsp api vat cep form types浠嬬粛
JavaPoet 鏄竴涓敤鏉ョ敓鎴?.java婧愭枃浠剁殑Java API銆?/p>
浣跨敤
maven渚濊禆
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.11.1</version>
</dependency>
鍒涘缓demo
import javax.lang.model.element.Modifier;
public class Client {
private static final String PACKAGE = "com.imooc.sourcecode.java.javapoet.test2";
public static void main(String[] args) {
testCreateUser();
testCreateClient();
}
//鍒涘缓涓€涓皟鐢ㄥ疄浣撶被鐨勭被
private static void testCreateClient() {
//瀹氫箟瀹炰綋绫?
ClassName user = ClassName.get(PACKAGE, "User");
//鍒涘缓浠g爜鍧? CodeBlock mainCodeBlock = CodeBlock
.builder()
.addStatement("$T $L = new $T", user, "user", user)
.addStatement("$T.out.println($L.$L())", System.class, "user", "getUserName")
.build();
//鍒涘缓main鏂规硶
MethodSpec mainMethod = MethodSpec
.methodBuilder("main")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addParameter(String[].class, "args")
.addCode(mainCodeBlock)
.build();
//鍒涘缓绫? TypeSpec client = TypeSpec.classBuilder("Client")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
.addMethod(mainMethod)
.build();
JavaFile javaFile = JavaFile.builder(PACKAGE, client)
.build();
System.out.println(javaFile.toString());
}
//鍒涘缓涓€涓甫娉ㄨВ鐨勫疄浣撶被
private static void testCreateUser() {
//鍒涘缓涓€涓睘鎬? FieldSpec userNameField = FieldSpec
.builder(String.class, "userName") // 瀹氫箟灞炴€х被鍨嬪拰灞炴€у悕绉? .addModifiers(Modifier.PRIVATE) // 瀹氫箟灞炴€т慨楗扮
.build();
//鍒涘缓涓€涓棤鍙傛瀯閫犳柟娉? MethodSpec noArgsContructor = MethodSpec
.constructorBuilder()
.addModifiers(Modifier.PUBLIC) // 瀹氫箟灞炴€т慨楗扮
.build();
//鍒涘缓涓€涓弬鏁? ParameterSpec userNameParam = ParameterSpec
.builder(String.class, "userName") // 瀹氫箟鍙傛暟绫诲瀷鍜屽弬鏁板悕绉? .build();
//鍒涘缓涓€涓湁鍙傛瀯閫犳柟娉? MethodSpec allArgsContructor = MethodSpec
.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(userNameParam)
.addStatement("this.$N = $N", userNameParam, userNameParam) // 瀹氫箟璇彞
.build();
//鍒涘缓涓€涓敞瑙? AnnotationSpec getterAnnotation = AnnotationSpec
.builder(Getter.class)
.build();
//鍒涘缓涓€涓敞瑙? AnnotationSpec setterAnnotation = AnnotationSpec
.builder(Setter.class)
.build();
//鍒涘缓涓€涓被
TypeSpec user = TypeSpec.classBuilder("User")
.addModifiers(Modifier.PUBLIC, Modifier.FINAL) // 瀹氫箟绫讳慨楗扮
.addField(userNameField) // 娣诲姞灞炴€? .addAnnotation(getterAnnotation) // 娣诲姞娉ㄨВ
.addAnnotation(setterAnnotation)
.addMethod(noArgsContructor) // 娣诲姞鏋勯€犳柟娉? .addMethod(allArgsContructor)
.build();
JavaFile javaFile = JavaFile.builder(PACKAGE, user)
.build();
//浠g爜鐢熸垚锛岃浆鎹㈡垚瀛楃涓? System.out.println(javaFile.toString());
}
}
鐪嬩竴涓婮avaPoet鍐呴儴鏄浣曞鐞嗗弬鏁扮殑
private void addArgument(String format, char c, Object arg) {
switch (c) {
//$N琛ㄧず鑾峰彇鍙傛暟鐨刵ame
case 鈥楴鈥?
this.args.add(argToName(arg));
break;
//$L琛ㄧず瀛楅潰鎰忎箟锛屽師鏍疯緭鍑? case 鈥楲鈥?
this.args.add(argToLiteral(arg));
break;
//$S琛ㄧず杞垚瀛楃涓? case 鈥楽鈥?
this.args.add(argToString(arg));
break;
//$T琛ㄧず杞垚绫诲瀷锛屽苟鑷姩import
case 鈥楾鈥?
this.args.add(argToType(arg));
break;
default:
throw new IllegalArgumentException(
String.format("invalid format string: 鈥?s鈥?quot;, format));
}
}
private String argToName(Object o) {
if (o instanceof CharSequence) return o.toString();
if (o instanceof ParameterSpec) return ((ParameterSpec) o).name;
if (o instanceof FieldSpec) return ((FieldSpec) o).name;
if (o instanceof MethodSpec) return ((MethodSpec) o).name;
if (o instanceof TypeSpec) return ((TypeSpec) o).name;
throw new IllegalArgumentException("expected name but was " + o);
}
鍒涘缓缁撴灉涓?/p>
package com.imooc.sourcecode.java.javapoet.test2;
import java.lang.String;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public final class User {
private String userName;
public User() {
}
public User(String userName) {
this.userName = userName;
}
}
package com.imooc.sourcecode.java.javapoet.test2;
import java.lang.String;
import java.lang.System;
public final class Client {
public static void main(String[] args) {
User user = new User;
System.out.println(user.getUserName());
}
}
鍙互鐪嬪埌浣跨敤JavaPoet鏉ョ敓鎴恓ava婧愮爜杩樻槸闈炲父绠€鍗曠殑銆?/p>
以上是关于JavaPoet鍏ラ棬的主要内容,如果未能解决你的问题,请参考以下文章