1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.booter;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Objects;
25 import java.util.Properties;
26
27 import static java.nio.charset.StandardCharsets.ISO_8859_1;
28 import static org.apache.maven.surefire.api.util.ReflectionUtils.loadClass;
29
30
31
32
33 public final class TypeEncodedValue {
34 private final String type;
35 private final String value;
36
37 public TypeEncodedValue(String type, String value) {
38 this.type = type;
39 this.value = value;
40 }
41
42 private boolean isTypeClass() {
43 return Class.class.getName().equals(type);
44 }
45
46 public Object getDecodedValue() {
47 return getDecodedValue(Thread.currentThread().getContextClassLoader());
48 }
49
50 public Object getDecodedValue(ClassLoader classLoader) {
51 if (type.trim().isEmpty()) {
52 return null;
53 } else if (type.equals(String.class.getName())) {
54 return value;
55 } else if (isTypeClass()) {
56 return loadClass(classLoader, value);
57 } else if (type.equals(File.class.getName())) {
58 return new File(value);
59 } else if (type.equals(Boolean.class.getName())) {
60 return Boolean.valueOf(value);
61 } else if (type.equals(Integer.class.getName())) {
62 return Integer.valueOf(value);
63 } else if (type.equals(Properties.class.getName())) {
64 Properties result = new Properties();
65 try {
66 result.load(new ByteArrayInputStream(value.getBytes(ISO_8859_1)));
67 return result;
68 } catch (IOException e) {
69 throw new IllegalStateException("bug in property conversion", e);
70 }
71 } else {
72 throw new IllegalArgumentException("Unknown parameter type: " + type);
73 }
74 }
75
76 @Override
77 public boolean equals(Object o) {
78 if (this == o) {
79 return true;
80 }
81 if (o == null || getClass() != o.getClass()) {
82 return false;
83 }
84
85 TypeEncodedValue that = (TypeEncodedValue) o;
86
87 return equalsType(that) && equalsValue(that);
88 }
89
90 @Override
91 public int hashCode() {
92 int result = type != null ? type.hashCode() : 0;
93 result = 31 * result + (value != null ? value.hashCode() : 0);
94 return result;
95 }
96
97 @Override
98 public String toString() {
99 return "TypeEncodedValue{" + "type='" + type + '\'' + ", value='" + value + '\'' + '}';
100 }
101
102 private boolean equalsType(TypeEncodedValue that) {
103 return Objects.equals(type, that.type);
104 }
105
106 private boolean equalsValue(TypeEncodedValue that) {
107 return Objects.equals(value, that.value);
108 }
109 }