Skip to content

Commit 2ba396a

Browse files
committed
fix LOGBACK-1158
1 parent bd78efa commit 2ba396a

File tree

6 files changed

+164
-159
lines changed

6 files changed

+164
-159
lines changed

logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedBasicPropertyIA.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void body(InterpretationContext ec, String body) {
9696
break;
9797
case AS_BASIC_PROPERTY_COLLECTION:
9898
actionData.parentBean.addBasicProperty(actionData.propertyName, finalBody);
99+
break;
99100
default:
100101
addError("Unexpected aggregationType " + actionData.aggregationType);
101102
}

logback-core/src/main/java/ch/qos/logback/core/joran/util/PropertySetter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ public AggregationType computeAggregationType(String name) {
151151
return AggregationType.AS_BASIC_PROPERTY_COLLECTION;
152152
case AS_COMPLEX_PROPERTY:
153153
return AggregationType.AS_COMPLEX_PROPERTY_COLLECTION;
154+
case AS_COMPLEX_PROPERTY_COLLECTION:
155+
addError("Unexpected AggregationType " + type);
154156
}
155157
}
156158

@@ -164,12 +166,12 @@ public AggregationType computeAggregationType(String name) {
164166
}
165167

166168
private Method findAdderMethod(String name) {
167-
String propertyName = BeanUtil.INSTANCE.toLowerCamelCase(name);
169+
String propertyName = BeanUtil.SINGLETON.toLowerCamelCase(name);
168170
return beanDescription.getAdder(propertyName);
169171
}
170172

171173
private Method findSetterMethod(String name) {
172-
String propertyName = BeanUtil.INSTANCE.toLowerCamelCase(name);
174+
String propertyName = BeanUtil.SINGLETON.toLowerCamelCase(name);
173175
return beanDescription.getSetter(propertyName);
174176
}
175177

logback-core/src/main/java/ch/qos/logback/core/joran/util/beans/BeanDescriptionFactory.java

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,51 @@
1616
*/
1717
public class BeanDescriptionFactory {
1818

19-
public static final BeanDescriptionFactory INSTANCE=new BeanDescriptionFactory();
19+
public static final BeanDescriptionFactory INSTANCE = new BeanDescriptionFactory();
2020

21-
private BeanUtil beanUtil=BeanUtil.INSTANCE;
22-
23-
/**
24-
*
25-
* @param clazz to create a {@link BeanDescription} for.
26-
* @return a {@link BeanDescription} for the given class.
27-
*/
28-
public BeanDescription create(Class<?> clazz){
29-
Map<String, Method> propertyNameToGetter=new HashMap<String, Method>();
30-
Map<String, Method> propertyNameToSetter=new HashMap<String, Method>();
31-
Map<String, Method> propertyNameToAdder=new HashMap<String, Method>();
32-
Method[] methods = clazz.getMethods();
33-
for (Method method : methods) {
34-
if(beanUtil.isGetter(method)){
35-
String propertyName=beanUtil.getPropertyName(method);
36-
Method oldGetter = propertyNameToGetter.put(propertyName, method);
37-
if(oldGetter!=null){
38-
if(oldGetter.getName().startsWith(BeanUtil.PREFIX_GETTER_IS)){
39-
propertyNameToGetter.put(propertyName, oldGetter);
40-
}
41-
String message=String.format("Warning: Class '%s' contains multiple getters for the same property '%s'.", clazz.getCanonicalName(),propertyName);
42-
System.err.println(message);
43-
}
44-
}else if(beanUtil.isSetter(method)){
45-
String propertyName=beanUtil.getPropertyName(method);
46-
Method oldSetter = propertyNameToSetter.put(propertyName, method);
47-
if(oldSetter!=null){
48-
String message=String.format("Warning: Class '%s' contains multiple setters for the same property '%s'.", clazz.getCanonicalName(),propertyName);
49-
System.err.println(message);
50-
}
51-
}else if(beanUtil.isAdder(method)){
52-
String propertyName=beanUtil.getPropertyName(method);
53-
Method oldAdder = propertyNameToAdder.put(propertyName, method);
54-
if(oldAdder!=null){
55-
String message=String.format("Warning: Class '%s' contains multiple adders for the same property '%s'.", clazz.getCanonicalName(),propertyName);
56-
System.err.println(message);
57-
}
58-
}
59-
}
60-
return new BeanDescription(clazz,propertyNameToGetter,propertyNameToSetter,propertyNameToAdder);
61-
}
21+
private BeanUtil beanUtil = BeanUtil.SINGLETON;
6222

23+
/**
24+
*
25+
* @param clazz to create a {@link BeanDescription} for.
26+
* @return a {@link BeanDescription} for the given class.
27+
*/
28+
public BeanDescription create(Class<?> clazz) {
29+
Map<String, Method> propertyNameToGetter = new HashMap<String, Method>();
30+
Map<String, Method> propertyNameToSetter = new HashMap<String, Method>();
31+
Map<String, Method> propertyNameToAdder = new HashMap<String, Method>();
32+
Method[] methods = clazz.getMethods();
33+
for (Method method : methods) {
34+
if (beanUtil.isGetter(method)) {
35+
String propertyName = beanUtil.getPropertyName(method);
36+
Method oldGetter = propertyNameToGetter.put(propertyName, method);
37+
if (oldGetter != null) {
38+
if (oldGetter.getName().startsWith(BeanUtil.PREFIX_GETTER_IS)) {
39+
propertyNameToGetter.put(propertyName, oldGetter);
40+
}
41+
String message = String.format("Warning: Class '%s' contains multiple getters for the same property '%s'.", clazz.getCanonicalName(),
42+
propertyName);
43+
System.err.println(message);
44+
}
45+
} else if (beanUtil.isSetter(method)) {
46+
String propertyName = beanUtil.getPropertyName(method);
47+
Method oldSetter = propertyNameToSetter.put(propertyName, method);
48+
if (oldSetter != null) {
49+
String message = String.format("Warning: Class '%s' contains multiple setters for the same property '%s'.", clazz.getCanonicalName(),
50+
propertyName);
51+
System.err.println(message);
52+
}
53+
} else if (beanUtil.isAdder(method)) {
54+
String propertyName = beanUtil.getPropertyName(method);
55+
Method oldAdder = propertyNameToAdder.put(propertyName, method);
56+
if (oldAdder != null) {
57+
String message = String.format("Warning: Class '%s' contains multiple adders for the same property '%s'.", clazz.getCanonicalName(),
58+
propertyName);
59+
System.err.println(message);
60+
}
61+
}
62+
}
63+
return new BeanDescription(clazz, propertyNameToGetter, propertyNameToSetter, propertyNameToAdder);
64+
}
6365

6466
}

logback-core/src/main/java/ch/qos/logback/core/joran/util/beans/BeanUtil.java

Lines changed: 112 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -8,130 +8,129 @@
88
*/
99
public class BeanUtil {
1010

11-
public static final BeanUtil INSTANCE=new BeanUtil();
11+
public static final BeanUtil SINGLETON = new BeanUtil();
1212

13-
public static final String PREFIX_GETTER_IS="is";
14-
public static final String PREFIX_GETTER_GET="get";
15-
public static final String PREFIX_SETTER="set";
16-
public static final String PREFIX_ADDER="add";
13+
public static final String PREFIX_GETTER_IS = "is";
14+
public static final String PREFIX_GETTER_GET = "get";
15+
public static final String PREFIX_SETTER = "set";
16+
public static final String PREFIX_ADDER = "add";
1717

18-
/**
19-
*
20-
* @param method to check if it is an 'adder' method.
21-
* @return true if the given method is an 'adder' method.
22-
*/
23-
public boolean isAdder(Method method) {
24-
int parameterCount = getParameterCount(method);
25-
if(parameterCount!=1){
26-
return false;
27-
}
28-
Class<?> returnType = method.getReturnType();
29-
if(returnType!=void.class){
30-
return false;
31-
}
32-
String methodName = method.getName();
33-
return methodName.startsWith(PREFIX_ADDER);
34-
}
18+
/**
19+
*
20+
* @param method to check if it is an 'adder' method.
21+
* @return true if the given method is an 'adder' method.
22+
*/
23+
public boolean isAdder(Method method) {
24+
int parameterCount = getParameterCount(method);
25+
if (parameterCount != 1) {
26+
return false;
27+
}
28+
Class<?> returnType = method.getReturnType();
29+
if (returnType != void.class) {
30+
return false;
31+
}
32+
String methodName = method.getName();
33+
return methodName.startsWith(PREFIX_ADDER);
34+
}
3535

36-
/**
37-
*
38-
* @param method to check if it is a standard java beans getter.
39-
* @return true if the given method is a standard java beans getter.
40-
*/
41-
public boolean isGetter(Method method) {
42-
int parameterCount = getParameterCount(method);
43-
if(parameterCount>0){
44-
return false;
45-
}
46-
Class<?> returnType = method.getReturnType();
47-
if(returnType==void.class){
48-
return false;
49-
}
50-
String methodName = method.getName();
51-
if(!methodName.startsWith(PREFIX_GETTER_GET)&&!methodName.startsWith(PREFIX_GETTER_IS)){
52-
return false;
53-
}
54-
if(methodName.startsWith(PREFIX_GETTER_IS)){
55-
if(!returnType.equals(boolean.class)&&!returnType.equals(Boolean.class)){
56-
return false;
57-
}
58-
}
59-
return true;
60-
}
36+
/**
37+
*
38+
* @param method to check if it is a standard java beans getter.
39+
* @return true if the given method is a standard java beans getter.
40+
*/
41+
public boolean isGetter(Method method) {
42+
int parameterCount = getParameterCount(method);
43+
if (parameterCount > 0) {
44+
return false;
45+
}
46+
Class<?> returnType = method.getReturnType();
47+
if (returnType == void.class) {
48+
return false;
49+
}
50+
String methodName = method.getName();
51+
if (!methodName.startsWith(PREFIX_GETTER_GET) && !methodName.startsWith(PREFIX_GETTER_IS)) {
52+
return false;
53+
}
54+
if (methodName.startsWith(PREFIX_GETTER_IS)) {
55+
if (!returnType.equals(boolean.class) && !returnType.equals(Boolean.class)) {
56+
return false;
57+
}
58+
}
59+
return true;
60+
}
6161

62-
private int getParameterCount(Method method) {
63-
return method.getParameterTypes().length;
64-
}
62+
private int getParameterCount(Method method) {
63+
return method.getParameterTypes().length;
64+
}
6565

66-
/**
67-
*
68-
* @param method to check if it is a standard java beans setter.
69-
* @return true if the given method is a standard java beans setter.
70-
*/
71-
public boolean isSetter(Method method){
72-
int parameterCount = getParameterCount(method);
73-
if(parameterCount!=1){
74-
return false;
75-
}
76-
Class<?> returnType = method.getReturnType();
77-
if(returnType!=void.class){
78-
return false;
79-
}
80-
String methodName = method.getName();
81-
if(!methodName.startsWith(PREFIX_SETTER)){
82-
return false;
83-
}
84-
return true;
85-
}
66+
/**
67+
*
68+
* @param method to check if it is a standard java beans setter.
69+
* @return true if the given method is a standard java beans setter.
70+
*/
71+
public boolean isSetter(Method method) {
72+
int parameterCount = getParameterCount(method);
73+
if (parameterCount != 1) {
74+
return false;
75+
}
76+
Class<?> returnType = method.getReturnType();
77+
if (returnType != void.class) {
78+
return false;
79+
}
80+
String methodName = method.getName();
81+
if (!methodName.startsWith(PREFIX_SETTER)) {
82+
return false;
83+
}
84+
return true;
85+
}
8686

87-
/**
88-
* @param method to get the associated property name for.
89-
* @return The property name of the associated property if the given method matches a standard java beans getter or setter.
90-
*/
91-
public String getPropertyName(Method method) {
92-
String methodName = method.getName();
93-
String rawPropertyName=getSubstringIfPrefixMatches(methodName,PREFIX_GETTER_GET);
94-
if(rawPropertyName==null){
95-
rawPropertyName=getSubstringIfPrefixMatches(methodName, PREFIX_SETTER);
96-
}
97-
if(rawPropertyName==null){
98-
rawPropertyName=getSubstringIfPrefixMatches(methodName,PREFIX_GETTER_IS);
99-
}
100-
if(rawPropertyName==null){
101-
rawPropertyName=getSubstringIfPrefixMatches(methodName,PREFIX_ADDER);
102-
}
103-
return toLowerCamelCase(rawPropertyName);
104-
}
87+
/**
88+
* @param method to get the associated property name for.
89+
* @return The property name of the associated property if the given method matches a standard java beans getter or setter.
90+
*/
91+
public String getPropertyName(Method method) {
92+
String methodName = method.getName();
93+
String rawPropertyName = getSubstringIfPrefixMatches(methodName, PREFIX_GETTER_GET);
94+
if (rawPropertyName == null) {
95+
rawPropertyName = getSubstringIfPrefixMatches(methodName, PREFIX_SETTER);
96+
}
97+
if (rawPropertyName == null) {
98+
rawPropertyName = getSubstringIfPrefixMatches(methodName, PREFIX_GETTER_IS);
99+
}
100+
if (rawPropertyName == null) {
101+
rawPropertyName = getSubstringIfPrefixMatches(methodName, PREFIX_ADDER);
102+
}
103+
return toLowerCamelCase(rawPropertyName);
104+
}
105105

106-
/**
107-
* Converts the given String into lower camel case form.
108-
* @param string to decapitalize.
109-
* @return null if the given String is null.
110-
* Emtpy string if the given string is empty.
111-
* The given string if the first two consecutive letters are in upper case.
112-
* The given string with the first letter in lower case otherwise, which might be the given string.
113-
*/
114-
public String toLowerCamelCase(String string){
115-
if(string==null){
116-
return null;
117-
}
118-
if(string.isEmpty()){
119-
return string;
120-
}
121-
if (string.length() > 1 && Character.isUpperCase(string.charAt(1)) &&
122-
Character.isUpperCase(string.charAt(0))){
123-
return string;
106+
/**
107+
* Converts the given String into lower camel case form.
108+
* @param string to decapitalize.
109+
* @return null if the given String is null.
110+
* Emtpy string if the given string is empty.
111+
* The given string if the first two consecutive letters are in upper case.
112+
* The given string with the first letter in lower case otherwise, which might be the given string.
113+
*/
114+
public String toLowerCamelCase(String string) {
115+
if (string == null) {
116+
return null;
117+
}
118+
if (string.isEmpty()) {
119+
return string;
120+
}
121+
if (string.length() > 1 && Character.isUpperCase(string.charAt(1)) && Character.isUpperCase(string.charAt(0))) {
122+
return string;
124123
}
125124
char chars[] = string.toCharArray();
126125
chars[0] = Character.toLowerCase(chars[0]);
127126
return new String(chars);
128-
}
127+
}
129128

130-
private String getSubstringIfPrefixMatches(String wholeString,String prefix) {
131-
if(wholeString.startsWith(prefix)){
132-
return wholeString.substring(prefix.length());
133-
}
134-
return null;
135-
}
129+
private String getSubstringIfPrefixMatches(String wholeString, String prefix) {
130+
if (wholeString.startsWith(prefix)) {
131+
return wholeString.substring(prefix.length());
132+
}
133+
return null;
134+
}
136135

137136
}

logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Properties;
2121

2222
import javax.mail.Message;
23-
import javax.mail.MessagingException;
2423
import javax.mail.Multipart;
2524
import javax.mail.Session;
2625
import javax.mail.Transport;

logback-examples/src/main/java/chapters/appenders/mail/EMail.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ static public void main(String[] args) throws Exception {
5050
}
5151

5252
logger.error("At last an error.", new Exception("Just testing"));
53-
53+
54+
lc.stop();
55+
5456
StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
5557
}
5658

0 commit comments

Comments
 (0)