use of java.lang.reflect.Parameter in project configuration-as-code-plugin by jenkinsci.
the class DataBoundConfigurator method configure.
@Override
public Object configure(Object c) throws Exception {
Map config = c instanceof Map ? (Map) c : Collections.EMPTY_MAP;
final Constructor constructor = getDataBoundConstructor(target);
if (constructor == null) {
throw new IllegalStateException(target.getName() + " is missing a @DataBoundConstructor");
}
final Parameter[] parameters = constructor.getParameters();
final String[] names = ClassDescriptor.loadParameterNames(constructor);
Object[] args = new Object[names.length];
if (parameters.length > 0) {
for (int i = 0; i < names.length; i++) {
final Object value = config.remove(names[i]);
if (value == null && parameters[i].getAnnotation(Nonnull.class) != null) {
throw new IllegalArgumentException(names[i] + " is required to configure " + target);
}
final Class t = parameters[i].getType();
if (value != null) {
if (Collection.class.isAssignableFrom(t)) {
if (!(value instanceof List)) {
throw new IllegalArgumentException(names[i] + " should be a list");
}
final Type pt = parameters[i].getParameterizedType();
final Configurator lookup = Configurator.lookup(pt);
final ArrayList<Object> list = new ArrayList<>();
for (Object o : (List) value) {
list.add(lookup.configure(o));
}
args[i] = list;
} else {
final Type pt = parameters[i].getParameterizedType();
final Type k = pt != null ? pt : t;
final Configurator configurator = Configurator.lookup(k);
if (configurator == null)
throw new IllegalStateException("No configurator implementation to manage " + k);
args[i] = configurator.configure(value);
}
logger.info("Setting " + target + "." + names[i] + " = " + value);
} else if (t.isPrimitive()) {
args[i] = Defaults.defaultValue(t);
}
}
}
final Object object;
try {
object = constructor.newInstance(args);
} catch (IllegalArgumentException ex) {
List<String> argumentTypes = new ArrayList<>(args.length);
for (Object arg : args) {
argumentTypes.add(arg != null ? arg.getClass().getName() : "null");
}
throw new IOException("Failed to construct instance of " + target + ". Constructor: " + constructor.toString() + ". Arguments: " + argumentTypes, ex);
}
final Set<Attribute> attributes = describe();
for (Attribute attribute : attributes) {
final String name = attribute.getName();
final Configurator lookup = Configurator.lookup(attribute.getType());
if (config.containsKey(name)) {
final Object yaml = config.get(name);
Object value;
if (attribute.isMultiple()) {
List l = new ArrayList<>();
for (Object o : (List) yaml) {
l.add(lookup.configure(o));
}
value = l;
} else {
value = lookup.configure(config.get(name));
}
attribute.setValue(object, value);
}
}
for (Method method : target.getMethods()) {
if (method.getParameterCount() == 0 && method.getAnnotation(PostConstruct.class) != null) {
method.invoke(object, null);
}
}
return object;
}
use of java.lang.reflect.Parameter in project configuration-as-code-plugin by jenkinsci.
the class DataBoundConfigurator method describe.
@Override
public Set<Attribute> describe() {
final Set<Attribute> attributes = super.describe();
final Constructor constructor = getDataBoundConstructor(target);
if (constructor != null) {
final Parameter[] parameters = constructor.getParameters();
final String[] names = ClassDescriptor.loadParameterNames(constructor);
for (int i = 0; i < parameters.length; i++) {
final Parameter p = parameters[i];
final Attribute a = detectActualType(names[i], p.getParameterizedType());
if (a == null)
continue;
attributes.add(a);
}
}
return attributes;
}
use of java.lang.reflect.Parameter in project NoraUi by NoraUi.
the class Step method runAllStepsInLoop.
/**
* Runs a bunch of steps for a Gherkin loop.
*
* @param loopedSteps
* GherkinConditionedLoopedStep steps to run
* @throws TechnicalException
* is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi.
*/
protected void runAllStepsInLoop(List<GherkinConditionedLoopedStep> loopedSteps) throws TechnicalException {
for (final GherkinConditionedLoopedStep loopedStep : loopedSteps) {
final List<GherkinStepCondition> stepConditions = new ArrayList<>();
final String[] expecteds = loopedStep.getExpected().split(";");
final String[] actuals = loopedStep.getActual().split(";");
if (actuals.length != expecteds.length) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_EXPECTED_ACTUAL_SIZE_DIFFERENT));
}
for (int i = 0; i < expecteds.length; i++) {
stepConditions.add(new GherkinStepCondition(loopedStep.getKey(), expecteds[i], actuals[i]));
}
boolean found = false;
for (final Entry<String, Method> elem : Context.getCucumberMethods().entrySet()) {
final Matcher matcher = Pattern.compile("value=(.*)\\)").matcher(elem.getKey());
if (matcher.find()) {
final Matcher matcher2 = Pattern.compile(matcher.group(1)).matcher(loopedStep.getStep());
if (matcher2.find()) {
Object[] tab;
if (elem.getValue().isAnnotationPresent(Conditioned.class)) {
tab = new Object[matcher2.groupCount() + 1];
tab[matcher2.groupCount()] = stepConditions;
} else {
tab = new Object[matcher2.groupCount()];
}
for (int i = 0; i < matcher2.groupCount(); i++) {
final Parameter param = elem.getValue().getParameters()[i];
if (param.getType() == int.class) {
final int ii = Integer.parseInt(matcher2.group(i + 1));
tab[i] = ii;
} else if (param.getType() == boolean.class) {
tab[i] = Boolean.parseBoolean(matcher2.group(i + 1));
} else {
tab[i] = matcher2.group(i + 1);
}
}
try {
found = true;
elem.getValue().invoke(NoraUiInjector.getNoraUiInjectorSource().getInstance(elem.getValue().getDeclaringClass()), tab);
break;
} catch (final Exception e) {
throw new TechnicalException("\"" + loopedStep.getStep() + "\"", e.getCause());
}
}
}
}
if (!found) {
throw new TechnicalException(String.format(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_STEP_UNDEFINED), loopedStep.getStep()));
}
}
}
use of java.lang.reflect.Parameter in project SilverKing by Morgan-Stanley.
the class JNIUtil method getReferencedClasses.
public static <E extends Executable> Set<Class> getReferencedClasses(E e) {
Set<Class> s;
s = new HashSet<>();
if (!omitted(e)) {
if (debugReferencedClasses) {
System.out.printf("\tgetReferencedClassesE %s\n", e.getName());
}
if (e instanceof Method) {
if (isReferencedClass(((Method) e).getReturnType())) {
if (debugReferencedClasses) {
System.out.printf("\tAdding return type %s\n", ((Method) e).getReturnType());
}
s.add(((Method) e).getReturnType());
} else {
if (debugReferencedClasses) {
System.out.printf("Not referenced class1 %s %s\n", e.getName(), ((Method) e).getReturnType());
}
}
}
for (Parameter p : e.getParameters()) {
if (omitted(p.getType())) {
}
if (isReferencedClass(p.getType())) {
if (debugReferencedClasses) {
System.out.printf("\tAdding parameter %s\n", p.getType());
}
s.add(p.getType());
} else {
if (debugReferencedClasses) {
System.out.printf("Not referenced class2 %s %s\n", e.getName(), p.getType());
}
}
}
} else {
if (debugReferencedClasses) {
System.out.printf("Omitted %s\n", e.getName());
}
}
return s;
}
use of java.lang.reflect.Parameter in project SilverKing by Morgan-Stanley.
the class JNIUtil method getJNISignature.
public static String getJNISignature(Constructor c) {
StringBuffer sb;
sb = new StringBuffer();
sb.append('(');
for (Parameter p : c.getParameters()) {
sb.append(typeString(p.getType()));
}
sb.append(")V");
return sb.toString();
}
Aggregations