net.oneandone.stool.configuration.ReflectAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of main Show documentation
Show all versions of main Show documentation
Stool's main component. Java Library, cli, setup code.
The newest version!
/*
* Copyright 1&1 Internet AG, https://github.com/1and1/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.oneandone.stool.configuration;
import net.oneandone.inline.ArgumentException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** Handles Stool or Stage property. Converts between strings an objects and deals with reflection */
public class ReflectAccessor extends Accessor {
private final Field field;
public ReflectAccessor(String name, Field field) {
super(name);
this.field = field;
field.setAccessible(true);
}
protected String doGet(Object configuration) {
Object obj;
StringBuilder builder;
boolean first;
try {
obj = field.get(configuration);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
}
if (obj instanceof List) {
builder = new StringBuilder();
first = true;
for (Object item : (List) obj) {
if (first) {
first = false;
} else {
builder.append(", ");
}
builder.append(item.toString());
}
return builder.toString();
} else if (obj instanceof Map) {
builder = new StringBuilder();
first = true;
for (Map.Entry