public static Builder builder(Class<?> type) { return builder(ClassName.get(type)); }
public Builder addAnnotation(ClassName annotation) { this.annotations.add(AnnotationSpec.builder(annotation).build()); return this; }
public Builder addAnnotation(ClassName annotation) { this.annotations.add(AnnotationSpec.builder(annotation).build()); return this; }
public Builder addAnnotation(ClassName annotation) { this.annotations.add(AnnotationSpec.builder(annotation).build()); return this; }
private static AnnotationSpec annotation(ClassName className) { return AnnotationSpec.builder(className).build(); }
private static AnnotationSpec annotation(final ClassName className) { return AnnotationSpec.builder(className).build(); }
public static AnnotationSpec get(AnnotationMirror annotation) { TypeElement element = (TypeElement) annotation.getAnnotationType().asElement(); AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get(element)); Visitor visitor = new Visitor(builder); for (ExecutableElement executableElement : annotation.getElementValues().keySet()) { String name = executableElement.getSimpleName().toString(); AnnotationValue value = annotation.getElementValues().get(executableElement); value.accept(visitor, name); } return builder.build(); }
public Builder addAnnotation(ClassName annotation) { return addAnnotation(AnnotationSpec.builder(annotation).build()); }
private static List<AnnotationSpec> getExternalAnnotations(VariableElement param) { final List<? extends AnnotationMirror> annotationMirrors = param.getAnnotationMirrors(); final List<AnnotationSpec> annotations = new ArrayList<>(); for (AnnotationMirror annotationMirror : annotationMirrors) { if (annotationMirror.getAnnotationType().toString().startsWith(COMPONENTS_PACKAGE)) { continue; } final AnnotationSpec.Builder annotationSpec = AnnotationSpec.builder( ClassName.bestGuess(annotationMirror.getAnnotationType().toString())); Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues(); for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> elementValue : elementValues.entrySet()) { annotationSpec.addMember( elementValue.getKey().getSimpleName().toString(), elementValue.getValue().toString()); } annotations.add(annotationSpec.build()); } return annotations; }
private void createFactoryClass() throws IOException { final TypeName configurationBuilderFactory = Types.CONFIGURATION_BUILDER_FACTORY; Strings.writeClass(processingEnv.getFiler(), TypeSpec.classBuilder(factoryName) .addModifiers(Modifier.PUBLIC) .addSuperinterface(configurationBuilderFactory) .addAnnotation(AnnotationSpec.builder(AutoService.class).addMember("value", "$T.class", configurationBuilderFactory).build()) .addMethod(Types.overriding(Types.getOnlyMethod(processingEnv, Strings.CONFIGURATION_BUILDER_FACTORY)) .addAnnotation(Types.NON_NULL) .addStatement("return new $T($L)", ClassName.get(PACKAGE, builderName), PARAM_0) .build()) .build()); } }
@Override public void enterRule(Rule rule) { // generates a new ephemeral unique class name for each generated rule. Only valid for the runtime of the jvm classFile = TypeSpec.classBuilder("rule$" + rule.id()) .addSuperinterface(GeneratedRule.class) .addModifiers(Modifier.FINAL, Modifier.PUBLIC) .addAnnotation(AnnotationSpec.builder(SuppressWarnings.class) .addMember("value", "$S", "unchecked") .build() ) .addMethod(MethodSpec.methodBuilder("name") .returns(String.class) .addAnnotation(Override.class) .addModifiers(Modifier.PUBLIC) .addStatement("return $S", rule.name()) .build() ); constructorBuilder = MethodSpec.constructorBuilder() .addModifiers(Modifier.PUBLIC) .addParameter(FunctionRegistry.class, "functionRegistry"); lateConstructorBlock = CodeBlock.builder(); hoistedConstantExpressions = CodeBlock.builder(); }
private TypeSpec classProviders(ClassName className, List<MethodSpec> methodSpecs) { return TypeSpec.classBuilder(className.simpleName() + "Actionable") .addModifiers(Modifier.PUBLIC) .addAnnotation(AnnotationSpec.builder(Generated.class) .addMember("value", "$S", ActionsProcessor.class.getCanonicalName()) .addMember("comments", "$S", "Generated code from RxCache. Don't modify. Or modify. It doesn't matter.") .build()) .addMethods(methodSpecs) .build(); }
private static MethodSpec sipBuilder(SpecModel specModel, final PropModel prop) { final AnnotationSpec spAnnotation = AnnotationSpec.builder(ClassNames.DIMENSION) .addMember("unit", "$T.SP", ClassNames.DIMENSION) .build(); return builder( specModel, prop, prop.getName() + "Sp", Collections.singletonList(parameter(prop, TypeName.FLOAT, "sips", spAnnotation)), "$L.sipsToPixels(sips)", RESOURCE_RESOLVER); }
private static MethodSpec dipBuilder(SpecModel specModel, final PropModel prop) { final AnnotationSpec dipAnnotation = AnnotationSpec.builder(ClassNames.DIMENSION) .addMember("unit", "$T.DP", ClassNames.DIMENSION) .build(); return builder( specModel, prop, prop.getName() + "Dip", Collections.singletonList(parameter(prop, TypeName.FLOAT, "dips", dipAnnotation)), "$L.dipsToPixels(dips)", RESOURCE_RESOLVER); }
@Test public void testDiffModelHasDifferentParameterFromProp() { when(mDiffModel.getName()).thenReturn("propName"); when(mDiffModel.getTypeName()).thenReturn( ParameterizedTypeName.get(ClassNames.DIFF, TypeName.BOOLEAN.box()) .annotated(AnnotationSpec.builder(Prop.class).build())); when(mDiffModel.getAnnotations()).thenReturn(ImmutableList.of(annotation(Prop.class))); List<SpecModelValidationError> validationErrors = DiffValidation.validate(mSpecModel); assertSingleError(validationErrors, DiffValidation.PROP_MISMATCH_ERROR); }
@Test public void testDiffModelHasDifferentParameterFromState() { when(mDiffModel.getTypeName()).thenReturn( ParameterizedTypeName.get(ClassNames.DIFF, TypeName.BOOLEAN.box()) .annotated(AnnotationSpec.builder(State.class).build())); List<SpecModelValidationError> validationErrors = DiffValidation.validate(mSpecModel); assertSingleError(validationErrors, DiffValidation.STATE_MISMATCH_ERROR); }
@Test public void testNoErrorProp() { when(mDiffModel.getName()).thenReturn("propName"); when(mDiffModel.getTypeName()).thenReturn( ParameterizedTypeName.get(ClassNames.DIFF, TypeName.INT.box()) .annotated(AnnotationSpec.builder(Prop.class).build())); when(mDiffModel.getAnnotations()).thenReturn(ImmutableList.of(annotation(Prop.class))); List<SpecModelValidationError> validationErrors = DiffValidation.validate(mSpecModel); assertThat(validationErrors).hasSize(0); }
@Test public void testDiffModelHasNoTypeParameter() { when(mDiffModel.getTypeName()).thenReturn( ClassNames.DIFF.annotated(AnnotationSpec.builder(State.class).build())); List<SpecModelValidationError> validationErrors = DiffValidation.validate(mSpecModel); assertSingleError(validationErrors, DiffValidation.MISSING_TYPE_PARAMETER_ERROR); }
@Test public void testAnnotationExtraction() { final Elements elements = mCompilationRule.getElements(); final TypeElement element = elements.getTypeElement(TestClass.class.getCanonicalName()); final ImmutableList<AnnotationSpec> annotationSpecs = AnnotationExtractor.extractValidAnnotations(element); assertThat(annotationSpecs) .hasSize(1) .withFailMessage("Only the @Deprecated annotation should be extracted."); assertThat(annotationSpecs) .contains(AnnotationSpec.builder(Deprecated.class).build()) .doesNotContain(AnnotationSpec.builder(OnCreateInitialState.class).build()) .doesNotContain(AnnotationSpec.builder(SourceAnnotation.class).build()); } }
static TypeSpecDataHolder generateTreeProps(SpecModel specModel) { final TypeSpecDataHolder.Builder typeSpecDataHolder = TypeSpecDataHolder.newBuilder(); final ImmutableList<TreePropModel> treeProps = specModel.getTreeProps(); for (TreePropModel treeProp : treeProps) { typeSpecDataHolder.addField( FieldSpec.builder(treeProp.getTypeName(), treeProp.getName()) .addAnnotation(TreeProp.class) .addAnnotation( AnnotationSpec.builder(Comparable.class) .addMember("type", "$L", getComparableType(specModel, treeProp)) .build()) .build()); } return typeSpecDataHolder.build(); }