commit | 3b302bd79ea75e22e21abf61d2366785d5d8d1c7 | [log] [tgz] |
---|---|---|
author | Jake Wharton <jw@squareup.com> | Wed Jan 14 11:09:17 2015 -0500 |
committer | Jake Wharton <jw@squareup.com> | Wed Jan 14 11:09:17 2015 -0500 |
tree | 0885423d9963a0b8fc1e23fa768695bf2b226171 | |
parent | 29e2cdd0b69da1d212ab2b21e58f0885a0ec9235 [diff] |
Fix up example code.
JavaPoet
is a Java API for generating .java
source files.
Source file generation can useful when doing things such as annotation processing or interacting with metadata files (e.g., database schemas, protocol formats). By generating code, you eliminate the need to write boilerplate while also keeping a single source of truth for the metadata.
TypeSpec raven = TypeSpec.classBuilder("Raven") .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .addMethod(MethodSpec.methodBuilder("main") .addModifiers(Modifier.PUBLIC) .addCode("$T verses = new $T();\n", Types.parameterizedType(List.class, String.class), Types.parameterizedType(ArrayList.class, String.class)) .addCode("verses.add($S);\n", "Once upon a midnight dreary, while I pondered, weak and weary...") .addCode("verses.add($S);\n", "Over many a quaint and curious volume of forgotten lore—") .addCode("System.out.println(verses);\n") .build()) .build(); JavaFile ravenSourceFile = new JavaFile.Builder() .packageName("com.squareup.poe") .typeSpec(raven) .build();
Would produce the following source output:
package com.squareup.poe; import java.util.ArrayList; import java.util.List; public final class Raven { public void main() { List<String> verses = new ArrayList<String>(); verses.add("Once upon a midnight dreary, while I pondered, weak and weary..."); verses.add("Over many a quaint and curious volume of forgotten lore—"); System.out.println(verses); } }
Download the latest .jar or depend via Maven:
<dependency> <groupId>com.squareup</groupId> <artifactId>javapoet</artifactId> <version>1.0.0</version> </dependency>
or Gradle:
compile 'com.squareup:javapoet:1.0.0'
Snapshots of the development version are available in Sonatype's snapshots
repository.
Copyright 2015 Square, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.