Example usage for javax.annotation.processing Messager printMessage

List of usage examples for javax.annotation.processing Messager printMessage

Introduction

In this page you can find the example usage for javax.annotation.processing Messager printMessage.

Prototype

void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e);

Source Link

Document

Prints a message of the specified kind at the location of the element.

Usage

From source file:Main.java

private static void _error(Messager messager, Element element, String message, Object... args) {
    if (args.length > 0) {
        message = String.format(message, args);
    }/*from ww w . j  a  va2s .co  m*/
    messager.printMessage(Diagnostic.Kind.ERROR, message, element);
}

From source file:de.hasait.genesis.base.util.GenesisUtils.java

public static void printError(final Messager pMessager, final Element pElement, final String pFormat,
        final Object... pArgs) {
    final String message = String.format(pFormat, pArgs);
    pMessager.printMessage(Diagnostic.Kind.ERROR, message, pElement);
}

From source file:de.hasait.genesis.base.util.GenesisUtils.java

public static void printNote(final Messager pMessager, final Element pElement, final String pFormat,
        final Object... pArgs) {
    final String message = String.format(pFormat, pArgs);
    pMessager.printMessage(Diagnostic.Kind.NOTE, message, pElement);
}

From source file:de.hasait.genesis.base.util.GenesisUtils.java

public static void printStackTrace(final Messager pMessager, final Element pElement, final Throwable pThrowable,
        final String pFormat, final Object... pArgs) {
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);
    final String message = String.format(pFormat, pArgs);
    pw.println(message);//w  w w . j  av a 2  s. co  m
    pThrowable.printStackTrace(pw);
    pw.flush();
    pMessager.printMessage(Diagnostic.Kind.ERROR, sw.toString(), pElement);
}

From source file:org.jdto.tools.AnnotationConfigVerifier.java

private void validateElementConfiguration(SourceConfiguration sourceConfig, Element getter,
        TypeElement targetObject, Messager messager) {

    String sourceProperty = sourceConfig.getPropertyName();

    //means unknown configuration.
    if (sourceProperty == null) {
        return;//from w w w  .j  av a 2s.c  om
    }

    //source does not need to be validated.
    if (Source.ROOT_OBJECT.equals(sourceProperty)) {
        return;
    }

    //split the properties.
    String[] propertyPath = StringUtils.split(sourceProperty, ".");

    TypeElement currentObject = targetObject;

    //go through the paths looking for the getters.

    for (String property : propertyPath) {

        ExecutableElement element = ModelUtils.findGetterOnType(currentObject, property);

        //the target object should have the getter.
        if (element == null) {
            messager.printMessage(Diagnostic.Kind.MANDATORY_WARNING,
                    "Property " + property + " not found on type: " + currentObject,
                    sourceConfig.getConfiguredElement());
            break; //no further analysis can be done.
        } else {
            currentObject = (TypeElement) processingEnv.getTypeUtils().asElement(element.getReturnType());
        }
    }

}

From source file:org.jdto.tools.AnnotationConfigVerifier.java

private TypeElement extractTargetType(Element element, TypeElement annotationElement, Messager messager) {

    List<? extends AnnotationMirror> am = element.getAnnotationMirrors();

    for (AnnotationMirror annotationMirror : am) {
        if (annotationMirror.getAnnotationType().equals(annotationElement.asType())) {

            //the annotation has only one argument so is easy to extract the value
            Map<? extends ExecutableElement, ? extends AnnotationValue> map = processingEnv.getElementUtils()
                    .getElementValuesWithDefaults(annotationMirror);

            //iterate and return the first value.
            for (ExecutableElement executableElement : map.keySet()) {

                AnnotationValue val = map.get(executableElement);

                String type = val.getValue().toString();

                return super.processingEnv.getElementUtils().getTypeElement(type);
            }// w  ww.  j ava  2 s .c o m

            return null;

        }
    }

    messager.printMessage(Diagnostic.Kind.ERROR, "Could not find target class on element", element);
    return null;
}

From source file:org.jdto.tools.AnnotationConfigVerifier.java

private SourceConfiguration extractSourceProperty(TypeElement element, Element getter, Messager msg) {

    //check for annotations.
    Source annot = getter.getAnnotation(Source.class);

    if (annot != null) {
        return new SourceConfiguration(annot.value(), getter);
    }/*  w ww . j  ava2s  . c  o  m*/

    //normalize the value.
    String name = getter.getSimpleName().toString();

    name = (name.startsWith("is")) ? name.substring(2) : name.substring(3);

    //uncapitalize.
    name = StringUtils.uncapitalize(name);

    //config might be on the setter which is incorrect.
    Element setter = ModelUtils.findSetterOnType(element, name);

    annot = setter.getAnnotation(Source.class);

    if (annot != null) {
        msg.printMessage(Diagnostic.Kind.MANDATORY_WARNING,
                "@Source or @Sources annotation must not appear on setters", setter);
    }

    //at this point the annotaiton is either on the field or not present.
    Element field = ModelUtils.findFieldOnType(element, name);

    //if no field is found, that is a valid configuration. return the name of the property.
    if (field == null) {
        return new SourceConfiguration(name, getter);
    }

    //if no annotation is present on the field then also return the field name.
    annot = field.getAnnotation(Source.class);

    if (annot == null) {
        return new SourceConfiguration(name, field);
    }

    //if the field is annotated, issue a compiler warning for performance.
    msg.printMessage(Diagnostic.Kind.MANDATORY_WARNING,
            "Annotations on getters perform better than annotations on fields.", field);

    return new SourceConfiguration(annot.value(), field);
}