Skip to content

Commit

Permalink
Merge pull request #781 from dmlloyd/config-prim
Browse files Browse the repository at this point in the history
[#390] Support some more primitive types
  • Loading branch information
dmlloyd authored Feb 6, 2019
2 parents 4c0601b + 75742a1 commit a47bf27
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ private GroupConfigType processConfigGroup(final String containingName, final Co
gct.addField(leaf = new BooleanConfigType(field.getName(), gct, consume, defaultValue.equals(ConfigItem.NO_DEFAULT) ? "false" : defaultValue));
} else if (fieldClass == int.class) {
gct.addField(leaf = new IntConfigType(field.getName(), gct, consume, defaultValue.equals(ConfigItem.NO_DEFAULT) ? "0" : defaultValue));
} else if (fieldClass == long.class) {
gct.addField(leaf = new LongConfigType(field.getName(), gct, consume, defaultValue.equals(ConfigItem.NO_DEFAULT) ? "0" : defaultValue));
} else if (fieldClass == double.class) {
gct.addField(leaf = new DoubleConfigType(field.getName(), gct, consume, defaultValue.equals(ConfigItem.NO_DEFAULT) ? "0" : defaultValue));
} else if (fieldClass == float.class) {
gct.addField(leaf = new FloatConfigType(field.getName(), gct, consume, defaultValue.equals(ConfigItem.NO_DEFAULT) ? "0" : defaultValue));
} else {
throw reportError(field, "Unsupported primitive field type");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.jboss.shamrock.deployment.configuration;

import java.lang.reflect.Field;
import java.util.OptionalDouble;

import io.smallrye.config.SmallRyeConfig;
import org.jboss.protean.gizmo.BytecodeCreator;
import org.jboss.protean.gizmo.MethodDescriptor;
import org.jboss.protean.gizmo.ResultHandle;
import org.jboss.shamrock.deployment.AccessorFinder;
import org.jboss.shamrock.runtime.configuration.NameIterator;
import org.wildfly.common.Assert;

/**
*/
public class DoubleConfigType extends LeafConfigType {

private static final MethodDescriptor OPTDOUBLE_OR_ELSE_METHOD = MethodDescriptor.ofMethod(OptionalDouble.class, "orElse", double.class, double.class);
private static final MethodDescriptor DOUBLE_VALUE_METHOD = MethodDescriptor.ofMethod(Double.class, "doubleValue", double.class);

final String defaultValue;

public DoubleConfigType(final String containingName, final CompoundConfigType container, final boolean consumeSegment, final String defaultValue) {
super(containingName, container, consumeSegment);
Assert.checkNotEmptyParam("defaultValue", defaultValue);
this.defaultValue = defaultValue;
}

public void acceptConfigurationValue(final NameIterator name, final SmallRyeConfig config) {
final GroupConfigType container = getContainer(GroupConfigType.class);
if (isConsumeSegment()) name.previous();
container.acceptConfigurationValueIntoLeaf(this, name, config);
// the iterator is not used after this point
// if (isConsumeSegment()) name.next();
}

public void generateAcceptConfigurationValue(final BytecodeCreator body, final ResultHandle name, final ResultHandle config) {
final GroupConfigType container = getContainer(GroupConfigType.class);
if (isConsumeSegment()) body.invokeVirtualMethod(NI_PREV_METHOD, name);
container.generateAcceptConfigurationValueIntoLeaf(body, this, name, config);
// the iterator is not used after this point
// if (isConsumeSegment()) body.invokeVirtualMethod(NI_NEXT_METHOD, name);
}

public void acceptConfigurationValueIntoGroup(final Object enclosing, final Field field, final NameIterator name, final SmallRyeConfig config) {
try {
field.setDouble(enclosing, config.getValue(name.toString(), OptionalDouble.class).orElse(config.convert(defaultValue, Double.class).doubleValue()));
} catch (IllegalAccessException e) {
throw toError(e);
}
}

public void generateAcceptConfigurationValueIntoGroup(final BytecodeCreator body, final ResultHandle enclosing, final MethodDescriptor setter, final ResultHandle name, final ResultHandle config) {
// config.getValue(name.toString(), OptionalDouble.class).orElse(config.convert(defaultValue, Double.class).doubleValue())
final ResultHandle optionalValue = body.checkCast(body.invokeVirtualMethod(
SRC_GET_VALUE,
config,
body.invokeVirtualMethod(
OBJ_TO_STRING_METHOD,
name
),
body.loadClass(OptionalDouble.class)
), OptionalDouble.class);
final ResultHandle convertedDefault = getConvertedDefault(body, config);
final ResultHandle defaultedValue = body.checkCast(body.invokeVirtualMethod(
OPTDOUBLE_OR_ELSE_METHOD,
optionalValue,
convertedDefault
), Double.class);
final ResultHandle doubleValue = body.invokeVirtualMethod(DOUBLE_VALUE_METHOD, defaultedValue);
body.invokeStaticMethod(setter, enclosing, doubleValue);
}

public Class<?> getItemClass() {
return double.class;
}

void getDefaultValueIntoEnclosingGroup(final Object enclosing, final SmallRyeConfig config, final Field field) {
try {
field.setDouble(enclosing, config.convert(defaultValue, Double.class).doubleValue());
} catch (IllegalAccessException e) {
throw toError(e);
}
}

void generateGetDefaultValueIntoEnclosingGroup(final BytecodeCreator body, final ResultHandle enclosing, final MethodDescriptor setter, final ResultHandle config) {
body.invokeStaticMethod(setter, enclosing, body.invokeVirtualMethod(DOUBLE_VALUE_METHOD, getConvertedDefault(body, config)));
}

public ResultHandle writeInitialization(final BytecodeCreator body, final AccessorFinder accessorFinder, final ResultHandle smallRyeConfig) {
return body.invokeVirtualMethod(DOUBLE_VALUE_METHOD, getConvertedDefault(body, smallRyeConfig));
}

private ResultHandle getConvertedDefault(final BytecodeCreator body, final ResultHandle config) {
return body.checkCast(body.invokeVirtualMethod(
SRC_CONVERT_METHOD,
config,
body.load(defaultValue),
body.loadClass(Double.class)
), Double.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.jboss.shamrock.deployment.configuration;

import java.lang.reflect.Field;

import io.smallrye.config.SmallRyeConfig;
import org.jboss.protean.gizmo.AssignableResultHandle;
import org.jboss.protean.gizmo.BranchResult;
import org.jboss.protean.gizmo.BytecodeCreator;
import org.jboss.protean.gizmo.MethodDescriptor;
import org.jboss.protean.gizmo.ResultHandle;
import org.jboss.shamrock.deployment.AccessorFinder;
import org.jboss.shamrock.runtime.configuration.NameIterator;
import org.wildfly.common.Assert;

/**
*/
public class FloatConfigType extends LeafConfigType {

private static final MethodDescriptor FLOAT_VALUE_METHOD = MethodDescriptor.ofMethod(Float.class, "floatValue", float.class);

final String defaultValue;

public FloatConfigType(final String containingName, final CompoundConfigType container, final boolean consumeSegment, final String defaultValue) {
super(containingName, container, consumeSegment);
Assert.checkNotEmptyParam("defaultValue", defaultValue);
this.defaultValue = defaultValue;
}

public void acceptConfigurationValue(final NameIterator name, final SmallRyeConfig config) {
final GroupConfigType container = getContainer(GroupConfigType.class);
if (isConsumeSegment()) name.previous();
container.acceptConfigurationValueIntoLeaf(this, name, config);
// the iterator is not used after this point
// if (isConsumeSegment()) name.next();
}

public void generateAcceptConfigurationValue(final BytecodeCreator body, final ResultHandle name, final ResultHandle config) {
final GroupConfigType container = getContainer(GroupConfigType.class);
if (isConsumeSegment()) body.invokeVirtualMethod(NI_PREV_METHOD, name);
container.generateAcceptConfigurationValueIntoLeaf(body, this, name, config);
// the iterator is not used after this point
// if (isConsumeSegment()) body.invokeVirtualMethod(NI_NEXT_METHOD, name);
}

public void acceptConfigurationValueIntoGroup(final Object enclosing, final Field field, final NameIterator name, final SmallRyeConfig config) {
try {
final Float floatValue = config.getValue(name.toString(), Float.class);
final float f = floatValue != null ? floatValue.floatValue() : config.convert(defaultValue, Float.class).floatValue();
field.setFloat(enclosing, f);
} catch (IllegalAccessException e) {
throw toError(e);
}
}

public void generateAcceptConfigurationValueIntoGroup(final BytecodeCreator body, final ResultHandle enclosing, final MethodDescriptor setter, final ResultHandle name, final ResultHandle config) {
// final Float floatValue = config.getValue(name.toString(), Float.class);
// final float f = floatValue != null ? floatValue.floatValue() : config.convert(defaultValue, Float.class).floatValue();
final AssignableResultHandle result = body.createVariable(float.class);
final ResultHandle floatValue = body.checkCast(body.invokeVirtualMethod(
SRC_GET_VALUE,
config,
body.invokeVirtualMethod(
OBJ_TO_STRING_METHOD,
name
),
body.loadClass(Float.class)
), Float.class);
final BranchResult ifNull = body.ifNull(floatValue);
final BytecodeCreator isNull = ifNull.trueBranch();
isNull.assign(result,
isNull.checkCast(isNull.invokeVirtualMethod(
FLOAT_VALUE_METHOD,
floatValue,
getConvertedDefault(isNull, config)
),
Float.class)
);
final BytecodeCreator isNotNull = ifNull.falseBranch();
isNotNull.assign(result,
isNotNull.invokeVirtualMethod(
FLOAT_VALUE_METHOD,
floatValue
)
);
body.invokeStaticMethod(setter, enclosing, result);
}

public Class<?> getItemClass() {
return float.class;
}

void getDefaultValueIntoEnclosingGroup(final Object enclosing, final SmallRyeConfig config, final Field field) {
try {
field.setFloat(enclosing, config.convert(defaultValue, Float.class).floatValue());
} catch (IllegalAccessException e) {
throw toError(e);
}
}

void generateGetDefaultValueIntoEnclosingGroup(final BytecodeCreator body, final ResultHandle enclosing, final MethodDescriptor setter, final ResultHandle config) {
body.invokeStaticMethod(setter, enclosing, body.invokeVirtualMethod(FLOAT_VALUE_METHOD, getConvertedDefault(body, config)));
}

public ResultHandle writeInitialization(final BytecodeCreator body, final AccessorFinder accessorFinder, final ResultHandle smallRyeConfig) {
return body.invokeVirtualMethod(FLOAT_VALUE_METHOD, getConvertedDefault(body, smallRyeConfig));
}

private ResultHandle getConvertedDefault(final BytecodeCreator body, final ResultHandle config) {
return body.checkCast(body.invokeVirtualMethod(
SRC_CONVERT_METHOD,
config,
body.load(defaultValue),
body.loadClass(Float.class)
), Float.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.jboss.protean.gizmo.ResultHandle;
import org.jboss.shamrock.deployment.AccessorFinder;
import org.jboss.shamrock.runtime.configuration.NameIterator;
import org.wildfly.common.Assert;

/**
*/
Expand All @@ -21,6 +22,7 @@ public class IntConfigType extends LeafConfigType {

public IntConfigType(final String containingName, final CompoundConfigType container, final boolean consumeSegment, final String defaultValue) {
super(containingName, container, consumeSegment);
Assert.checkNotEmptyParam("defaultValue", defaultValue);
this.defaultValue = defaultValue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.jboss.shamrock.deployment.configuration;

import java.lang.reflect.Field;
import java.util.OptionalLong;

import io.smallrye.config.SmallRyeConfig;
import org.jboss.protean.gizmo.BytecodeCreator;
import org.jboss.protean.gizmo.MethodDescriptor;
import org.jboss.protean.gizmo.ResultHandle;
import org.jboss.shamrock.deployment.AccessorFinder;
import org.jboss.shamrock.runtime.configuration.NameIterator;
import org.wildfly.common.Assert;

/**
*/
public class LongConfigType extends LeafConfigType {

private static final MethodDescriptor OPTLONG_OR_ELSE_METHOD = MethodDescriptor.ofMethod(OptionalLong.class, "orElse", long.class, long.class);
private static final MethodDescriptor LONG_VALUE_METHOD = MethodDescriptor.ofMethod(Long.class, "longValue", long.class);

final String defaultValue;

public LongConfigType(final String containingName, final CompoundConfigType container, final boolean consumeSegment, final String defaultValue) {
super(containingName, container, consumeSegment);
Assert.checkNotEmptyParam("defaultValue", defaultValue);
this.defaultValue = defaultValue;
}

public void acceptConfigurationValue(final NameIterator name, final SmallRyeConfig config) {
final GroupConfigType container = getContainer(GroupConfigType.class);
if (isConsumeSegment()) name.previous();
container.acceptConfigurationValueIntoLeaf(this, name, config);
// the iterator is not used after this point
// if (isConsumeSegment()) name.next();
}

public void generateAcceptConfigurationValue(final BytecodeCreator body, final ResultHandle name, final ResultHandle config) {
final GroupConfigType container = getContainer(GroupConfigType.class);
if (isConsumeSegment()) body.invokeVirtualMethod(NI_PREV_METHOD, name);
container.generateAcceptConfigurationValueIntoLeaf(body, this, name, config);
// the iterator is not used after this point
// if (isConsumeSegment()) body.invokeVirtualMethod(NI_NEXT_METHOD, name);
}

public void acceptConfigurationValueIntoGroup(final Object enclosing, final Field field, final NameIterator name, final SmallRyeConfig config) {
try {
field.setLong(enclosing, config.getValue(name.toString(), OptionalLong.class).orElse(config.convert(defaultValue, Long.class).longValue()));
} catch (IllegalAccessException e) {
throw toError(e);
}
}

public void generateAcceptConfigurationValueIntoGroup(final BytecodeCreator body, final ResultHandle enclosing, final MethodDescriptor setter, final ResultHandle name, final ResultHandle config) {
// config.getValue(name.toString(), OptionalLong.class).orElse(config.convert(defaultValue, Long.class).longValue())
final ResultHandle optionalValue = body.checkCast(body.invokeVirtualMethod(
SRC_GET_VALUE,
config,
body.invokeVirtualMethod(
OBJ_TO_STRING_METHOD,
name
),
body.loadClass(OptionalLong.class)
), OptionalLong.class);
final ResultHandle convertedDefault = getConvertedDefault(body, config);
final ResultHandle defaultedValue = body.checkCast(body.invokeVirtualMethod(
OPTLONG_OR_ELSE_METHOD,
optionalValue,
convertedDefault
), Long.class);
final ResultHandle longValue = body.invokeVirtualMethod(LONG_VALUE_METHOD, defaultedValue);
body.invokeStaticMethod(setter, enclosing, longValue);
}

public Class<?> getItemClass() {
return long.class;
}

void getDefaultValueIntoEnclosingGroup(final Object enclosing, final SmallRyeConfig config, final Field field) {
try {
field.setLong(enclosing, config.convert(defaultValue, Long.class).longValue());
} catch (IllegalAccessException e) {
throw toError(e);
}
}

void generateGetDefaultValueIntoEnclosingGroup(final BytecodeCreator body, final ResultHandle enclosing, final MethodDescriptor setter, final ResultHandle config) {
body.invokeStaticMethod(setter, enclosing, body.invokeVirtualMethod(LONG_VALUE_METHOD, getConvertedDefault(body, config)));
}

public ResultHandle writeInitialization(final BytecodeCreator body, final AccessorFinder accessorFinder, final ResultHandle smallRyeConfig) {
return body.invokeVirtualMethod(LONG_VALUE_METHOD, getConvertedDefault(body, smallRyeConfig));
}

private ResultHandle getConvertedDefault(final BytecodeCreator body, final ResultHandle config) {
return body.checkCast(body.invokeVirtualMethod(
SRC_CONVERT_METHOD,
config,
body.load(defaultValue),
body.loadClass(Long.class)
), Long.class);
}
}

0 comments on commit a47bf27

Please sign in to comment.