Skip to content

Commit

Permalink
Make value generation locale independent.
Browse files Browse the repository at this point in the history
  • Loading branch information
yevgenp committed Jun 19, 2024
1 parent 55c130b commit ef2b962
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.squareup.javapoet.MethodSpec;

import java.util.Locale;

import static net.openhft.chronicle.values.Primitives.boxed;
import static net.openhft.chronicle.values.Utils.capitalize;

Expand All @@ -34,15 +36,15 @@ class PrimitiveBackedHeapMemberGenerator extends HeapMemberGenerator {
fieldType = determineFieldType();
assert fieldType.isPrimitive();
capType = capitalize(fieldType.getName());
upperType = fieldType.getName().toUpperCase();
upperType = fieldType.getName().toUpperCase(Locale.ROOT);
}

PrimitiveBackedHeapMemberGenerator(FieldModel fieldModel, Class<?> fieldType) {
super(fieldModel);
this.fieldType = fieldType;
assert fieldType.isPrimitive();
capType = capitalize(fieldType.getName());
upperType = fieldType.getName().toUpperCase();
upperType = fieldType.getName().toUpperCase(Locale.ROOT);
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/openhft/chronicle/values/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package net.openhft.chronicle.values;

import java.util.Locale;

final class Utils {
static final Class<?> UNSAFE_CLASS;

Expand All @@ -37,7 +39,7 @@ public static int roundUp(int divident, int divisor) {
}

static String capitalize(String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1);
return s.substring(0, 1).toUpperCase(Locale.ROOT) + s.substring(1);
}

static String formatIntOrLong(long v) {
Expand Down
90 changes: 49 additions & 41 deletions src/test/java/net/openhft/chronicle/values/ValueGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.Locale;

import static net.openhft.chronicle.values.Generators.generateHeapClass;
import static net.openhft.chronicle.values.Generators.generateNativeClass;
import static net.openhft.chronicle.values.Values.newHeapInstance;
import static net.openhft.chronicle.values.Values.newNativeReference;
import static org.junit.Assert.*;
import static net.openhft.compiler.CompilerUtils.CACHED_COMPILER;
import static org.junit.Assert.*;

/**
* User: peter.lawrey Date: 06/10/13 Time: 20:13
Expand Down Expand Up @@ -64,42 +65,49 @@ public void testGenerateJavaCode() {

@Test
public void testGenerateJavaCode2() {
MinimalInterface mi = newHeapInstance(MinimalInterface.class);

mi.byte$((byte) 1);
mi.char$('2');
mi.short$((short) 3);
mi.int$(4);
mi.float$(5);
mi.long$(6);
mi.double$(7);
mi.flag(true);

assertEquals(1, mi.byte$());
assertEquals('2', mi.char$());
assertEquals(3, mi.short$());
assertEquals(4, mi.int$());
assertEquals(5.0, mi.float$(), 0);
assertEquals(6, mi.long$());
assertEquals(7.0, mi.double$(), 0.0);
assertTrue(mi.flag());

Bytes<ByteBuffer> bbb = Bytes.wrapForWrite(ByteBuffer.allocate(64));
mi.writeMarshallable(bbb);
System.out.println("size: " + bbb.writePosition());

MinimalInterface mi2 = newHeapInstance(MinimalInterface.class);
bbb.readPosition(0);
mi2.readMarshallable(bbb);

assertEquals(1, mi2.byte$());
assertEquals('2', mi2.char$());
assertEquals(3, mi2.short$());
assertEquals(4, mi2.int$());
assertEquals(5.0, mi2.float$(), 0);
assertEquals(6, mi2.long$());
assertEquals(7.0, mi2.double$(), 0.0);
assertTrue(mi2.flag());
Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.forLanguageTag("tr"));
try {
MinimalInterface mi = newHeapInstance(MinimalInterface.class);

mi.byte$((byte) 1);
mi.char$('2');
mi.short$((short) 3);
mi.int$(4);
mi.float$(5);
mi.long$(6);
mi.double$(7);
mi.flag(true);

assertEquals(1, mi.byte$());
assertEquals('2', mi.char$());
assertEquals(3, mi.short$());
assertEquals(4, mi.int$());
assertEquals(5.0, mi.float$(), 0);
assertEquals(6, mi.long$());
assertEquals(7.0, mi.double$(), 0.0);
assertTrue(mi.flag());

Bytes<ByteBuffer> bbb = Bytes.wrapForWrite(ByteBuffer.allocate(64));
mi.writeMarshallable(bbb);
System.out.println("size: " + bbb.writePosition());

MinimalInterface mi2 = newHeapInstance(MinimalInterface.class);
bbb.readPosition(0);
mi2.readMarshallable(bbb);

assertEquals(1, mi2.byte$());
assertEquals('2', mi2.char$());
assertEquals(3, mi2.short$());
assertEquals(4, mi2.int$());
assertEquals(5.0, mi2.float$(), 0);
assertEquals(6, mi2.long$());
assertEquals(7.0, mi2.double$(), 0.0);
assertTrue(mi2.flag());
}
finally {
Locale.setDefault(defaultLocale);
}
}

@SuppressWarnings("rawtypes")
Expand All @@ -111,7 +119,7 @@ public void testGenerateNativeWithGetUsing() throws ClassNotFoundException, Ille
Class<?> aClass = CACHED_COMPILER.loadFromJava(
BytecodeGen.getClassLoader(JavaBeanInterfaceGetUsing.class),
JavaBeanInterfaceGetUsing.class.getName() + "$$Native", actual);
JavaBeanInterfaceGetUsing jbi = (JavaBeanInterfaceGetUsing) aClass.asSubclass(JavaBeanInterfaceGetUsing.class).getDeclaredConstructor().newInstance();
JavaBeanInterfaceGetUsing jbi = aClass.asSubclass(JavaBeanInterfaceGetUsing.class).getDeclaredConstructor().newInstance();
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate(64));
((Byteable) jbi).bytesStore(bytes, 0L, ((Byteable) jbi).maxSize());

Expand Down Expand Up @@ -207,7 +215,7 @@ private <T> T loadNativeTypeAndCreateValue(Class<T> type) throws InstantiationEx
Class<?> aClass = Values.nativeClassFor(type);
T jbi;
try {
jbi = (T) aClass.asSubclass(type).getConstructor().newInstance();
jbi = aClass.asSubclass(type).getConstructor().newInstance();
} catch (NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
Expand All @@ -221,7 +229,7 @@ private <T> T loadHeapTypeAndCreateValue(Class<T> type) throws InstantiationExce
ValueModel.simpleName(type) + "$$Heap");
System.out.println(actual);
Class<T> aClass = Values.heapClassFor(type);
T jbi = (T) aClass.asSubclass(type).getDeclaredConstructor().newInstance();
T jbi = aClass.asSubclass(type).getDeclaredConstructor().newInstance();
return jbi;
}

Expand Down Expand Up @@ -327,7 +335,7 @@ public void testGenerateInterfaceWithDateOnHeap() {
}

@Test
public void testGenerateInterfaceWithDateNativeInstace() {
public void testGenerateInterfaceWithDateNativeInstance() {
//dvg.setDumpCode(true);
JavaBeanInterfaceGetDate jbid = newNativeReference(JavaBeanInterfaceGetDate.class);
BytesStore<?, ByteBuffer> bytes = BytesStore.wrap(ByteBuffer.allocate(64));
Expand Down

0 comments on commit ef2b962

Please sign in to comment.