Skip to content

Fix incremental compilation issue #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package io.javaoperatorsdk.operator.processing;
package io.javaoperatorsdk.operator.processing.annotation;

import com.google.auto.service.AutoService;
import com.squareup.javapoet.*;
import io.fabric8.kubernetes.api.builder.Function;
import io.fabric8.kubernetes.client.CustomResourceDoneable;
import io.javaoperatorsdk.operator.api.ResourceController;

import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -30,23 +28,14 @@
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@AutoService(Processor.class)
public class ControllerAnnotationProcessor extends AbstractProcessor {
private FileObject resource;
PrintWriter printWriter = null;
private ControllersResourceWriter controllersResourceWriter;
private Set<String> generatedDoneableClassFiles = new HashSet<>();

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
try {
resource = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", CONTROLLERS_RESOURCE_PATH);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
printWriter = new PrintWriter(resource.openOutputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
controllersResourceWriter = new ControllersResourceWriter(processingEnv);
controllersResourceWriter.loadExistingMappings();
}

@Override
Expand All @@ -57,15 +46,17 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
= roundEnv.getElementsAnnotatedWith(annotation);
annotatedElements.stream().filter(element -> element.getKind().equals(ElementKind.CLASS))
.map(e -> (TypeElement) e)
.forEach(e -> this.generateDoneableClass(e, printWriter));
.forEach(e -> this.generateDoneableClass(e));
}
} finally {
printWriter.close();
if (roundEnv.processingOver()) {
controllersResourceWriter.flush();
}
}
return true;
}

private void generateDoneableClass(TypeElement controllerClassSymbol, PrintWriter printWriter) {
private void generateDoneableClass(TypeElement controllerClassSymbol) {
try {
final TypeMirror resourceType = findResourceType(controllerClassSymbol);

Expand All @@ -84,14 +75,14 @@ private void generateDoneableClass(TypeElement controllerClassSymbol, PrintWrite
destinationClassFileName,
CONTROLLERS_RESOURCE_PATH)
);
printWriter.println(controllerClassSymbol.getQualifiedName() + "," + customResourceType.toString());
controllersResourceWriter.add(controllerClassSymbol.getQualifiedName().toString(), customResourceType.toString());
return;
}
JavaFileObject builderFile = processingEnv.getFiler()
.createSourceFile(destinationClassFileName);

try (PrintWriter out = new PrintWriter(builderFile.openWriter())) {
printWriter.println(controllerClassSymbol.getQualifiedName() + "," + customResourceType.toString());
controllersResourceWriter.add(controllerClassSymbol.getQualifiedName().toString(), customResourceType.toString());
final MethodSpec constructor = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PUBLIC)
.addParameter(customResourceType, "resource")
Expand Down Expand Up @@ -125,7 +116,6 @@ private TypeMirror findResourceType(TypeElement controllerClassSymbol) throws Ex
)
.findFirst()
.orElseThrow(() -> new Exception("ResourceController is not implemented by " + controllerClassSymbol.toString()));

return controllerType.getTypeArguments().get(0);
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.javaoperatorsdk.operator.processing.annotation;

import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.StandardLocation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static io.javaoperatorsdk.operator.ControllerUtils.CONTROLLERS_RESOURCE_PATH;

class ControllersResourceWriter {
private Map<String, String> mappings = new ConcurrentHashMap<>();
private final ProcessingEnvironment processingEnvironment;

public ControllersResourceWriter(ProcessingEnvironment processingEnvironment) {
this.processingEnvironment = processingEnvironment;
}

public ControllersResourceWriter loadExistingMappings() {
try {
final var readonlyResource = processingEnvironment
.getFiler()
.getResource(StandardLocation.CLASS_OUTPUT, "", CONTROLLERS_RESOURCE_PATH);

final var bufferedReader = new BufferedReader(new InputStreamReader(readonlyResource.openInputStream()));
final var existingLines = bufferedReader
.lines()
.map(l -> l.split(","))
.collect(Collectors.toMap(parts -> parts[0], parts -> parts[1]));
mappings.putAll(existingLines);
} catch (IOException e) {
}
return this;
}

public ControllersResourceWriter add(String controllerClassName, String customResourceTypeName) {
this.mappings.put(controllerClassName, customResourceTypeName);
return this;
}

public void flush() {
PrintWriter printWriter = null;
try {
final var resource = processingEnvironment
.getFiler()
.createResource(StandardLocation.CLASS_OUTPUT, "", CONTROLLERS_RESOURCE_PATH);
printWriter = new PrintWriter(resource.openOutputStream());


for (Map.Entry<String, String> entry : mappings.entrySet()) {
printWriter.println(entry.getKey() + "," + entry.getValue());
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
if (printWriter != null) {
printWriter.close();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.javaoperatorsdk.operator.processing;
package io.javaoperatorsdk.operator.processing.annotation;

import com.google.testing.compile.*;
import com.google.testing.compile.Compiler;
Expand Down