2017-02-20 7 views
1

Byte Buddyが私が追加したアノテーションのデフォルト値を出さないようにする方法はありますか?以下の例をビルドプラグインに基づいて、bazフィールドの@XmlAttribute注釈の結果のバイトコードから冗長なrequirednamespaceの値を省きたいと思います。Byte Buddyがアノテーションのデフォルト値を出力しないようにすることはできますか?

FOO/Bar.java:

package foo; 

import javax.xml.bind.annotation.XmlAttribute; 

public class Bar { 
    @XmlAttribute(name = "qux") 
    public String qux; 
} 

ネット/ bytebuddy /テスト/ SimplePlugin.java:

...  
public class SimplePlugin implements Plugin { 
...  
    @Override 
    public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription) { 
     return builder.defineField("baz", String.class, Visibility.PUBLIC) 
      .annotateField(AnnotationDescription.Builder.ofType(XmlAttribute.class) 
       .define("name", "baz") 
       .build()); 
    } 
} 

FOO/Bar.class:

// 
// Source code recreated from a .class file by IntelliJ IDEA 
// (powered by Fernflower decompiler) 
// 

package foo; 

import javax.xml.bind.annotation.XmlAttribute; 

public class Bar { 
    @XmlAttribute(
     name = "qux" 
    ) 
    public String qux; 
    @XmlAttribute(
     name = "baz", 
     required = false, 
     namespace = "##default" 
    ) 
    public String baz; 

    public Bar() { 
    } 
} 

答えて

1

バイトバディできます既定の注釈値をスキップするように構成できます。ただし、Byte Buddyのコンフィグレーションは、Pluginインターフェイスを実装した変換ビルドプラグインの対象外です。 Byte Buddy APIは、別のEntryPointインターフェイスを提供し、これを使用してByte Buddyの初期化を制御することができます。

ネット/ bytebuddy /テスト/ SimpleEntryPoint.java:

package net.bytebuddy.test; 

... 

public class SimpleEntryPoint implements EntryPoint { 
    @Override 
    public ByteBuddy getByteBuddy() { 
     return new ByteBuddy() 
      .with(AnnotationValueFilter.Default.SKIP_DEFAULTS); 
    } 

    ... 
} 

のpom.xml:

... 
    <plugin> 
      <groupId>net.bytebuddy</groupId> 
      <artifactId>byte-buddy-maven-plugin</artifactId> 
      <executions> 
       <execution> 
        <goals> 
         <goal>transform</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <initialization> 
        <entryPoint>net.bytebuddy.test.SimpleEntryPoint</entryPoint> 
       </initialization> 
       <transformations> 
        <transformation> 
         <plugin>net.bytebuddy.test.SimplePlugin</plugin> 
        </transformation> 
       </transformations> 
      </configuration> 
     </plugin> 
... 

がfoo/Bar.class:

// 
// Source code recreated from a .class file by IntelliJ IDEA 
// (powered by Fernflower decompiler) 
// 

package foo; 

import javax.xml.bind.annotation.XmlAttribute; 

public class Bar { 
    @XmlAttribute(
     name = "qux" 
    ) 
    public String qux; 
    @XmlAttribute(
     name = "baz" 
    ) 
    public String baz; 

    public Bar() { 
    } 
} 
関連する問題