Skip to content

Commit

Permalink
[Java] allow processors to be disabled via system property
Browse files Browse the repository at this point in the history
  • Loading branch information
nbradac committed Jun 3, 2024
1 parent af23a71 commit dfb3032
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public class ConfigProcessor extends Processor

private final Map<String, Config> typeConfigMap = new HashMap<>();

@Override
protected String getEnabledPropertyName()
{
return "aeron.build.configProcessor.enabled";
}

@Override
protected String getPrintNotesPropertyName()
{
Expand All @@ -55,7 +61,7 @@ protected String getFailOnErrorPropertyName()
* {@inheritDoc}
*/
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv)
public void doProcess(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv)
{
final Map<String, ConfigInfo> configInfoMap = new HashMap<>();

Expand Down Expand Up @@ -112,6 +118,7 @@ else if (element instanceof TypeElement)
catch (final Exception e)
{
e.printStackTrace(System.err);
return;
}

try
Expand All @@ -128,8 +135,6 @@ else if (element instanceof TypeElement)
"an error occurred while writing output: " + e.getMessage());
}
}

return false;
}

private ConfigInfo processElement(final Map<String, ConfigInfo> configInfoMap, final VariableElement element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class Validation

private String message;

private ByteArrayOutputStream ba_out;
private ByteArrayOutputStream baOut;

private PrintStream ps_out;
private PrintStream psOut;

boolean isValid()
{
Expand All @@ -35,9 +35,9 @@ boolean isValid()

void close()
{
if (this.ps_out != null)
if (this.psOut != null)
{
this.ps_out.close();
this.psOut.close();
}
}

Expand All @@ -55,21 +55,21 @@ void invalid(final String message)

PrintStream out()
{
if (this.ps_out == null)
if (this.psOut == null)
{
this.ba_out = new ByteArrayOutputStream();
this.ps_out = new PrintStream(ba_out);
this.baOut = new ByteArrayOutputStream();
this.psOut = new PrintStream(baOut);
}

return ps_out;
return psOut;
}

void printOn(final PrintStream out)
{
out.println(" " + (this.valid ? "+" : "-") + " " + this.message);
if (this.ps_out != null)
if (this.psOut != null)
{
out.println(this.ba_out);
out.println(this.baOut);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
@SupportedAnnotationTypes("io.aeron.counter.AeronCounter")
public class CounterProcessor extends Processor
{
@Override
protected String getEnabledPropertyName()
{
return "aeron.build.counterProcessor.enabled";
}

@Override
protected String getPrintNotesPropertyName()
{
Expand All @@ -50,7 +56,7 @@ protected String getFailOnErrorPropertyName()
* {@inheritDoc}
*/
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv)
public void doProcess(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv)
{
final Map<String, CounterInfo> counterInfoMap = new HashMap<>();

Expand Down Expand Up @@ -93,8 +99,6 @@ public boolean process(final Set<? extends TypeElement> annotations, final Round
"an error occurred while writing output: " + e.getMessage());
}
}

return false;
}

private void processElement(final Map<String, CounterInfo> counterInfoMap, final VariableElement element)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class Validation

private String message;

private ByteArrayOutputStream ba_out;
private ByteArrayOutputStream baOut;

private PrintStream ps_out;
private PrintStream psOut;

Validation(final String name)
{
Expand All @@ -41,9 +41,9 @@ boolean isValid()

void close()
{
if (this.ps_out != null)
if (this.psOut != null)
{
this.ps_out.close();
this.psOut.close();
}
}

Expand All @@ -61,22 +61,22 @@ void invalid(final String message)

PrintStream out()
{
if (this.ps_out == null)
if (this.psOut == null)
{
this.ba_out = new ByteArrayOutputStream();
this.ps_out = new PrintStream(ba_out);
this.baOut = new ByteArrayOutputStream();
this.psOut = new PrintStream(baOut);
}

return ps_out;
return psOut;
}

void printOn(final PrintStream out)
{
out.println(name);
out.println(" " + (this.valid ? "+" : "-") + " " + this.message);
if (this.ps_out != null)
if (this.psOut != null)
{
out.println(this.ba_out);
out.println(this.baOut);
}
}
}
20 changes: 20 additions & 0 deletions aeron-annotations/src/main/java/io/aeron/utility/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.tools.Diagnostic;
import java.util.Set;

/**
* abstract processor
*/
public abstract class Processor extends AbstractProcessor
{
private boolean enabled = false;

private boolean printNotes = false;

private Diagnostic.Kind errorKind;
Expand All @@ -45,6 +49,7 @@ public SourceVersion getSupportedSourceVersion()
@Override
public synchronized void init(final ProcessingEnvironment processingEnv)
{
enabled = System.getProperty(getEnabledPropertyName(), "true").equalsIgnoreCase("true");
printNotes = System.getProperty(getPrintNotesPropertyName(), "false").equalsIgnoreCase("true");
errorKind =
System.getProperty(getFailOnErrorPropertyName(), "false").equalsIgnoreCase("true") ?
Expand All @@ -53,10 +58,25 @@ public synchronized void init(final ProcessingEnvironment processingEnv)
super.init(processingEnv);
}

protected abstract String getEnabledPropertyName();

protected abstract String getPrintNotesPropertyName();

protected abstract String getFailOnErrorPropertyName();

protected abstract void doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv);

@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv)
{
if (enabled)
{
doProcess(annotations, roundEnv);
}

return false;
}

protected String getDocComment(final Element element)
{
final String description = processingEnv.getElementUtils().getDocComment(element);
Expand Down

0 comments on commit dfb3032

Please sign in to comment.