@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
 private void castAndSetProperty(
     InvokeInstruction insn, String property, List<Instruction> instructions, Class<?> primitive) {
   Variable castVar = program.createVariable();
   InvokeInstruction castInvoke = new InvokeInstruction();
   castInvoke.setType(InvocationType.SPECIAL);
   String primitiveCapitalized = primitive.getName();
   primitiveCapitalized =
       Character.toUpperCase(primitiveCapitalized.charAt(0)) + primitiveCapitalized.substring(1);
   castInvoke.setMethod(
       new MethodReference(
           ResourceAccessor.class, "castFrom" + primitiveCapitalized, primitive, Object.class));
   castInvoke.getArguments().add(insn.getArguments().get(0));
   castInvoke.setReceiver(castVar);
   instructions.add(castInvoke);
   setProperty(insn, property, instructions, castVar);
 }