3
3
import com .google .gson .TypeAdapter ;
4
4
import com .google .gson .stream .JsonReader ;
5
5
import com .google .gson .stream .JsonWriter ;
6
- import net .sf .anathema .hero .template .magic .AbilityFavoringType ;
7
- import net .sf .anathema .hero .template .magic .AttributeFavoringType ;
6
+ import net .sf .anathema .framework .environment .ObjectFactory ;
8
7
import net .sf .anathema .hero .template .magic .FavoringTraitType ;
9
8
10
9
import java .io .IOException ;
10
+ import java .util .Collection ;
11
11
12
12
public class FavoringTraitTypeAdapter extends TypeAdapter <FavoringTraitType > {
13
+ private final ObjectFactory objectFactory ;
14
+
15
+ public FavoringTraitTypeAdapter (ObjectFactory objectFactory ) {
16
+ this .objectFactory = objectFactory ;
17
+ }
18
+
13
19
@ Override
14
20
public void write (JsonWriter out , FavoringTraitType value ) throws IOException {
15
21
//nothing to do
@@ -18,11 +24,11 @@ public void write(JsonWriter out, FavoringTraitType value) throws IOException {
18
24
@ Override
19
25
public FavoringTraitType read (JsonReader in ) throws IOException {
20
26
String string = in .nextString ();
21
- if ( string . equals ( "Ability" )) {
22
- return new AbilityFavoringType ();
23
- }
24
- if ( string . equals ( "Attribute" )) {
25
- return new AttributeFavoringType ();
27
+ Collection < FavoringTraitType > types = objectFactory . instantiateAllImplementers ( FavoringTraitType . class );
28
+ for ( FavoringTraitType type : types ) {
29
+ if ( type . getId (). equals ( string )) {
30
+ return type ;
31
+ }
26
32
}
27
33
throw new IllegalArgumentException ("Unknown trait type: " + string );
28
34
}
0 commit comments