Skip to content

Commit

Permalink
Add clang-format to the Java extension project
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Jan 5, 2024
1 parent f347b38 commit 20a351b
Show file tree
Hide file tree
Showing 43 changed files with 326 additions and 280 deletions.
18 changes: 17 additions & 1 deletion compiler/extensions/java/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ zserio_java.install_dir - Directory where to install Zserio Doc Extension
zserio_core.jar_file - Zserio core jar file used for classpath.
Default is "${zserio_extensions.build_dir}/../core/${ant.java.version}/jar/
zserio_core.jar".
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 @@ -150,7 +151,22 @@ spotbugs.home_dir - Location of the spotbugs tool. If not set, spotb
</copy>
</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.src_dir}" includes="**/*.java"/>
</apply>
</target>

<target name="spotbugs" depends="clang_format" if="spotbugs.home_dir">
<mkdir dir="${zserio_java.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 @@ -62,8 +62,8 @@ private static String getAccessorName(String accessorNamePrefix, String memberNa
return getAccessorName(accessorNamePrefix, memberName, "");
}

private static String getAccessorName(String accessorNamePrefix, String memberName,
String accessorNameSuffix)
private static String getAccessorName(
String accessorNamePrefix, String memberName, String accessorNameSuffix)
{
StringBuilder accessorName = new StringBuilder(accessorNamePrefix);
if (!memberName.isEmpty())
Expand All @@ -87,5 +87,5 @@ private static String getAccessorName(String accessorNamePrefix, String memberNa
private static final String GETTER_NAME_PREFIX = "get";
private static final String SETTER_NAME_PREFIX = "set";
private static final String FUNCTION_NAME_PREFIX = "func";
private static final String REMOVED_ENUMERATOR_PREFIX ="ZSERIO_REMOVED_";
private static final String REMOVED_ENUMERATOR_PREFIX = "ZSERIO_REMOVED_";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public boolean getIsDynamicBitField()

public static BitSizeTemplateData create(TypeInstantiation typeInstantiation,
ExpressionFormatter javaExpressionFormatter, ExpressionFormatter javaLambdaExpressionFormatter)
throws ZserioExtensionException
throws ZserioExtensionException
{
if (typeInstantiation.getBaseType() instanceof FixedSizeType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,21 @@ public BitmaskEmitterTemplateData(TemplateDataContext context, BitmaskType bitma
javaNativeMapper.getJavaIntegralType(bitmaskTypeInstantiation);

underlyingTypeInfo = new NativeTypeInfoTemplateData(nativeIntegralType, bitmaskTypeInstantiation);
bitSize = BitSizeTemplateData.create(bitmaskTypeInstantiation, javaExpressionFormatter,
javaLambdaExpressionFormatter);
bitSize = BitSizeTemplateData.create(
bitmaskTypeInstantiation, javaExpressionFormatter, javaLambdaExpressionFormatter);
runtimeFunction = RuntimeFunctionDataCreator.createData(context, bitmaskTypeInstantiation);

final BigInteger lowerBound = getLowerBound(bitmaskTypeInstantiation);
this.lowerBound = lowerBound.equals(nativeIntegralType.getLowerBound()) ? null :
nativeIntegralType.formatLiteral(lowerBound);
this.lowerBound = lowerBound.equals(nativeIntegralType.getLowerBound())
? null
: nativeIntegralType.formatLiteral(lowerBound);
// upper bound is needed for negation since java uses signed types!
final BigInteger upperBound = getUpperBound(bitmaskTypeInstantiation);
this.upperBound = nativeIntegralType.formatLiteral(upperBound);
this.checkUpperBound = !upperBound.equals(nativeIntegralType.getUpperBound());

values = new ArrayList<BitmaskValueData>();
for (BitmaskValue bitmaskValue: bitmaskType.getValues())
for (BitmaskValue bitmaskValue : bitmaskType.getValues())
values.add(new BitmaskValueData(context, nativeIntegralType, bitmaskValue));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public CaseMember(TemplateDataContext context, ChoiceType choiceType, ChoiceCase
caseList.add(new Case(context, caseExpression.getExpression()));

final Field fieldType = choiceCaseType.getField();
compoundField = (fieldType != null)
? new CompoundFieldTemplateData(context, choiceType, fieldType) : null;
compoundField =
(fieldType != null) ? new CompoundFieldTemplateData(context, choiceType, fieldType) : null;
}

public Iterable<Case> getCaseList()
Expand All @@ -122,7 +122,8 @@ public CompoundFieldTemplateData getCompoundField()

public static final class Case
{
public Case(TemplateDataContext context, Expression choiceExpression) throws ZserioExtensionException
public Case(TemplateDataContext context, Expression choiceExpression)
throws ZserioExtensionException
{
final ExpressionFormatter javaExpressionFormatter = context.getJavaExpressionFormatter();
final ExpressionFormatter javaCaseExpressionFormatter =
Expand All @@ -145,7 +146,7 @@ public String getExpressionForCase()
public final String expressionForCase;
}

private final List<Case> caseList;
private final List<Case> caseList;
private final CompoundFieldTemplateData compoundField;
}

Expand All @@ -155,8 +156,8 @@ public DefaultMember(TemplateDataContext context, ChoiceType choiceType,
ChoiceDefault choiceDefaultType) throws ZserioExtensionException
{
final Field fieldType = choiceDefaultType.getField();
compoundField = (fieldType != null) ?
new CompoundFieldTemplateData(context, choiceType, fieldType) : null;
compoundField =
(fieldType != null) ? new CompoundFieldTemplateData(context, choiceType, fieldType) : null;
}

public CompoundFieldTemplateData getCompoundField()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public CompoundParameterTemplateData getCompoundParametersData()
return compoundParametersData;
}

private final String compoundName;
private final String compoundName;
private final CompoundParameterTemplateData compoundParametersData;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import java.util.ArrayList;

import zserio.ast.ArrayInstantiation;
import zserio.ast.ChoiceType;
import zserio.ast.CompoundType;
import zserio.ast.ParameterizedTypeInstantiation;
import zserio.ast.ParameterizedTypeInstantiation.InstantiatedParameter;
import zserio.ast.ArrayInstantiation;
import zserio.ast.Expression;
import zserio.ast.Field;
import zserio.ast.ParameterizedTypeInstantiation;
import zserio.ast.ParameterizedTypeInstantiation.InstantiatedParameter;
import zserio.ast.TypeInstantiation;
import zserio.ast.TypeReference;
import zserio.ast.UnionType;
Expand Down Expand Up @@ -41,8 +41,8 @@ public CompoundFieldTemplateData(TemplateDataContext context, CompoundType paren
final JavaNativeMapper javaNativeMapper = context.getJavaNativeMapper();
final boolean isTypeNullable = (optional != null);
final JavaNativeType nullableNativeType = javaNativeMapper.getNullableJavaType(fieldTypeInstantiation);
final JavaNativeType nativeType = (isTypeNullable) ? nullableNativeType :
javaNativeMapper.getJavaType(fieldTypeInstantiation);
final JavaNativeType nativeType =
(isTypeNullable) ? nullableNativeType : javaNativeMapper.getJavaType(fieldTypeInstantiation);

nullableTypeInfo = new NativeTypeInfoTemplateData(nullableNativeType, fieldTypeInstantiation);
typeInfo = new NativeTypeInfoTemplateData(nativeType, fieldTypeInstantiation);
Expand All @@ -66,8 +66,8 @@ public CompoundFieldTemplateData(TemplateDataContext context, CompoundType paren
constraint = createConstraint(field, javaExpressionFormatter);
lambdaConstraint = createConstraint(field, javaLambdaExpressionFormatter);

bitSize = BitSizeTemplateData.create(fieldTypeInstantiation, javaExpressionFormatter,
javaLambdaExpressionFormatter);
bitSize = BitSizeTemplateData.create(
fieldTypeInstantiation, javaExpressionFormatter, javaLambdaExpressionFormatter);
offset = createOffset(field, javaNativeMapper, javaExpressionFormatter, javaLambdaExpressionFormatter);
array = createArray(context, nativeType, fieldTypeInstantiation, parentType);
runtimeFunction = RuntimeFunctionDataCreator.createData(context, fieldTypeInstantiation);
Expand Down Expand Up @@ -184,13 +184,15 @@ public static final class Optional
{
public Optional(Field field, ExpressionFormatter javaExpressionFormatter,
ExpressionFormatter javaLambdaExpressionFormatter, boolean isRecursive)
throws ZserioExtensionException
throws ZserioExtensionException
{
final Expression optionalClauseExpression = field.getOptionalClauseExpr();
clause = (optionalClauseExpression == null) ? null :
javaExpressionFormatter.formatGetter(optionalClauseExpression);
lambdaClause = (optionalClauseExpression == null) ? null :
javaLambdaExpressionFormatter.formatGetter(optionalClauseExpression);
clause = (optionalClauseExpression == null)
? null
: javaExpressionFormatter.formatGetter(optionalClauseExpression);
lambdaClause = (optionalClauseExpression == null)
? null
: javaLambdaExpressionFormatter.formatGetter(optionalClauseExpression);
isUsedIndicatorName = AccessorNameFormatter.getIsUsedIndicatorName(field);
isSetIndicatorName = AccessorNameFormatter.getIsSetIndicatorName(field);
resetterName = AccessorNameFormatter.getResetterName(field);
Expand Down Expand Up @@ -238,8 +240,8 @@ public boolean getIsRecursive()
public static final class Offset
{
public Offset(Expression offsetExpression, JavaNativeMapper javaNativeMapper,
ExpressionFormatter javaExpressionFormatter,
ExpressionFormatter javaLambdaExpressionFormatter) throws ZserioExtensionException
ExpressionFormatter javaExpressionFormatter, ExpressionFormatter javaLambdaExpressionFormatter)
throws ZserioExtensionException
{
getter = javaExpressionFormatter.formatGetter(offsetExpression);
setter = javaExpressionFormatter.formatSetter(offsetExpression);
Expand Down Expand Up @@ -312,8 +314,8 @@ public Array(TemplateDataContext context, NativeArrayType nativeType,
requiresElementClass = nativeRawArray.requiresElementClass();
requiresOwnerContext = createRequiresOwnerContext(elementTypeInstantiation);

elementBitSize = BitSizeTemplateData.create(elementTypeInstantiation, javaExpressionFormatter,
javaLambdaExpressionFormatter);
elementBitSize = BitSizeTemplateData.create(
elementTypeInstantiation, javaExpressionFormatter, javaLambdaExpressionFormatter);
elementCompound = createCompound(context, elementTypeInstantiation);
elementIsRecursive = elementTypeInstantiation.getBaseType() == parentType;
elementUsedInPackedArray = context.getPackedTypesCollector().isUsedInPackedArray(
Expand Down Expand Up @@ -475,12 +477,11 @@ public InstantiatedParameterData(TemplateDataContext context,
final TypeReference parameterTypeReference =
instantiatedParameter.getParameter().getTypeReference();
final JavaNativeMapper javaNativeMapper = context.getJavaNativeMapper();
final JavaNativeType nativeParameterType =
javaNativeMapper.getJavaType(parameterTypeReference);
final JavaNativeType nativeParameterType = javaNativeMapper.getJavaType(parameterTypeReference);
typeInfo = new NativeTypeInfoTemplateData(nativeParameterType, parameterTypeReference);
final ExpressionFormatter javaExpressionFormatter = context.getJavaExpressionFormatter();
expression = javaExpressionFormatter.formatGetter(
instantiatedParameter.getArgumentExpression());
expression =
javaExpressionFormatter.formatGetter(instantiatedParameter.getArgumentExpression());
final ExpressionFormatter javaLambdaExpressionFormatter =
context.getJavaLambdaExpressionFormatter();
lambdaExpression = javaLambdaExpressionFormatter.formatGetter(
Expand Down Expand Up @@ -513,7 +514,7 @@ public String getLambdaExpression()

private static Optional createOptional(Field field, ZserioType fieldBaseType, CompoundType parentType,
ExpressionFormatter javaExpressionFormatter, ExpressionFormatter javaLambdaExpressionFormatter)
throws ZserioExtensionException
throws ZserioExtensionException
{
if (!field.isOptional())
return null;
Expand Down Expand Up @@ -555,14 +556,14 @@ private static String createConstraint(Field field, ExpressionFormatter javaExpr

private static Offset createOffset(Field field, JavaNativeMapper javaNativeMapper,
ExpressionFormatter javaExpressionFormatter, ExpressionFormatter javaLambdaExpressionFormatter)
throws ZserioExtensionException
throws ZserioExtensionException
{
final Expression offsetExpression = field.getOffsetExpr();
if (offsetExpression == null)
return null;

return new Offset(offsetExpression, javaNativeMapper, javaExpressionFormatter,
javaLambdaExpressionFormatter);
return new Offset(
offsetExpression, javaNativeMapper, javaExpressionFormatter, javaLambdaExpressionFormatter);
}

private static Array createArray(TemplateDataContext context, JavaNativeType nativeType,
Expand All @@ -578,8 +579,8 @@ private static Array createArray(TemplateDataContext context, JavaNativeType nat
nativeType.getClass().getName() + "'!");
}

return new Array(context, (NativeArrayType)nativeType,
(ArrayInstantiation)typeInstantiation, parentType);
return new Array(
context, (NativeArrayType)nativeType, (ArrayInstantiation)typeInstantiation, parentType);
}

private static Compound createCompound(TemplateDataContext context, TypeInstantiation typeInstantiation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.List;

import zserio.ast.CompoundType;
import zserio.ast.TypeReference;
import zserio.ast.Function;
import zserio.ast.TypeReference;
import zserio.extension.common.ExpressionFormatter;
import zserio.extension.common.ZserioExtensionException;
import zserio.extension.java.types.JavaNativeType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
*/
final class DocCommentsDataCreator
{
public static DocCommentsTemplateData createData(TemplateDataContext context,
DocumentableAstNode documentableNode) throws ZserioExtensionException
public static DocCommentsTemplateData createData(
TemplateDataContext context, DocumentableAstNode documentableNode) throws ZserioExtensionException
{
return createData(context, documentableNode.getDocComments());
}

public static DocCommentsTemplateData createData(TemplateDataContext context,
List<DocComment> docComments) throws ZserioExtensionException
public static DocCommentsTemplateData createData(TemplateDataContext context, List<DocComment> docComments)
throws ZserioExtensionException
{
final int numComments = docComments.size();
int firstStickyCommentIndex = numComments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ else if (referencedScopeSymbol == null)
final JavaNativeSymbol nativePackageSymbol =
javaNativeMapper.getJavaSymbol(referencedPackageSymbol);
link = getPackageSymbolFullName(nativePackageSymbol, referencedPackageSymbol) +
getScopeSymbolSuffix(nativePackageSymbol, referencedPackageSymbol, referencedScopeSymbol);
getScopeSymbolSuffix(
nativePackageSymbol, referencedPackageSymbol, referencedScopeSymbol);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
final class EnumerationEmitter extends JavaDefaultEmitter
{
public EnumerationEmitter(OutputFileManager outputFileManager, JavaExtensionParameters javaParameters,
PackedTypesCollector packedTypesCollector)
PackedTypesCollector packedTypesCollector)
{
super(outputFileManager, javaParameters, packedTypesCollector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public EnumerationEmitterTemplateData(TemplateDataContext context, EnumType enum
javaNativeMapper.getJavaIntegralType(enumTypeInstantiation);

underlyingTypeInfo = new NativeTypeInfoTemplateData(nativeIntegralType, enumTypeInstantiation);
bitSize = BitSizeTemplateData.create(enumTypeInstantiation, javaExpressionFormatter,
javaLambdaExpressionFormatter);
bitSize = BitSizeTemplateData.create(
enumTypeInstantiation, javaExpressionFormatter, javaLambdaExpressionFormatter);
runtimeFunction = RuntimeFunctionDataCreator.createData(context, enumTypeInstantiation);

items = new ArrayList<EnumItemData>();
for (EnumItem item: enumType.getItems())
for (EnumItem item : enumType.getItems())
items.add(new EnumItemData(context, nativeIntegralType, item));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ private void processTemplate(String templateName, Object templateData, PackageNa
final File outDir = new File(javaParameters.getOutputDir(), packageName.toFilesystemPath());
final File outputFile = new File(outDir, outFileNameRoot + JAVA_SOURCE_EXTENSION);

final boolean generate = !outputFileManager.checkTimestamps(outputFile) ||
!checkGeneratorDescription(outputFile);
final boolean generate =
!outputFileManager.checkTimestamps(outputFile) || !checkGeneratorDescription(outputFile);
if (generate)
{
FreeMarkerUtil.processTemplate(JAVA_TEMPLATE_LOCATION + templateName, templateData, outputFile,
false);
FreeMarkerUtil.processTemplate(
JAVA_TEMPLATE_LOCATION + templateName, templateData, outputFile, false);
}

outputFileManager.registerOutputFile(outputFile, generate);
Expand All @@ -106,7 +106,7 @@ private boolean checkGeneratorDescription(File outputFile)
{
final String[] generatorDescription = context.getGeneratorDescription().split("\\n");
final String[] generatorDescriptionCandidate =
lines.limit(generatorDescription.length).toArray(String[]::new);
lines.limit(generatorDescription.length).toArray(String[] ::new);
return Arrays.equals(generatorDescription, generatorDescriptionCandidate);
}
catch (IOException e)
Expand Down
Loading

0 comments on commit 20a351b

Please sign in to comment.