@Override public void exitRule(Rule rule) { // create fields for each used function classFile.addFields(functionMembers); // create fields for hoisted constant expressions classFile.addFields(hoistedExpressionMembers); // TODO these can be shared and should potentially created by an AnnotationProcessor for each defined function instead of every rule classFile.addTypes(functionArgsHolderTypes); // resolve functions (but only do so once for each function) constructorBuilder.addStatement("// resolve used functions"); functionReferences.forEach(block -> constructorBuilder.addStatement("$L", block)); // add initializers for fields that depend on the functions being set constructorBuilder.addStatement("// function parameters"); constructorBuilder.addCode(lateConstructorBlock.build()); // all the expressions/statements that are constant at compile time constructorBuilder.addStatement("// constant expressions"); constructorBuilder.addCode(hoistedConstantExpressions.build()); classFile.addMethod(constructorBuilder.build()); generatedFile = JavaFile.builder("org.graylog.plugins.pipelineprocessor.$dynamic.rules", classFile.build()) .build(); }
TypeSpec type() { TypeVariableName e = TypeVariableName.get("E"); return TypeSpec.classBuilder(naming.elementClassName) .addAnnotation(ProtoTypeProcessor.ANNOTATION_SPEC) .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT) .addTypeVariable(TypeVariableName.get("E", ParameterizedTypeName.get(naming.elementClassName, e))) .superclass(ParameterizedTypeName.get(naming.protoClassName, e)) .addMethod(MethodSpec.constructorBuilder().build()) .addMethod(get()) .addMethods(childrenMethods()) .addTypes(IntStream.rangeClosed(0, protoTypeType.maxLevel()) .mapToObj(index -> ElementLevel.of(this, index, protoTypeType.maxLevel())) .map(ElementLevel::type) .collect(Collectors.toList())) .build(); }
private TypeSpec migrationType() { MigrationVersionMetadata migrationVersion = schemaName().migrationVersion(); ClassName className = migrationVersion.className(); return TypeSpec.classBuilder(className.simpleName()) .addAnnotation(migrationAnnotation()) .addTypes(tableInfoList().stream() .map(TableInfoMetadata::type) .collect(MoreCollectors.toImmutableList())) .build(); }
.addFields(shapeModelSpec.staticFields()) .addMethod(sdkFieldsMethod()) .addTypes(nestedModelClassTypes());
return DeriveResult.result(DerivedCodeSpec.codeSpec(wrapperClassSpec.addTypes(Stream.concat(
return DeriveResult.result(DerivedCodeSpec.codeSpec(wrapperClassSpec.addTypes(Stream.concat(
.build()); enumBuilder.addTypes(valueTypes); enumBuilder.addMethods(factoryMethods); enumBuilder.addMethods(isMethods);
public TypeSpec build() { TypeSpec.Builder builder; switch (kind) { case ANNOTATION: builder = TypeSpec.annotationBuilder(name); break; case CLASS: builder = TypeSpec.classBuilder(name); break; case ENUM: builder = TypeSpec.enumBuilder(name); break; case INTERFACE: builder = TypeSpec.interfaceBuilder(name); break; default: throw new UnsupportedOperationException("Unknown kind: " + kind); } builder.addAnnotations(annotations) .addModifiers(modifiers.toArray(new Modifier[0])) .addTypeVariables(typeVariables) .superclass(superclass) .addSuperinterfaces(superinterfaces); enumConstants.forEach(builder::addEnumConstant); builder.addFields(fieldSpecs).addMethods(methodSpecs).addTypes(typeSpecs); originatingElements.forEach(builder::addOriginatingElement); return builder.build(); }
public TypeSpec build() { TypeSpec.Builder builder; switch (kind) { case ANNOTATION: builder = TypeSpec.annotationBuilder(name); break; case CLASS: builder = TypeSpec.classBuilder(name); break; case ENUM: builder = TypeSpec.enumBuilder(name); break; case INTERFACE: builder = TypeSpec.interfaceBuilder(name); break; default: throw new UnsupportedOperationException("Unknown kind: " + kind); } builder.addAnnotations(annotations) .addModifiers(modifiers.toArray(new Modifier[0])) .addTypeVariables(typeVariables) .superclass(superclass) .addSuperinterfaces(superinterfaces); enumConstants.forEach(builder::addEnumConstant); builder.addFields(fieldSpecs).addMethods(methodSpecs).addTypes(typeSpecs); originatingElements.forEach(builder::addOriginatingElement); return builder.build(); }
public TypeSpec generate() { TypeSpec.Builder builder = TypeSpec.classBuilder(getClazzName()) .addJavadoc( "Generated by PreferenceRoom. (https://github.com/skydoves/PreferenceRoom).\n") .addModifiers(PUBLIC) .superclass(ClassName.get(annotatedClazz.annotatedElement)) .addFields(getFieldSpecs()); if (annotatedClazz.isDefaultPreference) { builder.addMethod(getDefaultPreferenceConstructorSpec()); } else { builder.addMethod(getConstructorSpec()); } builder .addMethod(getInstanceSpec()) .addTypes(getOnChangedTypeSpecs()) .addFields(getOnChangedFieldSpecs()) .addMethods(getFieldMethodSpecs()) .addMethod(getClearMethodSpec()) .addMethod(getKeyNameListMethodSpec()) .addMethod(getEntityNameMethodSpec()); return builder.build(); }
public TypeSpec type() { TypeSpec.Builder type = TypeSpec.interfaceBuilder(tableName.simpleName()) .addAnnotation(Table.class) .addMethods(columnInfoList().stream() .map(ColumnInfoResult::method) .collect(MoreCollectors.toImmutableList())); Optional<PrimaryKeyInfoMetadata> primaryKeyInfo = primaryKeyInfo(); if (primaryKeyInfo.isPresent()) { TypeSpec primaryKeyType = primaryKeyInfo.get().type(); type.addType(primaryKeyType); } return type .addTypes(foreignKeyInfoList().stream() .map(ForeignKeyInfoMetadata::type) .collect(MoreCollectors.toImmutableList())) .build(); }
public TypeSpec type() { TypeSpec.Builder type = TypeSpec.interfaceBuilder(tableName.simpleName()) .addAnnotation(Table.class) .addMethods(columnInfoList().stream() .map(ColumnInfoResult::method) .collect(MoreCollectors.toImmutableList())); Optional<PrimaryKeyInfoMetadata> primaryKeyInfo = primaryKeyInfo(); if (primaryKeyInfo.isPresent()) { TypeSpec primaryKeyType = primaryKeyInfo.get().type(); type.addType(primaryKeyType); } return type .addTypes(foreignKeyInfoList().stream() .map(ForeignKeyInfoMetadata::type) .collect(MoreCollectors.toImmutableList())) .build(); }
@Override public String write() throws IOException { String factoryName = processorElement.simpleName() + "Factory"; FieldSpec instanceField = FieldSpec.builder(ClassName.bestGuess(factoryName), "INSTANCE", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) .initializer("new " + factoryName + "()") .build(); List<TypeSpec> requests = processorElement.methodsStream().map(this::makeRequestClass).collect(toList()); List<MethodSpec> overrideMethods = processorElement.methodsStream().map(this::makeOverrideMethod).collect(toList()); TypeSpec factory = DominoTypeBuilder.build(factoryName, RequestFactoryProcessor.class) .addSuperinterface(ClassName.bestGuess(processorElement.simpleName())) .addField(instanceField) .addTypes(requests) .addMethods(overrideMethods).build(); StringBuilder asString = new StringBuilder(); JavaFile.builder(processorElement.elementPackage(), factory).skipJavaLangImports(true).build().writeTo(asString); return asString.toString(); }
private TypeSpec.Builder toTypeSpec(DeriveConfig deriveConfig, ClassName targetClassName, DerivedCodeSpec codeSpec) { TypeSpec.Builder builder = TypeSpec.classBuilder(targetClassName) .addModifiers(Modifier.FINAL, caseOf(deriveConfig.targetClass().visibility()).Package_(Modifier.FINAL).otherwise_(Modifier.PUBLIC)) .addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build()) .addTypes(getClasses(codeSpec)) .addFields(getFields(codeSpec)) .addMethods(getMethods(codeSpec)); deriveConfig.targetClass().extend().ifPresent(cn -> { if (deriveUtils.findTypeElement(cn).get().getKind().isInterface()) { builder.addSuperinterface(cn); } else { builder.superclass(cn); } }); return builder; }
/** * Create the definition of the generated class. * * @param generatedAnnotations annotations to add to the generated type, if any * @return type definition */ public TypeSpec typeSpec(List<AnnotationSpec> generatedAnnotations) { return classBuilder(generatedType) .addFields(fields) .addMethod(constructor()) .addMethods(transform(methods(), getMethod)) .addAnnotations(generatedAnnotations) .addModifiers(PUBLIC, FINAL) .addTypes(nestedTypes) .build(); }
private TypeSpec.Builder toTypeSpec(DeriveConfig deriveConfig, ClassName targetClassName, DerivedCodeSpec codeSpec) { TypeSpec.Builder builder = TypeSpec.classBuilder(targetClassName) .addModifiers(Modifier.FINAL, caseOf(deriveConfig.targetClass().visibility()).Package_(Modifier.FINAL).otherwise_(Modifier.PUBLIC)) .addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PRIVATE).build()) .addTypes(getClasses(codeSpec)) .addFields(getFields(codeSpec)) .addMethods(getMethods(codeSpec)); deriveConfig.targetClass().extend().ifPresent(cn -> { if (deriveUtils.findTypeElement(cn).get().getKind().isInterface()) { builder.addSuperinterface(cn); } else { builder.superclass(cn); } }); return builder; }
private Builder specBuilder() { Builder builder = TypeSpec.classBuilder(className).addModifiers(modifiers).addFields(specs) .addTypes(innerClasses.stream().map(s -> s.specBuilder().build()) .collect(Collectors.toList())); if (superClass != null) builder.superclass(ClassName.get(superClass.packageName, superClass.className)); for (BiFunction<Builder, TestSource, Builder> function : builderTransformers) { builder = function.apply(builder, this); } return builder; }
private TypeSpec migrationType() { MigrationVersionMetadata migrationVersion = schemaName().migrationVersion(); ClassName className = migrationVersion.className(); return TypeSpec.classBuilder(className.simpleName()) .addAnnotation(migrationAnnotation()) .addTypes(tableInfoList().stream() .map(TableInfoMetadata::type) .collect(MoreCollectors.toImmutableList())) .build(); }
private static TypeSpec createTypeSpec(RuntimePermissionsElement element, TypeResolver typeResolver) { return TypeSpec.classBuilder(element.getGeneratedClassName()) .addModifiers(Modifier.FINAL) .addFields(createFields(element.getNeedsPermissionsMethods())) .addMethod(createConstructor()) .addMethods(createWithCheckMethods(element)) .addMethods(createPermissionHandlingMethods(element)) .addTypes(createPermissionRequestClasses(element)) .build(); }
public void addToTypeSpec(TypeSpec.Builder typeSpec) { for (JavadocSpec javadocSpec : javadocSpecs) { typeSpec.addJavadoc(javadocSpec.format, javadocSpec.args); } typeSpec.addAnnotations(annotationSpecs); typeSpec.addFields(fieldSpecs); typeSpec.addMethods(methodSpecs); typeSpec.addTypes(typeSpecs); typeSpec.addSuperinterfaces(superInterfaces); }