javapoet - 如何实现“扩展”和“实现”

时间:2016-01-11 06:17:35

标签: java javapoet

使用Javapoet,如何实现以下内容:

  1. A类扩展了B类

  2. C类实现接口D

  3. 在javadoc中,提到了如何创建接口。

2 个答案:

答案 0 :(得分:21)

使用$path="/var/www/html/aws"; $fileName =$path.'/logs/data'.date('Y-m-d_H-i-s'). '_' . uniqid() . '.txt'; $file = fopen($fileName,'w') or die('Could not create report file: ' . $fileName); foreach($_POST as $key => $value) { $reportLine = $key." = ".$value."\n"; fwrite($file, $reportLine) or die ('Could not write to report file ' . $reportLine); } fputs($file,"log aws"); fclose($file); 表示扩展,TypeSpec.Builder.superclass()表示工具。

答案 1 :(得分:7)

假设您要生成扩展Dummy类并实现Exception接口的Serializable类。生成代码是:

...
TypeSpec typeSpec = TypeSpec.classBuilder("Dummy")
  .addSuperinterface(Serializable.class) 
  .superclass(Exception.class) 
  .build();

JavaFile javaFile = JavaFile.builder("sample.javapoet", typeSpec).build();
...

生成的代码将是:

package sample.javapoet;

import java.io.Serializable;
import java.lang.Exception;

class Hoge extends Exception implements Serializable {
}