2017-03-07 54 views
2

從昨天開始,我一直在尋找一種方法來做到這一點。我擁有的是來自第三方的數百個POJO,並且需要根據業務規則對這些POJO應用屬性。我正在避免改變POJO,因爲第三方可能會重新創建它們,因此造成管理文件的惡夢。JAVA是否可以動態地讓一個類擴展另一個類?

我試圖做的是動態地讓一個類擴展另一個類。例如, 。

POJO:Foo.java

package abc.service; 

public class Foo { 
    private String greeting = ""; 

    public Foo(){ 
     gretting = "Good morning"; 
    } 

    public String getGreeting(){ 
     return greeting; 
    } 
} 
// end file 

礦:Bar.java

package abc.service; 

public class Bar { 
    private String claim = ""; 

    public Bar(){ 
     claim = "You're correct"; 
    } 

    public String getClaim(){ 
     return claim; 
    } 
} 
// end file 

礦:TestMe.java

試圖在這裏一類與...隔開POJO有一個POJO擴展我的另一個類。

這是超越JAVA的能力嗎?

package abc; 

public class TestMe { 
    Foo f = new Foo(); 
    Class c1 = f.getClass(); 

    Bar b = new Bar(); 
    Class c2 = b.getClass(); 

    Class merged = c2.asSubclass(c1); 

    // Trying to call the Foo method 
    System.out.println(merged.getGreeting()); 
    // Trying to call the Bar method 
    System.out.println(merged.getClaim()); 
} 

此外,發生的是從所提供的POJO創建JSON模式。但POJO僅基於UPDATE記錄方案。我正在尋找最佳方式讓POJO爲CREATE記錄場景擴展另一個類,這就是爲什麼我要動態地讓POJO在需要時擴展我的代碼的原因。

  • 需要生成POJO的用於UPDATE
  • 需要驗證自己的JSON JSON模式符合UPDATE
  • POJO的要求,需要他們的JSON轉換成的POJO的UPDATE

而且,

  • 需要爲CREATE生成POJO的json模式
  • 需要覈實他們的JSON符合POJO的要求製造
  • 需要把他們的JSON轉換成POJO的用於創建

使用傑克遜密新和ObjectMapper我能到我的代碼中動態適用於類當創建模式時,我遇到的問題是當試圖讓POJO擴展Mixin不會解決問題的類時。

+1

否(我必須鍵入至少13個字符,所以)... –

+0

號使用接口或抽象類試過嗎? –

+0

@EngineerDollery我當然希望它是可能的,否則嘲笑框架將無法正常工作;-) –

回答

1

這是不可能的。

簡而言之,JAVA目前(至最新版本)沒有規定在運行時動態擴展類並加載到JVM。

5

用普通的Java:不,它不能完成。

您可以在構建過程中或運行時更改字節碼。但是很難,而且沒有很多文檔。

AspectJ的declare parents表達式可能是構建時最簡單的方法。

如果您想在運行時執行此操作,請查看asm,CGLib或ByteBuddy等框架。但是您將不得不從自定義的ClassLoader或代理中運行代碼。

0

而不是擴展,你應該使用設計模式。例如Stategy Pattern。這可以讓你改變你的策略動態。

4

您可以使用組合而不是繼承。

public class Foo { 
    private String greeting = ""; 

    public Foo(){ 
     gretting = "Good morning"; 
    } 

    public String getGreeting(){ 
     return greeting; 
    } 
} 

你的類

public class Bar { 
    private String claim = ""; 
    private Foo foo; 

    public Bar(){ 
     claim = "You're correct"; 
     foo = new Foo(); 
    } 

    public String getClaim(){ 
     return claim; 
    } 

    public Foo getFoo(){ 
     return foo; 
    } 
} 

而且測試

public class TestMe { 

    // Trying to call the Foo method 
    System.out.println(bar.getFoo().getGreeting()); 
    // Trying to call the Bar method 
    System.out.println(bar.getClaim()); 
} 

或者你可以做你的類不同的一點。

public class Bar { 
     private String claim = ""; 
     private Foo foo; 

     public Bar(){ 
      claim = "You're correct"; 
      foo = new Foo(); 
     } 

     public String getClaim(){ 
      return claim; 
     } 

     public String getGreeting(){ 
      return foo.getGreeting(); 
     } 
    } 

而且測試

public class TestMe { 

    // Trying to call the Foo method 
    System.out.println(bar.getGreeting()); 
    // Trying to call the Bar method 
    System.out.println(bar.getClaim()); 
} 
相關問題