-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #781 from dmlloyd/config-prim
[#390] Support some more primitive types
- Loading branch information
Showing
5 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...eployment/src/main/java/org/jboss/shamrock/deployment/configuration/DoubleConfigType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
...deployment/src/main/java/org/jboss/shamrock/deployment/configuration/FloatConfigType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
.../deployment/src/main/java/org/jboss/shamrock/deployment/configuration/LongConfigType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |