Skip to content

Commit 1ebe0a8

Browse files
author
Vladimir Kotal
committed
introduce BeanBuilder
1 parent 3a83964 commit 1ebe0a8

File tree

2 files changed

+82
-29
lines changed

2 files changed

+82
-29
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryInfo.java

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
import java.util.logging.Level;
3232
import java.util.logging.Logger;
3333

34-
import net.sf.cglib.beans.BeanGenerator;
3534
import org.opengrok.indexer.configuration.Project;
3635
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3736
import org.opengrok.indexer.logger.LoggerFactory;
37+
import org.opengrok.indexer.util.BeanBuilder;
3838
import org.opengrok.indexer.util.ClassUtil;
3939
import org.opengrok.indexer.util.PathUtils;
4040

@@ -97,34 +97,19 @@ public Object getRepositoryInfoData() {
9797
}
9898

9999
private Object createRepositoryInfoTO() {
100-
BeanGenerator beanGenerator = new BeanGenerator();
101-
beanGenerator.addProperty("type", String.class);
102-
beanGenerator.addProperty("directoryNameRelative", String.class);
103-
beanGenerator.addProperty("remote", boolean.class);
104-
beanGenerator.addProperty("parent", String.class);
105-
beanGenerator.addProperty("branch", String.class);
106-
beanGenerator.addProperty("currentVersion", String.class);
107-
beanGenerator.addProperty("working", Boolean.class);
108-
beanGenerator.addProperty("handleRenamedFiles", boolean.class);
109-
beanGenerator.addProperty("historyEnabled", boolean.class);
110-
111-
Object myBean = beanGenerator.create();
112-
try {
113-
ClassUtil.setFieldValue(myBean, "type", this.type);
114-
ClassUtil.setFieldValue(myBean, "working",
115-
this.working == null ? false : this.working);
116-
ClassUtil.setFieldValue(myBean, "directoryNameRelative", this.directoryNameRelative);
117-
ClassUtil.setFieldValue(myBean, "remote", this.remote);
118-
ClassUtil.setFieldValue(myBean, "parent", this.parent);
119-
ClassUtil.setFieldValue(myBean, "branch", this.branch);
120-
ClassUtil.setFieldValue(myBean, "currentVersion", this.currentVersion);
121-
ClassUtil.setFieldValue(myBean, "handleRenamedFiles", this.handleRenamedFiles);
122-
ClassUtil.setFieldValue(myBean, "historyEnabled", this.historyEnabled);
123-
} catch (IOException e) {
124-
LOGGER.log(Level.WARNING, "cannot generate RepositoryInfo bean", e);
125-
return null;
126-
}
127-
return myBean;
100+
BeanBuilder builder = new BeanBuilder();
101+
102+
builder.add("type", String.class, this.type)
103+
.add("directoryNameRelative", String.class, this.directoryNameRelative)
104+
.add("remote", boolean.class, this.remote)
105+
.add("parent", String.class, this.parent)
106+
.add("branch", String.class, this.branch)
107+
.add("currentVersion", String.class, this.currentVersion)
108+
.add("working", Boolean.class, this.working == null ? false : this.working)
109+
.add("handleRenamedFiles", boolean.class, this.handleRenamedFiles)
110+
.add("historyEnabled", boolean.class, this.historyEnabled);
111+
112+
return builder.build();
128113
}
129114

130115
/**
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
24+
package org.opengrok.indexer.util;
25+
26+
import net.sf.cglib.beans.BeanGenerator;
27+
import org.opengrok.indexer.logger.LoggerFactory;
28+
29+
import java.io.IOException;
30+
import java.util.HashMap;
31+
import java.util.Map;
32+
import java.util.logging.Level;
33+
import java.util.logging.Logger;
34+
35+
public class BeanBuilder {
36+
private BeanGenerator beanGenerator;
37+
private Map<String, Object> valueMap;
38+
39+
private static final Logger LOGGER =
40+
LoggerFactory.getLogger(BeanBuilder.class);
41+
42+
public BeanBuilder() {
43+
beanGenerator = new BeanGenerator();
44+
valueMap = new HashMap<>();
45+
}
46+
47+
public BeanBuilder add(String name, Class<?> type, Object value) {
48+
beanGenerator.addProperty(name, type);
49+
valueMap.put(name, value);
50+
51+
return this;
52+
}
53+
54+
public Object build() {
55+
Object myBean = beanGenerator.create();
56+
57+
for (String name : valueMap.keySet()) {
58+
try {
59+
ClassUtil.setFieldValue(myBean, name, valueMap.get(name));
60+
} catch (IOException e) {
61+
LOGGER.log(Level.WARNING, "cannot generate RepositoryInfo bean", e);
62+
return null;
63+
}
64+
}
65+
66+
return myBean;
67+
}
68+
}

0 commit comments

Comments
 (0)