Skip to content

Commit

Permalink
Add clang-format to the Java runtime project
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Jan 5, 2024
1 parent d3cf6c2 commit 4bdaaf3
Show file tree
Hide file tree
Showing 87 changed files with 1,982 additions and 2,056 deletions.
36 changes: 34 additions & 2 deletions compiler/extensions/java/runtime/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ relational.sqlite.jar_file - SQLite JDBC jar file.
Default is "../../../../3rdparty/java/sqlitejdbc-3.39.4.jar".
relational.validation - Flag whether the support for low level validation is included.
Default is "yes".
clang_format.exe_file - Clang-format executable file. If not set, clang-format is not run.
spotbugs.home_dir - Location of the spotbugs tool. If not set, spotbugs is not run.
-->
Expand Down Expand Up @@ -208,7 +209,22 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spo
overwrite="true"/>
</target>

<target name="spotbugs" depends="install" if="spotbugs.home_dir">
<target name="clang_format" depends="install" if="clang_format.exe_file">
<echo>
Command hints to reformat source using clang-format tool:
git clang-format
clang-format --style=file -i [SOURCE]
</echo>
<apply executable="${clang_format.exe_file}" failonerror="true">
<arg value="--style=file"/>
<arg value="--dry-run"/>
<arg value="--Werror"/>
<srcfile/>
<fileset dir="${zserio_java_runtime.src_dir}" includes="**/*.java"/>
</apply>
</target>

<target name="spotbugs" depends="clang_format" if="spotbugs.home_dir">
<mkdir dir="${zserio_java_runtime.spotbugs.out_dir}"/>
<taskdef classpath="${spotbugs.home_dir}/lib/spotbugs-ant.jar"
resource="edu/umd/cs/findbugs/anttask/tasks.properties"/>
Expand Down Expand Up @@ -263,7 +279,23 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spo
</javac>
</target>

<target name="test.spotbugs" depends="spotbugs, test.compile" if="spotbugs.home_dir">
<target name="test.clang_format" depends="spotbugs, test.compile" if="clang_format.exe_file">
<echo>
Command hints to reformat source using clang-format tool:
git clang-format
clang-format --style=file -i [SOURCE]
</echo>
<apply executable="${clang_format.exe_file}" failonerror="true">
<arg value="--style=file"/>
<arg value="--dry-run"/>
<arg value="--Werror"/>
<srcfile/>
<fileset dir="${zserio_java_runtime.test.src_dir}" includes="**/*.java"
excludes="test_object/*.java"/>
</apply>
</target>

<target name="test.spotbugs" depends="test.clang_format" if="spotbugs.home_dir">
<mkdir dir="${zserio_java_runtime.test.spotbugs.out_dir}"/>
<taskdef classpath="${spotbugs.home_dir}/lib/spotbugs-ant.jar"
resource="edu/umd/cs/findbugs/anttask/tasks.properties"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public final class BitFieldUtil
public static long getBitFieldLowerBound(int length, boolean isSigned)
{
if (length <= 0 || length > getMaxBitFieldSize(isSigned))
throw new ZserioError("getBitFieldLowerBound: Asking for lower bound of bitfield with wrong " +
"length " + length + ".");
throw new ZserioError("getBitFieldLowerBound: Asking for lower bound of bitfield with wrong "
+ "length " + length + ".");

if (isSigned)
return -(1L << (length - 1));
Expand All @@ -36,8 +36,8 @@ public static long getBitFieldLowerBound(int length, boolean isSigned)
public static long getBitFieldUpperBound(int length, boolean isSigned)
{
if (length <= 0 || length > getMaxBitFieldSize(isSigned))
throw new ZserioError("getBitFieldUpperBound: Asking for upper bound of bitfield with wrong " +
"length " + length + ".");
throw new ZserioError("getBitFieldUpperBound: Asking for upper bound of bitfield with wrong "
+ "length " + length + ".");

if (isSigned)
return (1L << (length - 1)) - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public final class BitPositionUtil
*/
public static long alignTo(int alignmentValue, long bitPosition)
{
return (bitPosition > 0 && alignmentValue != 0) ?
(((bitPosition - 1) / alignmentValue) + 1) * alignmentValue : bitPosition;
return (bitPosition > 0 && alignmentValue != 0)
? (((bitPosition - 1) / alignmentValue) + 1) * alignmentValue
: bitPosition;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public final class BitSizeOfCalculator
*/
public static int getBitSizeOfVarInt16(short value)
{
final short absoluteValue = (short) Math.abs(value);
final short absoluteValue = (short)Math.abs(value);
if (absoluteValue >= (short)(1 << (6 + 8)))
throw new ZserioError("BitSizeOfCalculator: Value '" + value + "' is out of range for varint16!");

return (absoluteValue < (short) 1 << 6) ? 8 : 16;
return (absoluteValue < (short)1 << 6) ? 8 : 16;
}

/**
Expand Down Expand Up @@ -405,8 +405,7 @@ private static int sizeOfString(final String str)
size = str.getBytes("UTF-8").length;
}
catch (IOException e)
{
}
{}

return size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ public static boolean isSet(ZserioBitmask bitmaskValue, ZserioBitmask requiredMa
{
final Number bitmaskValueGeneric = bitmaskValue.getGenericValue();
final BigInteger bigBitmaskValue = bitmaskValueGeneric instanceof BigInteger
? (BigInteger)bitmaskValueGeneric : BigInteger.valueOf(bitmaskValueGeneric.longValue());
? (BigInteger)bitmaskValueGeneric
: BigInteger.valueOf(bitmaskValueGeneric.longValue());
final Number requiredMaskGeneric = requiredMask.getGenericValue();
final BigInteger bigRequiredMask = requiredMaskGeneric instanceof BigInteger
? (BigInteger)requiredMaskGeneric : BigInteger.valueOf(requiredMaskGeneric.longValue());
? (BigInteger)requiredMaskGeneric
: BigInteger.valueOf(requiredMaskGeneric.longValue());

return bigBitmaskValue.and(bigRequiredMask).equals(bigRequiredMask);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ public ConstraintError(final Throwable throwable)
super(throwable);
}

private static final long serialVersionUID = 921445202560692444L;
private static final long serialVersionUID = 921445202560692444L;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package zserio.runtime;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
Expand Down Expand Up @@ -258,10 +258,8 @@ public static String toJsonString(Object zserioObject)
public static void toJsonFile(Object zserioObject, String fileName, int indent, WalkFilter walkFilter)
throws IOException
{
try (
final OutputStream outputStream = new FileOutputStream(fileName);
final OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
)
try (final OutputStream outputStream = new FileOutputStream(fileName);
final OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))
{
toJsonStream(zserioObject, writer, indent, walkFilter);
}
Expand Down Expand Up @@ -523,8 +521,8 @@ private static TypeInfo getTypeInfo(Class<?> zserioClass)
catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException |
InvocationTargetException e)
{
throw new ZserioError("DebugStringUtil: Zserio object must have type info enabled " +
"(see zserio option -withTypeInfoCode)!");
throw new ZserioError("DebugStringUtil: Zserio object must have type info enabled "
+ "(see zserio option -withTypeInfoCode)!");
}
}

Expand Down
56 changes: 30 additions & 26 deletions compiler/extensions/java/runtime/src/zserio/runtime/FloatUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public final class FloatUtil
public static float convertShortToFloat(short float16Value)
{
// decompose half precision float (float16)
final short sign16Shifted = (short) (float16Value & FLOAT16_SIGN_MASK);
final short exponent16 = (short) ((float16Value & FLOAT16_EXPONENT_MASK) >>
FLOAT16_EXPONENT_BIT_POSITION);
final short significand16 = (short) (float16Value & FLOAT16_SIGNIFICAND_MASK);
final short sign16Shifted = (short)(float16Value & FLOAT16_SIGN_MASK);
final short exponent16 =
(short)((float16Value & FLOAT16_EXPONENT_MASK) >> FLOAT16_EXPONENT_BIT_POSITION);
final short significand16 = (short)(float16Value & FLOAT16_SIGNIFICAND_MASK);

// calculate significand for single precision float (float32)
int significand32 = ((int) significand16) <<
(FLOAT32_SIGNIFICAND_NUM_BITS - FLOAT16_SIGNIFICAND_NUM_BITS);
int significand32 = ((int)significand16)
<< (FLOAT32_SIGNIFICAND_NUM_BITS - FLOAT16_SIGNIFICAND_NUM_BITS);

// calculate exponent for single precision float (float32)
int exponent32;
Expand Down Expand Up @@ -65,8 +65,8 @@ else if (exponent16 == FLOAT16_EXPONENT_INFINITY_NAN)
}

// compose single precision float (float32)
final int sign32Shifted = (int) (sign16Shifted) << (FLOAT32_SIGN_BIT_POSITION -
FLOAT16_SIGN_BIT_POSITION);
final int sign32Shifted = (int)(sign16Shifted)
<< (FLOAT32_SIGN_BIT_POSITION - FLOAT16_SIGN_BIT_POSITION);
final int exponent32Shifted = exponent32 << FLOAT32_EXPONENT_BIT_POSITION;
final int float32Value = sign32Shifted | exponent32Shifted | significand32;

Expand All @@ -91,8 +91,8 @@ public static short convertFloatToShort(float float32)
final int significand32 = (float32Value & FLOAT32_SIGNIFICAND_MASK);

// calculate significand for half precision float (float16)
short significand16 = (short) ((significand32 >>
(FLOAT32_SIGNIFICAND_NUM_BITS - FLOAT16_SIGNIFICAND_NUM_BITS)));
short significand16 =
(short)((significand32 >> (FLOAT32_SIGNIFICAND_NUM_BITS - FLOAT16_SIGNIFICAND_NUM_BITS)));

// calculate exponent for half precision float (float16)
boolean needsRounding = false;
Expand All @@ -114,7 +114,7 @@ else if (exponent32 == FLOAT32_EXPONENT_INFINITY_NAN)
else
{
// normal number
final short signedExponent16 = (short) (exponent32 - FLOAT32_EXPONENT_BIAS + FLOAT16_EXPONENT_BIAS);
final short signedExponent16 = (short)(exponent32 - FLOAT32_EXPONENT_BIAS + FLOAT16_EXPONENT_BIAS);
if (signedExponent16 > FLOAT16_EXPONENT_INFINITY_NAN)
{
// exponent overflow, set infinity or NaN
Expand All @@ -123,7 +123,7 @@ else if (exponent32 == FLOAT32_EXPONENT_INFINITY_NAN)
else if (signedExponent16 <= 0)
{
// exponent underflow
if (signedExponent16 <= (short) (-FLOAT16_SIGNIFICAND_NUM_BITS))
if (signedExponent16 <= (short)(-FLOAT16_SIGNIFICAND_NUM_BITS))
{
// too big underflow, set to zero
exponent16 = 0;
Expand All @@ -135,31 +135,35 @@ else if (signedExponent16 <= 0)
exponent16 = 0;
final int fullSignificand32 = significand32 | (FLOAT32_SIGNIFICAND_MASK + 1);
final int significandShift = 1 - signedExponent16;
significand16 = (short) (fullSignificand32 >>
significand16 = (short)(fullSignificand32 >>
(FLOAT32_SIGNIFICAND_NUM_BITS - FLOAT16_SIGNIFICAND_NUM_BITS + significandShift));

needsRounding = ((fullSignificand32 >> (FLOAT32_SIGNIFICAND_NUM_BITS -
FLOAT16_SIGNIFICAND_NUM_BITS + significandShift - 1)) & 0x01) != 0;
needsRounding =
((fullSignificand32 >>
(FLOAT32_SIGNIFICAND_NUM_BITS - FLOAT16_SIGNIFICAND_NUM_BITS +
significandShift - 1)) &
0x01) != 0;
}
}
else
{
// exponent ok
exponent16 = signedExponent16;
needsRounding = ((significand32 >> (FLOAT32_SIGNIFICAND_NUM_BITS -
FLOAT16_SIGNIFICAND_NUM_BITS - 1)) & 0x01) != 0;
needsRounding =
((significand32 >> (FLOAT32_SIGNIFICAND_NUM_BITS - FLOAT16_SIGNIFICAND_NUM_BITS - 1)) &
0x01) != 0;
}
}

// compose half precision float (float16)
final short sign16Shifted = (short) (sign32Shifted >>> (FLOAT32_SIGN_BIT_POSITION -
FLOAT16_SIGN_BIT_POSITION));
final short exponent16Shifted = (short) (exponent16 << FLOAT16_EXPONENT_BIT_POSITION);
short float16Value = (short) (sign16Shifted | exponent16Shifted | significand16);
final short sign16Shifted =
(short)(sign32Shifted >>> (FLOAT32_SIGN_BIT_POSITION - FLOAT16_SIGN_BIT_POSITION));
final short exponent16Shifted = (short)(exponent16 << FLOAT16_EXPONENT_BIT_POSITION);
short float16Value = (short)(sign16Shifted | exponent16Shifted | significand16);

// check rounding
if (needsRounding)
float16Value += (short) 1; // might overflow to infinity
float16Value += (short)1; // might overflow to infinity

return float16Value;
}
Expand Down Expand Up @@ -212,16 +216,16 @@ public static long convertDoubleToLong(double float64)
return Double.doubleToLongBits(float64);
}

private static final short FLOAT16_SIGN_MASK = (short) 0x8000;
private static final short FLOAT16_EXPONENT_MASK = (short) 0x7C00;
private static final short FLOAT16_SIGNIFICAND_MASK = (short) 0x03FF;
private static final short FLOAT16_SIGN_MASK = (short)0x8000;
private static final short FLOAT16_EXPONENT_MASK = (short)0x7C00;
private static final short FLOAT16_SIGNIFICAND_MASK = (short)0x03FF;

private static final short FLOAT16_SIGN_BIT_POSITION = 15;
private static final short FLOAT16_EXPONENT_BIT_POSITION = 10;

private static final short FLOAT16_SIGNIFICAND_NUM_BITS = FLOAT16_EXPONENT_BIT_POSITION;

private static final short FLOAT16_EXPONENT_INFINITY_NAN = (short) 0x001F;
private static final short FLOAT16_EXPONENT_INFINITY_NAN = (short)0x001F;
private static final short FLOAT16_EXPONENT_BIAS = 15;

private static final int FLOAT32_SIGN_MASK = 0x80000000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public static int convertBitBufferSizeToInt(long value)
if (value > Integer.MAX_VALUE)
throw new ZserioError("VarSizeUtil: Value '" + value + "' is out of bounds for conversion!");

return (int) value;
return (int)value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ public ZserioError(final Throwable throwable)
super(throwable);
}

private static final long serialVersionUID = 921445202560692775L;
private static final long serialVersionUID = 921445202560692775L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public int bitSizeOf(long bitPosition)
else
{
endBitPosition = BitPositionUtil.alignTo(Byte.SIZE, endBitPosition);
endBitPosition += elementSize + (size - 1) * BitPositionUtil.alignTo(Byte.SIZE, elementSize);
endBitPosition +=
elementSize + (size - 1) * BitPositionUtil.alignTo(Byte.SIZE, elementSize);
}
}
else
Expand Down Expand Up @@ -169,8 +170,8 @@ public int bitSizeOfPacked(long bitPosition)
if (offsetInitializer != null)
endBitPosition = BitPositionUtil.alignTo(Byte.SIZE, endBitPosition);

endBitPosition += packedArrayTraits.bitSizeOf(
context, endBitPosition, rawArray.getElement(index));
endBitPosition +=
packedArrayTraits.bitSizeOf(context, endBitPosition, rawArray.getElement(index));
}
}

Expand Down Expand Up @@ -227,7 +228,7 @@ public long initializeOffsetsPacked(long bitPosition)
for (int index = 0; index < size; ++index)
packedArrayTraits.initContext(context, rawArray.getElement(index));

for (int index= 0; index < size; ++index)
for (int index = 0; index < size; ++index)
{
if (offsetInitializer != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public interface ArrayElement
/**
* Interface for elements of integral arrays.
*/
public static interface IntegralArrayElement extends ArrayElement
{
public static interface IntegralArrayElement extends ArrayElement {
/**
* Converts the element value to big integer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import java.math.BigInteger;

import zserio.runtime.BitSizeOfCalculator;
import zserio.runtime.SizeOf;
import zserio.runtime.PackableSizeOf;
import zserio.runtime.SizeOf;
import zserio.runtime.array.ArrayElement.IntegralArrayElement;
import zserio.runtime.io.BitBuffer;
import zserio.runtime.io.BitStreamReader;
import zserio.runtime.io.BitStreamWriter;
import zserio.runtime.io.Writer;
import zserio.runtime.io.PackableWriter;
import zserio.runtime.io.Writer;

/**
* Interface for array traits.
Expand Down Expand Up @@ -79,8 +79,7 @@ public interface ArrayTraits
/**
* Interface for integral array traits.
*/
public static interface IntegralArrayTraits extends ArrayTraits
{
public static interface IntegralArrayTraits extends ArrayTraits {
/**
* Creates integral array element from given big integer value.
*
Expand Down Expand Up @@ -1374,8 +1373,7 @@ public void write(BitStreamWriter writer, ArrayElement element) throws IOExcepti
/**
* Array traits for zserio object arrays (with writer part) which are mapped to Java zserio object array.
*/
public static final class WriteObjectArrayTraits<E extends Writer & SizeOf>
extends ObjectArrayTraits<E>
public static final class WriteObjectArrayTraits<E extends Writer & SizeOf> extends ObjectArrayTraits<E>
{
/**
* Constructor from element factory.
Expand Down
Loading

0 comments on commit 4bdaaf3

Please sign in to comment.