Java Code Examples for javax.annotation.processing.Messager#printMessage()

The following examples show how to use javax.annotation.processing.Messager#printMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationProcessing.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (processingEnv.getElementUtils().getModuleElement("m1x") == null) {
        throw new AssertionError("No m1x module found.");
    }

    Messager messager = processingEnv.getMessager();

    for (TypeElement clazz : ElementFilter.typesIn(roundEnv.getRootElements())) {
        for (VariableElement field : ElementFilter.fieldsIn(clazz.getEnclosedElements())) {
            messager.printMessage(Kind.NOTE, "field: " + field.getSimpleName());
        }
    }

    return false;
}
 
Example 2
Source File: SpecParser.java    From dataenum with Apache License 2.0 6 votes vote down vote up
public static Spec parse(Element element, ProcessingEnvironment processingEnv) {
  Messager messager = processingEnv.getMessager();

  if (element.getKind() != ElementKind.INTERFACE) {
    messager.printMessage(
        Diagnostic.Kind.ERROR, "@DataEnum can only be used on interfaces.", element);
    return null;
  }

  TypeElement dataEnum = (TypeElement) element;

  List<TypeVariableName> typeVariableNames = new ArrayList<>();
  for (TypeParameterElement typeParameterElement : dataEnum.getTypeParameters()) {
    typeVariableNames.add(TypeVariableName.get(typeParameterElement));
  }

  List<Value> values = ValuesParser.parse(dataEnum, processingEnv);
  if (values == null) {
    return null;
  }

  ClassName enumInterface = ClassName.get(dataEnum);
  return new Spec(enumInterface, typeVariableNames, values);
}
 
Example 3
Source File: AutoUtils.java    From fastjgame with Apache License 2.0 6 votes vote down vote up
/**
 * @param originTypeElement 原始类文件,用于获取包名,以及打印错误
 */
public static void writeToFile(final TypeElement originTypeElement, final TypeSpec.Builder typeBuilder,
                               final Elements elementUtils, final Messager messager, final Filer filer) {
    final TypeSpec typeSpec = typeBuilder.build();
    final JavaFile javaFile = JavaFile
            .builder(getPackageName(originTypeElement, elementUtils), typeSpec)
            // 不用导入java.lang包
            .skipJavaLangImports(true)
            // 4空格缩进
            .indent("    ")
            .build();
    try {
        // 输出到processingEnv.getFiler()会立即参与编译
        // 如果自己指定路径,可以生成源码到指定路径,但是可能无法被编译器检测到,本轮无法参与编译,需要再进行一次编译
        javaFile.writeTo(filer);
    } catch (IOException e) {
        messager.printMessage(Diagnostic.Kind.ERROR, getStackTrace(e), originTypeElement);
    }
}
 
Example 4
Source File: MessagerDiags.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnv) {
    Messager messager = processingEnv.getMessager();
    for (Element e : roundEnv.getRootElements()) {
        messager.printMessage(WARNING, WRN_NO_SOURCE);
        messager.printMessage(WARNING, WRN_WITH_SOURCE, e);
        messager.printMessage(WARNING, WRN_NO_SOURCE);
    }
    return false;
}
 
Example 5
Source File: MessagerDiags.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnv) {
    Messager messager = processingEnv.getMessager();
    for (Element e : roundEnv.getRootElements()) {
        messager.printMessage(WARNING, WRN_NO_SOURCE);
        messager.printMessage(WARNING, WRN_WITH_SOURCE, e);
        messager.printMessage(WARNING, WRN_NO_SOURCE);
    }
    return false;
}
 
Example 6
Source File: MessagerDiags.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnv) {
    Messager messager = processingEnv.getMessager();
    for (Element e : roundEnv.getRootElements()) {
        messager.printMessage(WARNING, WRN_NO_SOURCE);
        messager.printMessage(WARNING, WRN_WITH_SOURCE, e);
        messager.printMessage(WARNING, WRN_NO_SOURCE);
    }
    return false;
}
 
Example 7
Source File: MessagerDiags.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnv) {
    Messager messager = processingEnv.getMessager();
    for (Element e : roundEnv.getRootElements()) {
        messager.printMessage(WARNING, WRN_NO_SOURCE);
        messager.printMessage(WARNING, WRN_WITH_SOURCE, e);
        messager.printMessage(WARNING, WRN_NO_SOURCE);
    }
    return false;
}
 
Example 8
Source File: MessagerDiags.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations,
                       RoundEnvironment roundEnv) {
    Messager messager = processingEnv.getMessager();
    for (Element e : roundEnv.getRootElements()) {
        messager.printMessage(WARNING, WRN_NO_SOURCE);
        messager.printMessage(WARNING, WRN_WITH_SOURCE, e);
        messager.printMessage(WARNING, WRN_NO_SOURCE);
    }
    return false;
}
 
Example 9
Source File: CodeGenerator.java    From Barricade with Apache License 2.0 5 votes vote down vote up
/**
 * Generates the code for the Barricade configuration based on the annotations found.
 *
 * @param processingEnv Processing environment
 * @param configs Configuration detected by annotation processing
 * @param messager Messager to print logs
 * @throws IOException
 */
static void generateClass(ProcessingEnvironment processingEnv,
    HashMap<String, BarricadeResponseSet> configs, Messager messager) throws IOException {

  messager.printMessage(Diagnostic.Kind.NOTE, "Generating configuration code...");

  TypeSpec.Builder classBuilder = classBuilder(CLASS_NAME).addModifiers(PUBLIC, FINAL);

  FieldSpec valuesField = FieldSpec.builder(TYPE_CONFIG, "configs").addModifiers(PRIVATE).build();
  FieldSpec instanceField =
      FieldSpec.builder(ClassName.get(PACKAGE_NAME, CLASS_NAME), "barricadeConfig")
          .addModifiers(PRIVATE, STATIC)
          .build();

  MethodSpec.Builder instanceMethodBuilder = generateGetInstanceMethodBuilder();
  MethodSpec.Builder constructorMethodBuilder = generateConstructorBuilder(configs, messager);
  MethodSpec.Builder valuesMethod = generateGetConfigsMethodBuilder();
  MethodSpec.Builder getResponseMethodBuilder = generateGetResponseMethodBuilder();

  classBuilder.addType(generateEndpointsInnerClass(configs.keySet()));
  classBuilder.addType(generateResponsesInnerClass(configs));
  classBuilder.addField(instanceField);
  classBuilder.addField(valuesField);
  classBuilder.addMethod(instanceMethodBuilder.build());
  classBuilder.addMethod(constructorMethodBuilder.build());
  classBuilder.addMethod(valuesMethod.build());
  classBuilder.addMethod(getResponseMethodBuilder.build());

  classBuilder.addSuperinterface(IBarricadeConfig.class);

  JavaFile.Builder javaFileBuilder = builder(PACKAGE_NAME, classBuilder.build());
  JavaFile javaFile = javaFileBuilder.build();
  javaFile.writeTo(processingEnv.getFiler());

  messager.printMessage(Diagnostic.Kind.NOTE, "Code generation complete!");
}
 
Example 10
Source File: ProcessorUtils.java    From MRouter with Apache License 2.0 4 votes vote down vote up
private static void printMessage(Messager messager, Element element, Diagnostic.Kind kind, String message, Object... args) {
    if (args.length > 0) {
        message = String.format(message, args);
    }
    messager.printMessage(kind, message, element);
}
 
Example 11
Source File: TestClose2.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Messager messager = processingEnv.getMessager();
    messager.printMessage(Diagnostic.Kind.NOTE, "processing");
    return true;
}
 
Example 12
Source File: LetvPluginAnnotationProcessor.java    From letv with Apache License 2.0 4 votes vote down vote up
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Messager messager = this.processingEnv.getMessager();
    messager.printMessage(Kind.NOTE, "实现@LetvClassAutoLoad注解的class 数量: " + annotations.size());
    if (annotations.size() > 0) {
        String path = "./app/src/main/assets/";
        File folder = new File(path);
        if (!folder.exists()) {
            folder.mkdir();
        }
        try {
            File staticJsonFile = new File(path + "static.txt");
            if (!staticJsonFile.exists()) {
                staticJsonFile.createNewFile();
            }
            InputStreamReader inputReader = new InputStreamReader(new FileInputStream(staticJsonFile));
            BufferedReader bufReader = new BufferedReader(inputReader);
            Set<String> resultSet = new HashSet();
            while (true) {
                String line = bufReader.readLine();
                if (line == null) {
                    break;
                }
                resultSet.add(line);
            }
            bufReader.close();
            inputReader.close();
            boolean hasDif = false;
            for (TypeElement te : annotations) {
                for (Element e : roundEnv.getElementsAnnotatedWith(te)) {
                    String className = e.toString();
                    if (!resultSet.contains(className)) {
                        resultSet.add(className);
                        hasDif = true;
                    }
                }
            }
            if (hasDif) {
                messager.printMessage(Kind.NOTE, "更新class映射文件 static.txt");
                BufferedWriter outPut = new BufferedWriter(new FileWriter(staticJsonFile));
                for (String str : resultSet) {
                    outPut.write(str + "\n");
                    messager.printMessage(Kind.NOTE, "写入中: " + str);
                }
                outPut.close();
                messager.printMessage(Kind.NOTE, "更新class映射文件 static.txt 成功");
            } else {
                messager.printMessage(Kind.NOTE, "不存在未包含 class name");
                return true;
            }
        } catch (IOException e2) {
            e2.printStackTrace();
            messager.printMessage(Kind.NOTE, "更新class映射文件static.txt 失败 :  /app/src/main/assets/ 路径无效,请检查路径");
        }
    }
    return true;
}
 
Example 13
Source File: ValueParser.java    From dataenum with Apache License 2.0 4 votes vote down vote up
static Value parse(Element element, ProcessingEnvironment processingEnv) {
  Messager messager = processingEnv.getMessager();

  if (element.getKind() != ElementKind.METHOD) {
    messager.printMessage(
        Diagnostic.Kind.ERROR,
        String.format(
            "Value specs must be methods, found %s: %s",
            element.getKind().toString().toLowerCase(), element),
        element);
    return null;
  }

  ExecutableElement methodElement = (ExecutableElement) element;
  if (!isValueSpecMarker(methodElement.getReturnType(), processingEnv)) {
    messager.printMessage(
        Diagnostic.Kind.ERROR,
        String.format(
            "Value specs must return dataenum_case, found %s: %s",
            methodElement.getReturnType(), element),
        element);
    return null;
  }

  if (methodElement.getTypeParameters().size() != 0) {
    messager.printMessage(
        Diagnostic.Kind.ERROR,
        String.format(
            "Type parameters must be specified on the top-level interface, found: %s", element),
        element);
    return null;
  }

  if (methodElement.isVarArgs()) {
    messager.printMessage(
        Diagnostic.Kind.ERROR,
        String.format("Vararg parameters not permitted: %s", element),
        element);
    return null;
  }

  List<Parameter> parameters = new ArrayList<>();
  for (VariableElement parameterElement : methodElement.getParameters()) {
    String parameterName = parameterElement.getSimpleName().toString();
    TypeName parameterType = TypeName.get(parameterElement.asType());

    boolean nullable = isAnnotationPresent(parameterElement, ValueParser::isNullableAnnotation);
    boolean redacted = isAnnotationPresent(parameterElement, ValueParser::isRedactedAnnotation);
    Element parameterTypeElement =
        processingEnv.getTypeUtils().asElement(parameterElement.asType());
    boolean isEnum =
        parameterTypeElement != null && parameterTypeElement.getKind() == ElementKind.ENUM;

    parameters.add(new Parameter(parameterName, parameterType, nullable, redacted, isEnum));
  }

  String javadoc = processingEnv.getElementUtils().getDocComment(element);

  if (javadoc != null) {
    javadoc = javadoc.trim();
  }

  String valueSimpleName = methodElement.getSimpleName().toString();
  return new Value(
      valueSimpleName, javadoc, parameters, parseMethodAnnotations(methodElement, messager));
}
 
Example 14
Source File: DataEnumProcessor.java    From dataenum with Apache License 2.0 4 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
  Filer filer = processingEnv.getFiler();
  Messager messager = processingEnv.getMessager();

  AccessSelector accessSelector =
      new AccessSelector(roundEnvironment.getElementsAnnotatedWith(ConstructorAccess.class));

  for (Element element : roundEnvironment.getElementsAnnotatedWith(DataEnum.class)) {
    try {

      Spec spec = SpecParser.parse(element, processingEnv);
      if (spec == null) {
        continue;
      }

      OutputSpec outputSpec = OutputSpecFactory.create(spec);
      TypeSpec outputTypeSpec =
          SpecTypeFactory.create(
              outputSpec,
              accessSelector.accessModifierFor(outputSpec.outputClass().packageName()),
              element);

      JavaFile.Builder javaFileBuilder =
          JavaFile.builder(outputSpec.outputClass().packageName(), outputTypeSpec);
      if (needsCheckNotNull(spec)) {
        javaFileBuilder.addStaticImport(DataenumUtils.class, "checkNotNull");
      }
      if (needsNullSafeEquals(spec)) {
        javaFileBuilder.addStaticImport(DataenumUtils.class, "equal");
      }

      JavaFile javaFile = javaFileBuilder.build();
      javaFile.writeTo(filer);

    } catch (IOException | ParserException e) {
      messager.printMessage(Diagnostic.Kind.ERROR, e.getMessage());
    }
  }

  return false;
}
 
Example 15
Source File: AnnotatedClass.java    From AnnotationProcessorStarter with Apache License 2.0 4 votes vote down vote up
private void logError(final Messager pMessager, final Element pElement, final String pMessage, final Object... pArgs) {
    pMessager.printMessage(Diagnostic.Kind.ERROR, String.format(pMessage, pArgs), pElement);
}
 
Example 16
Source File: ComponentsProcessingException.java    From litho with Apache License 2.0 4 votes vote down vote up
@Override
public void print(Messager messager) {
  messager.printMessage(Diagnostic.Kind.ERROR, getMessage(), mElement, mAnnotationMirror);
}
 
Example 17
Source File: MethodExtractorUtils.java    From litho with Apache License 2.0 4 votes vote down vote up
/** @return a list of params for a method. */
static List<MethodParamModel> getMethodParams(
    ExecutableElement method,
    Messager messager,
    List<Class<? extends Annotation>> permittedAnnotations,
    List<Class<? extends Annotation>> permittedInterStageInputAnnotations,
    List<Class<? extends Annotation>> delegateMethodAnnotationsThatSkipDiffModels) {

  final List<MethodParamModel> methodParamModels = new ArrayList<>();
  final List<Name> savedParameterNames = getSavedParameterNames(method);
  final List<? extends VariableElement> params = method.getParameters();

  for (int i = 0, size = params.size(); i < size; i++) {
    final VariableElement param = params.get(i);
    final String paramName =
        savedParameterNames == null
            ? param.getSimpleName().toString()
            : savedParameterNames.get(i).toString();

    try {
      final TypeSpec typeSpec = generateTypeSpec(param.asType());

      if (!typeSpec.isValid()) {
        messager.printMessage(
            Diagnostic.Kind.WARNING,
            String.format(
                "The type of '%s' cannot be fully determined at compile time. "
                    + "This can cause issues if the target referenced is from a different "
                    + "package. "
                    + "Learn more at https://fburl.com/fblitho-cross-package-error.",
                param.getSimpleName()),
            param);
      }

      methodParamModels.add(
          MethodParamModelFactory.create(
              typeSpec,
              paramName,
              getLibraryAnnotations(param, permittedAnnotations),
              getExternalAnnotations(param),
              permittedInterStageInputAnnotations,
              canCreateDiffModels(method, delegateMethodAnnotationsThatSkipDiffModels),
              param));
    } catch (Exception e) {
      throw new ComponentsProcessingException(
          param,
          String.format(
              "Error processing the param '%s'. Are your imports set up correctly? The causing error was: %s",
              param, e));
    }
  }

  return methodParamModels;
}
 
Example 18
Source File: TestClose2.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Messager messager = processingEnv.getMessager();
    messager.printMessage(Diagnostic.Kind.NOTE, "processing");
    return true;
}
 
Example 19
Source File: AnnotatedClass.java    From FastAdapter with MIT License 4 votes vote down vote up
private void logError(final Messager pMessager, final Element pElement, final String pMessage,
    final Object... pArgs) {
  pMessager.printMessage(Diagnostic.Kind.ERROR, String.format(pMessage, pArgs), pElement);
}
 
Example 20
Source File: TestClose2.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Messager messager = processingEnv.getMessager();
    messager.printMessage(Diagnostic.Kind.NOTE, "processing");
    return true;
}