Skip to content

Commit

Permalink
Merge pull request jboss-logging#43 from jamezp/proc-env
Browse files Browse the repository at this point in the history
Prefer using the ProcessingEnvironment and don't reprocess if the rou…
  • Loading branch information
jamezp committed Mar 31, 2016
2 parents cd29d30 + f3cbf9a commit d038427
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 77 deletions.
@@ -1,5 +1,6 @@
package org.jboss.logging.processor.apt;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
Expand All @@ -11,19 +12,22 @@
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
abstract class AbstractClassType implements ClassType {
protected final ProcessingEnvironment processingEnv;
protected final Elements elements;
protected final Types types;
protected final TypeMirror typeMirror;

protected AbstractClassType(final Elements elements, final Types types, final TypeMirror typeMirror) {
this.elements = elements;
this.types = types;
AbstractClassType(final ProcessingEnvironment processingEnv, final TypeMirror typeMirror) {
this.processingEnv = processingEnv;
this.elements = processingEnv.getElementUtils();
this.types = processingEnv.getTypeUtils();
this.typeMirror = typeMirror;
}

protected AbstractClassType(final Elements elements, final Types types, final Element element) {
this.elements = elements;
this.types = types;
AbstractClassType(final ProcessingEnvironment processingEnv, final Element element) {
this.processingEnv = processingEnv;
this.elements = processingEnv.getElementUtils();
this.types = processingEnv.getTypeUtils();
this.typeMirror = element.asType();
}

Expand Down
Expand Up @@ -147,6 +147,13 @@ public synchronized void init(final ProcessingEnvironment processingEnv) {

@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
doProcess(annotations, roundEnv);
}
return true;
}

private void doProcess(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
boolean generate = true;
final Validator validator = new Validator(processingEnv.getElementUtils(), processingEnv.getTypeUtils());

Expand Down Expand Up @@ -195,7 +202,6 @@ public boolean process(final Set<? extends TypeElement> annotations, final Round
}
}
}
return true;
}

@SafeVarargs
Expand Down
Expand Up @@ -72,31 +72,30 @@ private MessageInterfaceFactory() {
* Creates a message interface from the {@link javax.lang.model.element.TypeElement} specified by the {@code
* interfaceElement} parameter.
*
* @param processingEnvironment the annotation processing environment.
* @param processingEnv the annotation processing environment.
* @param interfaceElement the interface element to parse.
*
* @return a message interface for the interface element.
*/
public static MessageInterface of(final ProcessingEnvironment processingEnvironment, final TypeElement interfaceElement) {
final Types types = processingEnvironment.getTypeUtils();
final Elements elements = processingEnvironment.getElementUtils();
if (types.isSameType(interfaceElement.asType(), ElementHelper.toType(elements, BasicLogger.class))) {
public static MessageInterface of(final ProcessingEnvironment processingEnv, final TypeElement interfaceElement) {
final Types types = processingEnv.getTypeUtils();
if (types.isSameType(interfaceElement.asType(), ElementHelper.toType(processingEnv.getElementUtils(), BasicLogger.class))) {
MessageInterface result = LOGGER_INTERFACE;
if (result == null) {
synchronized (LOCK) {
result = LOGGER_INTERFACE;
if (result == null) {
LOGGER_INTERFACE = LoggerInterface.of(elements, types);
LOGGER_INTERFACE = LoggerInterface.of(processingEnv);
result = LOGGER_INTERFACE;
}
}
}
return result;
}
final AptMessageInterface result = new AptMessageInterface(interfaceElement, types, elements);
final AptMessageInterface result = new AptMessageInterface(interfaceElement, processingEnv);
result.init();
for (TypeMirror typeMirror : interfaceElement.getInterfaces()) {
final MessageInterface extended = MessageInterfaceFactory.of(processingEnvironment, (TypeElement) types.asElement(typeMirror));
final MessageInterface extended = MessageInterfaceFactory.of(processingEnv, (TypeElement) types.asElement(typeMirror));
result.extendedInterfaces.add(extended);
result.extendedInterfaces.addAll(extended.extendedInterfaces());
}
Expand All @@ -118,8 +117,8 @@ private static class AptMessageInterface extends AbstractClassType implements Me
private String fqcn;
private int idLen;

private AptMessageInterface(final TypeElement interfaceElement, final Types types, final Elements elements) {
super(elements, types, interfaceElement);
private AptMessageInterface(final TypeElement interfaceElement, final ProcessingEnvironment processingEnv) {
super(processingEnv, interfaceElement);
this.interfaceElement = interfaceElement;
this.messageMethods = new LinkedList<>();
this.extendedInterfaces = new LinkedHashSet<>();
Expand Down Expand Up @@ -168,7 +167,7 @@ public String projectCode() {
}

private void init() {
final MessageMethodBuilder builder = MessageMethodBuilder.create(elements, types)
final MessageMethodBuilder builder = MessageMethodBuilder.create(processingEnv)
.add(getMessageMethods(interfaceElement));
final Collection<MessageMethod> m = builder.build();
this.messageMethods.addAll(m);
Expand Down Expand Up @@ -263,21 +262,21 @@ private static class LoggerInterface extends AbstractClassType implements Messag
private final TypeElement loggerInterface;
private final Set<MessageMethod> messageMethods;

private LoggerInterface(final Elements elements, final Types types, final TypeElement loggerInterface) {
super(elements, types, loggerInterface.asType());
private LoggerInterface(final ProcessingEnvironment processingEnv, final TypeElement loggerInterface) {
super(processingEnv, loggerInterface.asType());
messageMethods = new LinkedHashSet<>();
this.loggerInterface = loggerInterface;
}

private void init() {
final MessageMethodBuilder builder = MessageMethodBuilder.create(elements, types)
final MessageMethodBuilder builder = MessageMethodBuilder.create(processingEnv)
.add(getMessageMethods(loggerInterface));
final Collection<MessageMethod> m = builder.build();
this.messageMethods.addAll(m);
}

static LoggerInterface of(final Elements elements, final Types types) {
final LoggerInterface result = new LoggerInterface(elements, types, elements.getTypeElement(BasicLogger.class.getCanonicalName()));
static LoggerInterface of(final ProcessingEnvironment processingEnv) {
final LoggerInterface result = new LoggerInterface(processingEnv, processingEnv.getElementUtils().getTypeElement(BasicLogger.class.getCanonicalName()));
result.init();
return result;
}
Expand Down
Expand Up @@ -40,12 +40,12 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

import org.jboss.logging.Logger;
import org.jboss.logging.annotations.Cause;
Expand All @@ -69,12 +69,10 @@ final class MessageMethodBuilder {

private static final String MESSAGE_METHOD_SUFFIX = "$str";
private final List<ExecutableElement> methods;
private final Elements elements;
private final Types types;
private final ProcessingEnvironment processingEnv;

private MessageMethodBuilder(final Elements elements, final Types types) {
this.elements = elements;
this.types = types;
private MessageMethodBuilder(final ProcessingEnvironment processingEnv) {
this.processingEnv = processingEnv;
methods = new LinkedList<>();
}

Expand All @@ -84,18 +82,19 @@ MessageMethodBuilder add(final Collection<ExecutableElement> methods) {
}

Set<MessageMethod> build() {
final Elements elements = processingEnv.getElementUtils();
final Set<MessageMethod> result = new LinkedHashSet<>();
for (ExecutableElement elementMethod : methods) {
final AptMessageMethod resultMethod = new AptMessageMethod(elements, elementMethod);
resultMethod.inheritsMessage = inheritsMessage(methods, elementMethod);
resultMethod.message = findMessage(methods, elementMethod);
resultMethod.isOverloaded = isOverloaded(methods, elementMethod);
for (TypeMirror thrownType : elementMethod.getThrownTypes()) {
resultMethod.thrownTypes.add(ThrowableTypeFactory.of(elements, types, thrownType));
resultMethod.thrownTypes.add(ThrowableTypeFactory.of(processingEnv, thrownType));
}

// Create a list of parameters
for (Parameter parameter : ParameterFactory.of(elements, types, resultMethod.method)) {
for (Parameter parameter : ParameterFactory.of(processingEnv, resultMethod.method)) {
resultMethod.add(parameter);
}
// Check to see if the method is overloaded
Expand All @@ -107,7 +106,7 @@ Set<MessageMethod> build() {
resultMethod.translationKey = resultMethod.name();
}
// Set the return type
resultMethod.returnType = ReturnTypeFactory.of(elements, types, elementMethod.getReturnType(), resultMethod);
resultMethod.returnType = ReturnTypeFactory.of(processingEnv, elementMethod.getReturnType(), resultMethod);
result.add(resultMethod);
}
return Collections.unmodifiableSet(result);
Expand Down Expand Up @@ -169,8 +168,8 @@ private boolean hasMessageId(final Message message) {
return message != null && (message.id() != Message.NONE && message.id() != Message.INHERIT);
}

static MessageMethodBuilder create(final Elements elements, final Types types) {
return new MessageMethodBuilder(elements, types);
static MessageMethodBuilder create(final ProcessingEnvironment processingEnv) {
return new MessageMethodBuilder(processingEnv);
}

/**
Expand Down
Expand Up @@ -30,12 +30,12 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

import org.jboss.logging.annotations.Field;
Expand All @@ -59,7 +59,8 @@ final class ParameterFactory {
private ParameterFactory() {
}

public static Set<Parameter> of(final Elements elements, final Types types, final ExecutableElement method) {
public static Set<Parameter> of(final ProcessingEnvironment processingEnv, final ExecutableElement method) {
final Types types = processingEnv.getTypeUtils();
final Set<Parameter> result = new LinkedHashSet<>();
final List<? extends VariableElement> params = method.getParameters();
int index = 0;
Expand All @@ -80,9 +81,9 @@ public static Set<Parameter> of(final Elements elements, final Types types, fina
}
}
if (method.isVarArgs()) {
result.add(new AptParameter(elements, types, qualifiedType, param, formatClass, (++index == params.size())));
result.add(new AptParameter(processingEnv, qualifiedType, param, formatClass, (++index == params.size())));
} else {
result.add(new AptParameter(elements, types, qualifiedType, param, formatClass, false));
result.add(new AptParameter(processingEnv, qualifiedType, param, formatClass, false));
}
}
return result;
Expand All @@ -103,15 +104,14 @@ private static class AptParameter extends AbstractClassType implements Parameter
/**
* Only allow construction from within the parent class.
*
* @param elements the element utilities from the annotation processor.
* @param types the type utilities from the annotation processor.
* @param qualifiedType the qualified type name of the parameter.
* @param param the parameter.
* @param formatterClass the formatter class, or {@code null} if none
* @param isVarArgs {@code true} if this is a vararg parameter, otherwise {@code false}.
* @param processingEnv the annotation processing environment.
* @param qualifiedType the qualified type name of the parameter.
* @param param the parameter.
* @param formatterClass the formatter class, or {@code null} if none
* @param isVarArgs {@code true} if this is a vararg parameter, otherwise {@code false}.
*/
AptParameter(final Elements elements, final Types types, final String qualifiedType, final VariableElement param, final String formatterClass, final boolean isVarArgs) {
super(elements, types, param);
AptParameter(final ProcessingEnvironment processingEnv, final String qualifiedType, final VariableElement param, final String formatterClass, final boolean isVarArgs) {
super(processingEnv, param);
this.qualifiedType = qualifiedType;
this.param = param;
this.formatterClass = formatterClass;
Expand Down
Expand Up @@ -28,6 +28,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
Expand All @@ -37,7 +38,6 @@
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

import org.jboss.logging.annotations.ConstructType;
Expand All @@ -61,11 +61,11 @@ private ReturnTypeFactory() {
}


public static ReturnType of(final Elements elements, final Types types, final TypeMirror returnType, final MessageMethod method) {
public static ReturnType of(final ProcessingEnvironment processingEnv, final TypeMirror returnType, final MessageMethod method) {
if (returnType.getKind() == TypeKind.VOID) {
return VoidReturnType.getInstance(types);
return VoidReturnType.getInstance(processingEnv.getTypeUtils());
}
final AptReturnType result = new AptReturnType(elements, types, returnType, method);
final AptReturnType result = new AptReturnType(processingEnv, returnType, method);
result.init();
return result;
}
Expand All @@ -80,8 +80,8 @@ private static class AptReturnType extends AbstractClassType implements ReturnTy
private final MessageMethod method;
private ThrowableType throwableType;

AptReturnType(final Elements elements, final Types types, final TypeMirror returnType, final MessageMethod method) {
super(elements, types, returnType);
AptReturnType(final ProcessingEnvironment processingEnv, final TypeMirror returnType, final MessageMethod method) {
super(processingEnv, returnType);
this.returnType = returnType;
this.method = method;
throwableType = null;
Expand Down Expand Up @@ -138,7 +138,7 @@ private void init() {
throw new ProcessingException(method, "The requested type %s can not be assigned to %s.", returnType, this.returnType);
}
}
throwableType = ThrowableTypeFactory.forReturnType(elements, types, returnType, method);
throwableType = ThrowableTypeFactory.forReturnType(processingEnv, returnType, method);
}
final Element e = types.asElement(returnType);
if (e instanceof TypeElement) {
Expand Down

0 comments on commit d038427

Please sign in to comment.