mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
Merge pull request #2037 from reegnz/master
make NOTE diagnostic printing in APT optional
This commit is contained in:
commit
4feaae79b7
@ -96,6 +96,11 @@ public final class APTOptions {
|
||||
*/
|
||||
public static final String QUERYDSL_VARIABLE_NAME_FUNCTION_CLASS = "querydsl.variableNameFunctionClass";
|
||||
|
||||
/**
|
||||
* set whether info level messages should be written to stdout (default: false)
|
||||
*/
|
||||
public static final String QUERYDSL_LOG_INFO = "querydsl.logInfo";
|
||||
|
||||
private APTOptions() { }
|
||||
|
||||
}
|
||||
|
||||
@ -13,6 +13,8 @@
|
||||
*/
|
||||
package com.querydsl.apt;
|
||||
|
||||
import static com.querydsl.apt.APTOptions.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.lang.annotation.Annotation;
|
||||
@ -26,7 +28,6 @@ import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.type.NoType;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.lang.model.util.ElementFilter;
|
||||
import javax.tools.Diagnostic;
|
||||
import javax.tools.Diagnostic.Kind;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
@ -63,16 +64,19 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
|
||||
|
||||
private Context context;
|
||||
|
||||
private boolean shouldLogInfo;
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Running " + getClass().getSimpleName());
|
||||
setLogInfo();
|
||||
logInfo("Running " + getClass().getSimpleName());
|
||||
|
||||
if (roundEnv.processingOver() || annotations.size() == 0) {
|
||||
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
|
||||
}
|
||||
|
||||
if (roundEnv.getRootElements() == null || roundEnv.getRootElements().isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "No sources to process");
|
||||
logInfo("No sources to process");
|
||||
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
|
||||
}
|
||||
|
||||
@ -504,27 +508,27 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
|
||||
}
|
||||
private void serializeMetaTypes() {
|
||||
if (!context.supertypes.isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Kind.NOTE, "Serializing Supertypes");
|
||||
logInfo("Serializing Supertypes");
|
||||
serialize(conf.getSupertypeSerializer(), context.supertypes.values());
|
||||
}
|
||||
|
||||
if (!context.entityTypes.isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Kind.NOTE, "Serializing Entity types");
|
||||
logInfo("Serializing Entity types");
|
||||
serialize(conf.getEntitySerializer(), context.entityTypes.values());
|
||||
}
|
||||
|
||||
if (!context.extensionTypes.isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Kind.NOTE, "Serializing Extension types");
|
||||
logInfo("Serializing Extension types");
|
||||
serialize(conf.getEmbeddableSerializer(), context.extensionTypes.values());
|
||||
}
|
||||
|
||||
if (!context.embeddableTypes.isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Kind.NOTE, "Serializing Embeddable types");
|
||||
logInfo("Serializing Embeddable types");
|
||||
serialize(conf.getEmbeddableSerializer(), context.embeddableTypes.values());
|
||||
}
|
||||
|
||||
if (!context.projectionTypes.isEmpty()) {
|
||||
processingEnv.getMessager().printMessage(Kind.NOTE, "Serializing Projection types");
|
||||
logInfo("Serializing Projection types");
|
||||
serialize(conf.getDTOSerializer(), context.projectionTypes.values());
|
||||
}
|
||||
|
||||
@ -539,6 +543,27 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
|
||||
return SourceVersion.latestSupported();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getSupportedOptions() {
|
||||
Set<String> optionKeys = new HashSet<String>();
|
||||
optionKeys.add(QUERYDSL_LOG_INFO);
|
||||
return optionKeys;
|
||||
}
|
||||
|
||||
private void setLogInfo() {
|
||||
boolean hasProperty = processingEnv.getOptions().containsKey(QUERYDSL_LOG_INFO);
|
||||
if (hasProperty) {
|
||||
String val = processingEnv.getOptions().get(QUERYDSL_LOG_INFO);
|
||||
shouldLogInfo = Boolean.parseBoolean(val);
|
||||
}
|
||||
}
|
||||
|
||||
private void logInfo(String message) {
|
||||
if (shouldLogInfo) {
|
||||
processingEnv.getMessager().printMessage(Kind.NOTE, message);
|
||||
}
|
||||
}
|
||||
|
||||
private void serialize(Serializer serializer, Collection<EntityType> models) {
|
||||
for (EntityType model : models) {
|
||||
try {
|
||||
@ -565,7 +590,7 @@ public abstract class AbstractQuerydslProcessor extends AbstractProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
processingEnv.getMessager().printMessage(Kind.NOTE, "Generating " + className + " for " + elements);
|
||||
logInfo("Generating " + className + " for " + elements);
|
||||
JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(className,
|
||||
elements.toArray(new Element[elements.size()]));
|
||||
Writer writer = fileObject.openWriter();
|
||||
|
||||
@ -66,8 +66,8 @@ public abstract class AbstractProcessorTest {
|
||||
options.addAll(getAPTOptions());
|
||||
options.addAll(classes);
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream err = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream out = getStdOut();
|
||||
ByteArrayOutputStream err = getStdErr();
|
||||
int compilationResult = compiler.run(null, out, err, options.toArray(new String[options.size()]));
|
||||
|
||||
// Processor.elementCache.clear();
|
||||
@ -77,6 +77,14 @@ public abstract class AbstractProcessorTest {
|
||||
}
|
||||
}
|
||||
|
||||
protected ByteArrayOutputStream getStdOut() {
|
||||
return new ByteArrayOutputStream();
|
||||
}
|
||||
|
||||
protected ByteArrayOutputStream getStdErr() {
|
||||
return new ByteArrayOutputStream();
|
||||
}
|
||||
|
||||
protected Collection<String> getAPTOptions() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
63
querydsl-apt/src/test/java/com/querydsl/apt/NoteTest.java
Normal file
63
querydsl-apt/src/test/java/com/querydsl/apt/NoteTest.java
Normal file
@ -0,0 +1,63 @@
|
||||
package com.querydsl.apt;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NoteTest extends AbstractProcessorTest {
|
||||
|
||||
private Collection<String> aptOptions;
|
||||
|
||||
private ByteArrayOutputStream err = new ByteArrayOutputStream();
|
||||
|
||||
private static final String packagePath = "src/test/java/com/querydsl/apt/";
|
||||
|
||||
public void process() throws IOException {
|
||||
List<String> classes = getFiles(packagePath);
|
||||
process(QuerydslAnnotationProcessor.class, classes, "includedClasses");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<String> getAPTOptions() {
|
||||
return aptOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ByteArrayOutputStream getStdErr() {
|
||||
return err;
|
||||
}
|
||||
|
||||
protected boolean isStdErrEmpty() {
|
||||
return getStdErr().toByteArray().length == 0;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void processDefault() throws IOException {
|
||||
aptOptions = Collections.emptyList();
|
||||
process();
|
||||
assertTrue(isStdErrEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void processEnabled() throws IOException {
|
||||
aptOptions = Arrays.asList("-Aquerydsl.logInfo=true");
|
||||
process();
|
||||
assertFalse(isStdErrEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void processDisabled() throws IOException {
|
||||
aptOptions = Arrays.asList("-Aquerydsl.logInfo=false");
|
||||
process();
|
||||
assertTrue(isStdErrEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user