@Override
  public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef)
      throws IOException {
    ClassReader cls = context.getClassSource().get(methodRef.getClassName());
    MethodReader method = cls.getMethod(methodRef.getDescriptor());
    AnnotationReader providerAnnot = method.getAnnotations().get(MetadataProvider.class.getName());

    AnnotationReader refAnnot = method.getAnnotations().get(MetadataProviderRef.class.getName());
    methodRef = MethodReference.parse(refAnnot.getValue("value").getString());

    // Find and instantiate metadata generator
    ValueType generatorType = providerAnnot.getValue("value").getJavaClass();
    String generatorClassName = ((ValueType.Object) generatorType).getClassName();
    Class<?> generatorClass;
    try {
      generatorClass = Class.forName(generatorClassName, true, context.getClassLoader());
    } catch (ClassNotFoundException e) {
      context
          .getDiagnostics()
          .error(
              new CallLocation(methodRef),
              "Can't find metadata provider class {{c0}}",
              generatorClassName);
      return;
    }
    Constructor<?> cons;
    try {
      cons = generatorClass.getConstructor();
    } catch (NoSuchMethodException e) {
      context
          .getDiagnostics()
          .error(
              new CallLocation(methodRef),
              "Metadata generator {{c0}} does not have " + "a public no-arg constructor",
              generatorClassName);
      return;
    }
    MetadataGenerator generator;
    try {
      generator = (MetadataGenerator) cons.newInstance();
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
      context
          .getDiagnostics()
          .error(
              new CallLocation(methodRef),
              "Error instantiating metadata " + "generator {{c0}}",
              generatorClassName);
      return;
    }
    DefaultMetadataGeneratorContext metadataContext =
        new DefaultMetadataGeneratorContext(
            context.getClassSource(), context.getClassLoader(), context.getProperties(), context);

    // Generate resource loader
    Resource resource = generator.generateMetadata(metadataContext, methodRef);
    writer.append("return ");
    ResourceWriterHelper.write(writer, resource);
    writer.append(';').softNewLine();
  }
예제 #2
0
 @Override
 public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef)
     throws IOException {
   writer.append("var result = [];").softNewLine();
   writer
       .append("for (var key in ")
       .append(context.getParameterName(1))
       .append(") {")
       .indent()
       .softNewLine();
   writer.append("result.push(key);").softNewLine();
   writer.outdent().append("}").softNewLine();
   writer.append("return result;").softNewLine();
 }