protected void buildClassLevelAnnotations(
      ClassFile classFile, ClassFile templateClassFile, ConstPool constantPool)
      throws NotFoundException {
    List<?> templateAttributes = templateClassFile.getAttributes();
    Iterator<?> templateItr = templateAttributes.iterator();
    Annotation templateEntityListeners = null;
    while (templateItr.hasNext()) {
      Object object = templateItr.next();
      if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
        AnnotationsAttribute attr = (AnnotationsAttribute) object;
        Annotation[] items = attr.getAnnotations();
        for (Annotation annotation : items) {
          String typeName = annotation.getTypeName();
          if (typeName.equals(EntityListeners.class.getName())) {
            templateEntityListeners = annotation;
          }
        }
      }
    }

    if (templateEntityListeners != null) {
      AnnotationsAttribute annotationsAttribute =
          new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag);
      List<?> attributes = classFile.getAttributes();
      Iterator<?> itr = attributes.iterator();
      Annotation existingEntityListeners = null;
      while (itr.hasNext()) {
        Object object = itr.next();
        if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
          AnnotationsAttribute attr = (AnnotationsAttribute) object;
          Annotation[] items = attr.getAnnotations();
          for (Annotation annotation : items) {
            String typeName = annotation.getTypeName();
            if (typeName.equals(EntityListeners.class.getName())) {
              logger.debug(
                  "Stripping out previous EntityListeners annotation at the class level - will merge into new EntityListeners");
              existingEntityListeners = annotation;
              continue;
            }
            annotationsAttribute.addAnnotation(annotation);
          }
          itr.remove();
        }
      }

      Annotation entityListeners =
          getEntityListeners(constantPool, existingEntityListeners, templateEntityListeners);
      annotationsAttribute.addAnnotation(entityListeners);

      classFile.addAttribute(annotationsAttribute);
    }
  }
예제 #2
0
  private void addOutputArg(List<String> args, Object object) {
    if (!(object instanceof String))
      throw new IllegalArgumentException("Only a single output file can be specified");

    String value = (String) object;
    args.add("-o");
    if (value.endsWith(".vcf")) {
      args.add(value);
      try {
        ConstPool constPool = annotatedWalker.getClassFile().getConstPool();
        AnnotationsAttribute att =
            new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
        Annotation ann = new Annotation(Output.class.getName(), constPool);
        att.addAnnotation(ann);

        CtClass writerClass = pool.get(VCFWriter.class.getName());
        CtField field = new CtField(writerClass, "out", annotatedWalker);
        field.setModifiers(Modifier.PUBLIC);
        field.getFieldInfo().addAttribute(att);
        annotatedWalker.addField(field);
      } catch (NotFoundException e) {
        throw new RuntimeException(e);
      } catch (CannotCompileException e) {
        throw new RuntimeException(e);
      }
    } else throw new IllegalArgumentException("Only VCF files are supported as output files");
  }
예제 #3
0
  public void testJIRA256() throws Exception {
    // CtClass ec = sloader.get("test5.Entity");

    CtClass cc = sloader.makeClass("test5.JIRA256");
    ClassFile ccFile = cc.getClassFile();
    ConstPool constpool = ccFile.getConstPool();

    AnnotationsAttribute attr =
        new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
    javassist.bytecode.annotation.Annotation entityAnno =
        new javassist.bytecode.annotation.Annotation("test5.Entity", constpool);
    // = new javassist.bytecode.annotation.Annotation(constpool, ec);

    entityAnno.addMemberValue(
        "value", new javassist.bytecode.annotation.ArrayMemberValue(constpool));
    attr.addAnnotation(entityAnno);
    ccFile.addAttribute(attr);

    cc.writeFile();
    Object o = make(cc.getName());
    assertTrue(o.getClass().getName().equals("test5.JIRA256"));

    java.lang.annotation.Annotation[] annotations = o.getClass().getDeclaredAnnotations();
    assertEquals(1, annotations.length);
  }
예제 #4
0
  private void addVCFArg(List<String> args, Object object) {
    List<String> vcfs = new ArrayList<String>();
    if (object instanceof String) {
      vcfs.addAll(Arrays.asList(((String) object).split(",")));
    } else if (object instanceof List) {
      vcfs.add((String) object);
    }

    int count = 1;
    for (String vcf : vcfs) {
      args.add("--vcf" + count);
      args.add(vcf);
      try {
        ConstPool constPool = annotatedWalker.getClassFile().getConstPool();
        AnnotationsAttribute att =
            new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
        Annotation ann = new Annotation(Input.class.getName(), constPool);

        MemberValue fullName = new StringMemberValue("vcf" + count, constPool);
        ann.addMemberValue("fullName", fullName);
        att.addAnnotation(ann);

        RodBinding<VariantContext> dummy =
            new RodBinding<VariantContext>(VariantContext.class, "vcf", vcf, "VCF", null);
        CtClass rodClass = pool.get(dummy.getClass().getName());

        // If there are multiple vcfs, we number each one - vcf1, vcf2, ...
        // if there is only one, we just call it "vcf"
        CtField field =
            new CtField(rodClass, vcfs.size() > 1 ? "vcf" + count : "vcf", annotatedWalker);
        field.setModifiers(Modifier.PUBLIC);
        field.getFieldInfo().addAttribute(att);

        SignatureAttribute sig =
            new SignatureAttribute(
                constPool,
                "Lorg/broadinstitute/sting/commandline/RodBinding<Lorg/broadinstitute/sting/utils/variantcontext/VariantContext;>;;");
        field.getFieldInfo().addAttribute(sig);

        annotatedWalker.addField(field);

        if (config.containsKey("out")) {
          VCFHeaderInitializer vcfInit = new VCFHeaderInitializer(this);
          this.injections.get("initializers").add(vcfInit);
        }
      } catch (CannotCompileException e) {
        throw new RuntimeException(e);

      } catch (NotFoundException e) {
        throw new RuntimeException(e);
      }
      ++count;
    }
  }
  public byte[] transform(
      ClassLoader loader,
      String className,
      Class<?> classBeingRedefined,
      ProtectionDomain protectionDomain,
      byte[] classfileBuffer)
      throws IllegalClassFormatException {
    if (infos.isEmpty()) {
      return null;
    }
    String convertedClassName = className.replace('/', '.');
    SingleTableInheritanceInfo key = new SingleTableInheritanceInfo();
    key.setClassName(convertedClassName);
    int pos = infos.indexOf(key);
    if (pos >= 0) {
      try {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Converting " + convertedClassName + " to a SingleTable inheritance strategy.");
        }
        SingleTableInheritanceInfo myInfo = infos.get(pos);
        ClassFile classFile =
            new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
        ConstPool constantPool = classFile.getConstPool();
        AnnotationsAttribute annotationsAttribute =
            new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag);
        List<?> attributes = classFile.getAttributes();
        Iterator<?> itr = attributes.iterator();
        while (itr.hasNext()) {
          Object object = itr.next();
          if (AnnotationsAttribute.class.isAssignableFrom(object.getClass())) {
            AnnotationsAttribute attr = (AnnotationsAttribute) object;
            Annotation[] items = attr.getAnnotations();
            for (Annotation annotation : items) {
              String typeName = annotation.getTypeName();
              if (!typeName.equals(Inheritance.class.getName())
                  && ((myInfo.getDiscriminatorName() == null
                          && !typeName.equals(Table.class.getName()))
                      || myInfo.getDiscriminatorName() != null)) {
                annotationsAttribute.addAnnotation(annotation);
              }
            }
            itr.remove();
          }
        }
        Annotation inheritance = new Annotation(Inheritance.class.getName(), constantPool);
        ClassPool pool = ClassPool.getDefault();
        pool.importPackage("javax.persistence");
        pool.importPackage("java.lang");
        EnumMemberValue strategy =
            (EnumMemberValue)
                Annotation.createMemberValue(constantPool, pool.makeClass("InheritanceType"));
        strategy.setType(InheritanceType.class.getName());
        strategy.setValue(InheritanceType.SINGLE_TABLE.name());
        inheritance.addMemberValue("strategy", strategy);
        annotationsAttribute.addAnnotation(inheritance);
        if (myInfo.getDiscriminatorName() != null) {
          Annotation discriminator =
              new Annotation(DiscriminatorColumn.class.getName(), constantPool);
          StringMemberValue name = new StringMemberValue(constantPool);
          name.setValue(myInfo.getDiscriminatorName());
          discriminator.addMemberValue("name", name);
          EnumMemberValue discriminatorType =
              (EnumMemberValue)
                  Annotation.createMemberValue(constantPool, pool.makeClass("DiscriminatorType"));
          discriminatorType.setType(DiscriminatorType.class.getName());
          discriminatorType.setValue(myInfo.getDiscriminatorType().name());
          discriminator.addMemberValue("discriminatorType", discriminatorType);
          IntegerMemberValue length = new IntegerMemberValue(constantPool);
          length.setValue(myInfo.getDiscriminatorLength());
          discriminator.addMemberValue("length", length);

          annotationsAttribute.addAnnotation(discriminator);
        }
        classFile.addAttribute(annotationsAttribute);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream os = new DataOutputStream(bos);
        classFile.write(os);
        os.close();

        return bos.toByteArray();
      } catch (Exception ex) {
        ex.printStackTrace();
        throw new IllegalClassFormatException(
            "Unable to convert "
                + convertedClassName
                + " to a SingleTable inheritance strategy: "
                + ex.getMessage());
      }
    } else {
      return null;
    }
  }