Skip to content

Commit

Permalink
Fix annotation processor file writing
Browse files Browse the repository at this point in the history
  • Loading branch information
kb-1000 committed Nov 4, 2024
1 parent 9a849f9 commit d62d9d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
import com.google.auto.service.AutoService;
import com.palantir.javapoet.*;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

/**
Expand All @@ -23,6 +18,8 @@
* static fields.
*/
@AutoService(Processor.class)
@SupportedAnnotationTypes("dev.pswg.codecgenerator.GenerateCodec")
@SupportedSourceVersion(SourceVersion.RELEASE_21)
public class CodecGenerationProcessor extends AbstractProcessor
{
/**
Expand Down Expand Up @@ -435,7 +432,14 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
continue;
}

writeInterfaceToFile(generatedInterface);
try
{
generatedInterface.writeTo(processingEnv.getFiler());
}
catch (IOException e)
{
processingEnv.getMessager().printError(e.toString(), element);
}
}
}
}
Expand All @@ -460,6 +464,7 @@ private JavaFile generateInterface(String packageName, TypeElement classElement,

var iface = TypeSpec.interfaceBuilder(interfaceName)
.addModifiers(Modifier.PUBLIC)
.addOriginatingElement(classElement)
.addField(codec)
.addField(packetCodec)
.build();
Expand Down Expand Up @@ -687,31 +692,4 @@ private TypeMirror getCodecSourceType(CodecSource codecSource)

throw new RuntimeException("Source type did not result in a mirror");
}

private void writeInterfaceToFile(JavaFile file)
{
try
{
Path path = Paths.get(processingEnv.getFiler().getResource(StandardLocation.SOURCE_OUTPUT, "", "dummy").toUri());
var dir = path.getParent();
file.writeToFile(dir.toFile());
log("Generated in " + dir);
}
catch (IOException e)
{
log("Failed: %s".formatted(e.getMessage()));
}
}

@Override
public Set<String> getSupportedAnnotationTypes()
{
return Set.of(GenerateCodec.class.getCanonicalName());
}

@Override
public SourceVersion getSupportedSourceVersion()
{
return SourceVersion.latestSupported();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
import com.google.auto.service.AutoService;
import com.palantir.javapoet.*;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.tools.Diagnostic;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

/**
* An annotation processor that generates record builders
* and mutators
*/
@AutoService(Processor.class)
@SupportedAnnotationTypes("dev.pswg.mutablerecord.MutableRecord")
@SupportedSourceVersion(SourceVersion.RELEASE_21)
public class MutableRecordProcessor extends AbstractProcessor
{
private void log(String message)
Expand Down Expand Up @@ -50,7 +47,14 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
continue;
}

writeInterfaceToFile(generatedInterface);
try
{
generatedInterface.writeTo(processingEnv.getFiler());
}
catch (IOException e)
{
processingEnv.getMessager().printError(e.toString(), element);
}
}
}
}
Expand All @@ -66,7 +70,8 @@ private String getPackageName(TypeElement classElement)
private JavaFile generateInterface(String packageName, TypeElement classElement, String interfaceName)
{
var iface = TypeSpec.interfaceBuilder(interfaceName)
.addModifiers(Modifier.PUBLIC);
.addModifiers(Modifier.PUBLIC)
.addOriginatingElement(classElement);

for (var component : classElement.getRecordComponents())
{
Expand Down Expand Up @@ -157,31 +162,4 @@ private String capitalize(String str)

return str.substring(0, 1).toUpperCase() + str.substring(1);
}

private void writeInterfaceToFile(JavaFile file)
{
try
{
Path path = Paths.get(processingEnv.getFiler().getResource(StandardLocation.SOURCE_OUTPUT, "", "dummy").toUri());
var dir = path.getParent();
file.writeToFile(dir.toFile());
log("Generated in " + dir);
}
catch (IOException e)
{
log("Failed: %s".formatted(e.getMessage()));
}
}

@Override
public Set<String> getSupportedAnnotationTypes()
{
return Set.of(MutableRecord.class.getCanonicalName());
}

@Override
public SourceVersion getSupportedSourceVersion()
{
return SourceVersion.latestSupported();
}
}

0 comments on commit d62d9d5

Please sign in to comment.