-
Notifications
You must be signed in to change notification settings - Fork 234
/
BeanPropertyMatcher.java
168 lines (156 loc) · 5.61 KB
/
BeanPropertyMatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc., Stephen Connolly.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.plugins.credentials.matchers;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatcher;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;
import org.apache.commons.lang.StringEscapeUtils;
/**
* Matches credentials that have a Java Bean property with an expected value.
*
* @since 2.1.0
*/
public class BeanPropertyMatcher<T extends Serializable> implements CredentialsMatcher, CredentialsMatcher.CQL {
/**
* Standardize serialization.
*/
private static final long serialVersionUID = 1L;
/**
* The property name.
*/
@NonNull
private final String name;
/**
* The expected value.
*/
@CheckForNull
private final T expected;
/**
* Constructs an instance that matches the specified java bean property against the supplied value.
*
* @param name the property name.
* @param expected the expected value.
*/
public BeanPropertyMatcher(@NonNull String name, @CheckForNull T expected) {
this.name = name;
this.expected = expected;
}
/**
* {@inheritDoc}
*/
@Override
public String describe() {
if (expected == null) {
return String.format("(%s == null)", name);
}
if (expected instanceof String) {
return String.format("(%s == \"%s\")", name, StringEscapeUtils.escapeJava((String) expected));
}
if (expected instanceof Character) {
return String.format("(%s == \'%s\')", name, StringEscapeUtils.escapeJava(expected.toString()));
}
if (expected instanceof Number) {
return String.format("(%s == %s)", name, expected);
}
if (expected instanceof Boolean) {
return expected.toString();
}
if (expected instanceof Enum) {
return String.format("(%s == %s.%s)", name, ((Enum) expected).getDeclaringClass().getName(), ((Enum) expected).name());
}
return null; // we cannot describe the expected value in CQL
}
/**
* {@inheritDoc}
*/
@Override
public boolean matches(@NonNull Credentials item) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(item.getClass());
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (name.equals(pd.getName())) {
Method readMethod = pd.getReadMethod();
if (readMethod == null) {
return false; // we cannot read it therefore it cannot be a match
}
try {
Object actual = readMethod.invoke(item);
return Objects.equals(expected, actual);
} catch (IllegalAccessException | InvocationTargetException e) {
return false; // if we cannot access it then it's not a match
}
}
}
return false; // if there is no corresponding property then it cannot be a match
} catch (IntrospectionException e) {
return false; // if we cannot introspect it then it cannot be a match
}
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + (expected != null ? expected.hashCode() : 0);
return result;
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BeanPropertyMatcher<?> that = (BeanPropertyMatcher<?>) o;
if (!name.equals(that.name)) {
return false;
}
return Objects.equals(expected, that.expected);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("BeanPropertyMatcher{");
sb.append("name='").append(name).append('\'');
sb.append(", expected=").append(expected);
sb.append('}');
return sb.toString();
}
}