下面列出了怎么用java.lang.Deprecated的API类实例代码及写法,或者点击链接到github查看源代码。
/** https://github.com/square/javapoet/issues/253 */
@Test public void enumWithAnnotatedValues() throws Exception {
TypeSpec roshambo = TypeSpec.enumBuilder("Roshambo")
.addModifiers(Modifier.PUBLIC)
.addEnumConstant("ROCK", TypeSpec.anonymousClassBuilder("")
.addAnnotation(Deprecated.class)
.build())
.addEnumConstant("PAPER")
.addEnumConstant("SCISSORS")
.build();
assertThat(toString(roshambo)).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "import java.lang.Deprecated;\n"
+ "\n"
+ "public enum Roshambo {\n"
+ " @Deprecated\n"
+ " ROCK,\n"
+ "\n"
+ " PAPER,\n"
+ "\n"
+ " SCISSORS\n"
+ "}\n");
}
@Test public void innerAnnotationInAnnotationDeclaration() throws Exception {
TypeSpec bar = TypeSpec.annotationBuilder("Bar")
.addMethod(MethodSpec.methodBuilder("value")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.defaultValue("@$T", Deprecated.class)
.returns(Deprecated.class)
.build())
.build();
assertThat(toString(bar)).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "import java.lang.Deprecated;\n"
+ "\n"
+ "@interface Bar {\n"
+ " Deprecated value() default @Deprecated;\n"
+ "}\n"
);
}
@Test public void multipleAnnotationAddition() {
TypeSpec taco = TypeSpec.classBuilder("Taco")
.addAnnotations(Arrays.asList(
AnnotationSpec.builder(SuppressWarnings.class)
.addMember("value", "$S", "unchecked")
.build(),
AnnotationSpec.builder(Deprecated.class).build()))
.build();
assertThat(toString(taco)).isEqualTo(""
+ "package com.squareup.tacos;\n"
+ "\n"
+ "import java.lang.Deprecated;\n"
+ "import java.lang.SuppressWarnings;\n"
+ "\n"
+ "@SuppressWarnings(\"unchecked\")\n"
+ "@Deprecated\n"
+ "class Taco {\n"
+ "}\n");
}
public Hero(@NotNull String __typename, @NotNull String name,
@NotNull @Deprecated String deprecated, @Deprecated boolean deprecatedBool) {
this.__typename = Utils.checkNotNull(__typename, "__typename == null");
this.name = Utils.checkNotNull(name, "name == null");
this.deprecated = Utils.checkNotNull(deprecated, "deprecated == null");
this.deprecatedBool = deprecatedBool;
}
@Deprecated
public static deprecatedMethod() {
/* bad code to generate warnings and errors */
int unusedVar;
if (1 == 2) {
/* missed bracket: } */
}
@SuppressWarnings({"floopity", "floop"})
@Deprecated
@MyAnnotation(foo = "hi", fie = 15)
public static Annotation annotatedWithParams() {
return new AnnotatedWithParams();
}
/**
* Test deprecated field
* @deprecated For test purpose only
*/
public @NotNull @Deprecated String deprecated() {
return this.deprecated;
}
/**
* Test deprecated field
* @deprecated For test purpose only
*/
public @Deprecated boolean deprecatedBool() {
return this.deprecatedBool;
}
/**
* This method has been deprecated, please use method buildBounds instead.
* @return The buildable object.
*/
@Deprecated public List<TypeRef> getBounds(){
return build(bounds);
}
/**
* This method has been deprecated, please use method buildBounds instead.
* @return The buildable object.
*/
@Deprecated public List<TypeRef> getBounds();