Conditions | 12 |
Total Lines | 28 |
Code Lines | 18 |
Lines | 28 |
Ratio | 100 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like net.labymod.serverapi.Addons.getAddons(JsonObject) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | package net.labymod.serverapi; |
||
18 | View Code Duplication | public static List<Addon> getAddons( JsonObject jsonObject ) { |
|
19 | if ( !jsonObject.has( "addons" ) || !jsonObject.get( "addons" ).isJsonArray() ) |
||
20 | return new ArrayList<>(); |
||
21 | |||
22 | List<Addon> addons = new ArrayList<>(); |
||
23 | |||
24 | for ( JsonElement arrayElement : jsonObject.get( "addons" ).getAsJsonArray() ) { |
||
25 | if ( !arrayElement.isJsonObject() ) |
||
26 | continue; |
||
27 | |||
28 | JsonObject arrayObject = arrayElement.getAsJsonObject(); |
||
29 | |||
30 | if ( !arrayObject.has( "uuid" ) || !arrayObject.get( "uuid" ).isJsonPrimitive() || !arrayObject.get( "uuid" ).getAsJsonPrimitive().isString() |
||
31 | || !arrayObject.has( "name" ) || !arrayObject.get( "name" ).isJsonPrimitive() || !arrayObject.get( "name" ).getAsJsonPrimitive().isString() ) |
||
32 | continue; |
||
33 | |||
34 | UUID uuid = null; |
||
35 | |||
36 | try { |
||
37 | uuid = UUID.fromString( arrayObject.get( "uuid" ).getAsString() ); |
||
38 | } catch ( IllegalArgumentException ex ) { |
||
39 | continue; |
||
40 | } |
||
41 | |||
42 | addons.add( new Addon( uuid, arrayObject.get( "name" ).getAsString() ) ); |
||
43 | } |
||
44 | |||
45 | return addons; |
||
46 | } |
||
48 |