Avoid using dollar signs in identifiers as auto-generated names containing dollar signs ($) may lead to unexpected issues that can be difficult to diagnose.
Non-Compliant Code Examples
publicclass$Foo{}publicclassFo$o{}publicclassFoo${Stringbar$="bar";publicStringba$(){return"baz";}}/*
/* Copyright 2018-2024 contributors to the OpenLineage project
/* SPDX-License-Identifier: Apache-2.0
*/packageio.openlineage.spark3.agent.lifecycle.plan.column;import staticorg.mockito.ArgumentMatchers.any;import staticorg.mockito.Mockito.doAnswer;import staticorg.mockito.Mockito.mock;import staticorg.mockito.Mockito.mockStatic;import staticorg.mockito.Mockito.never;import staticorg.mockito.Mockito.times;import staticorg.mockito.Mockito.verify;import staticorg.mockito.Mockito.when;importio.openlineage.spark.agent.lifecycle.plan.column.ColumnLevelLineageBuilder;importio.openlineage.spark.agent.lifecycle.plan.column.ColumnLevelLineageContext;importio.openlineage.spark.agent.util.ScalaConversionUtils;importio.openlineage.sql.ColumnMeta;importjava.util.Arrays;importjava.util.HashMap;importjava.util.Map;importjava.util.concurrent.atomic.LongAccumulator;importorg.apache.spark.sql.catalyst.expressions.Attribute;importorg.apache.spark.sql.catalyst.expressions.AttributeReference;importorg.apache.spark.sql.catalyst.expressions.ExprId;importorg.apache.spark.sql.catalyst.expressions.NamedExpression;importorg.apache.spark.sql.catalyst.util.CaseInsensitiveMap$;importorg.apache.spark.sql.execution.datasources.jdbc.JDBCOptions;importorg.apache.spark.sql.execution.datasources.jdbc.JDBCRelation;importorg.apache.spark.sql.types.IntegerType$;importorg.apache.spark.sql.types.Metadata$;importorg.apache.spark.sql.types.StringType$;importorg.junit.jupiter.api.BeforeEach;importorg.junit.jupiter.api.Test;importorg.mockito.MockedStatic;importorg.mockito.stubbing.Answer;classJdbcColumnLineageExpressionCollectorTest{ColumnLevelLineageBuilderbuilder=mock(ColumnLevelLineageBuilder.class);ColumnLevelLineageContextcontext=mock(ColumnLevelLineageContext.class);ExprIdexprId1=ExprId.apply(20);ExprIdexprId2=ExprId.apply(21);ExprIddependencyId1=ExprId.apply(0);ExprIddependencyId2=ExprId.apply(1);Attributeexpression1=newAttributeReference("k",IntegerType$.MODULE$,false,Metadata$.MODULE$.empty(),exprId1,null);Attributeexpression2=newAttributeReference("j",StringType$.MODULE$,false,Metadata$.MODULE$.empty(),exprId2,null);JDBCRelationrelation=mock(JDBCRelation.class);JDBCOptionsjdbcOptions=mock(JDBCOptions.class);StringjdbcQuery="(select js1.k, CONCAT(js1.j1, js2.j2) as j from jdbc_source1 js1 join jdbc_source2 js2 on js1.k = js2.k) SPARK_GEN_SUBQ_0";StringinvalidJdbcQuery="(INVALID) SPARK_GEN_SUBQ_0";Stringurl="jdbc:postgresql://localhost:5432/test";Map<ColumnMeta,ExprId>mockMap=newHashMap<>();@BeforeEachvoidsetup(){when(relation.jdbcOptions()).thenReturn(jdbcOptions);scala.collection.immutable.Map<String,String>properties=ScalaConversionUtils.<String,String>asScalaMapEmpty();when(jdbcOptions.parameters()).thenReturn(CaseInsensitiveMap$.MODULE$.<String>apply(properties));when(context.getBuilder()).thenReturn(builder);}@TestvoidtestInputCollection(){when(jdbcOptions.tableOrQuery()).thenReturn(jdbcQuery);when(jdbcOptions.url()).thenReturn(url);finalLongAccumulatorid=newLongAccumulator(Long::sum,0L);try(MockedStatic<NamedExpression>utilities=mockStatic(NamedExpression.class)){utilities.when(NamedExpression::newExprId).thenAnswer((Answer<ExprId>)invocation->{ExprIdexprId=ExprId.apply(id.get());id.accumulate(1);returnexprId;});doAnswer(invocation->mockMap.putIfAbsent(invocation.getArgument(0),invocation.getArgument(1))).when(builder).addExternalMapping(any(ColumnMeta.class),any(ExprId.class));when(builder.getMapping(any(ColumnMeta.class))).thenAnswer(invocation->mockMap.get(invocation.getArgument(0)));JdbcColumnLineageCollector.extractExpressionsFromJDBC(context,relation,Arrays.asList(expression1,expression2));verify(builder,times(1)).addDependency(exprId2,dependencyId1);verify(builder,times(1)).addDependency(exprId2,dependencyId2);utilities.verify(NamedExpression::newExprId,times(3));}}@TestvoidtestInvalidQuery(){when(jdbcOptions.tableOrQuery()).thenReturn(invalidJdbcQuery);when(jdbcOptions.url()).thenReturn(url);JdbcColumnLineageCollector.extractExpressionsFromJDBC(context,relation,Arrays.asList(expression1,expression2));verify(builder,never()).addDependency(any(ExprId.class),any(ExprId.class));}}\
Compliant Code Examples
publicclassFoo{}
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- java-code-style # Rules to enforce Java code style.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines