Skip to content

Commit 4b575ff

Browse files
authored
Fix custom PMD rules (#513)
1 parent ebadc87 commit 4b575ff

File tree

16 files changed

+62
-114
lines changed

16 files changed

+62
-114
lines changed

cloudplatform/cloudplatform-connectivity/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/AuthenticationType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public enum AuthenticationType
9292
@Getter
9393
private final String identifier;
9494

95+
@Nonnull
9596
@Override
9697
public String toString()
9798
{

cloudplatform/cloudplatform-connectivity/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DestinationType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public enum DestinationType
4545
this.identifier = identifier;
4646
}
4747

48+
@Nonnull
4849
@Override
4950
public String toString()
5051
{
@@ -100,9 +101,7 @@ public static DestinationType ofIdentifierOrDefault(
100101
return ofIdentifier(identifier);
101102
}
102103
catch( final IllegalArgumentException e ) {
103-
if( log.isWarnEnabled() ) {
104-
log.warn("Identifier '{}' is not supported. Falling back to {}.", identifier, defaultDestinationType);
105-
}
104+
log.warn("Identifier '{}' is not supported. Falling back to {}.", identifier, defaultDestinationType);
106105
return defaultDestinationType;
107106
}
108107
}

cloudplatform/cloudplatform-connectivity/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/ProxyType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ public enum ProxyType
6262
return ofIdentifier(identifier);
6363
}
6464
catch( final IllegalArgumentException e ) {
65-
if( log.isWarnEnabled() ) {
66-
log.warn("Identifier '{}' is not supported. Falling back to {}.", identifier, defaultProxyType);
67-
}
65+
log.warn("Identifier '{}' is not supported. Falling back to {}.", identifier, defaultProxyType);
6866
return defaultProxyType;
6967
}
7068
}
@@ -133,6 +131,7 @@ public static ProxyType ofIdentifierSensitive( @Nonnull final String identifier
133131
throw new IllegalArgumentException("Unknown " + ProxyType.class.getSimpleName() + ": " + identifier + ".");
134132
}
135133

134+
@Nonnull
136135
@Override
137136
public String toString()
138137
{

cloudplatform/cloudplatform-connectivity/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/SecurityConfigurationStrategy.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public enum SecurityConfigurationStrategy
4343
*
4444
* @return The default value
4545
*/
46+
@Nonnull
4647
public static SecurityConfigurationStrategy getDefault()
4748
{
4849
return FROM_DESTINATION;
@@ -76,6 +77,7 @@ public static SecurityConfigurationStrategy ofIdentifierOrDefault( @Nullable fin
7677
throw new IllegalArgumentException("No SecurityConfigurationStrategy found for identifier: " + identifier);
7778
}
7879

80+
@Nonnull
7981
@Override
8082
public String toString()
8183
{

cloudplatform/connectivity-apache-httpclient4/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/HttpClientWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public HeaderElement[] getElements()
7373
return new HeaderElement[0];
7474
}
7575

76+
@Nonnull
7677
@Override
7778
public String toString()
7879
{

cloudplatform/connectivity-destination-service/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DestinationServiceRetrievalStrategy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public enum DestinationServiceRetrievalStrategy
5252
this.identifier = identifier;
5353
}
5454

55+
@Nonnull
5556
@Override
5657
public String toString()
5758
{

cloudplatform/connectivity-destination-service/src/main/java/com/sap/cloud/sdk/cloudplatform/connectivity/DestinationServiceTokenExchangeStrategy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public enum DestinationServiceTokenExchangeStrategy
7272
this.identifier = identifier;
7373
}
7474

75+
@Nonnull
7576
@Override
7677
public String toString()
7778
{

codestyle/sdk_specific_pmd_rules.xml

Lines changed: 36 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
<property name="xpath">
2121
<value>
2222
<![CDATA[
23-
//ClassOrInterfaceBodyDeclaration
24-
/FieldDeclaration[@Static]
25-
//PrimaryExpression[
26-
PrimaryPrefix/Name[@Image = "LoggerFactory.getLogger"] and
27-
PrimarySuffix//ClassOrInterfaceType[@Image = ancestor::ClassOrInterfaceDeclaration/@SimpleName]
28-
]
23+
//FieldDeclaration[@Static]
24+
/VariableDeclarator
25+
/MethodCall[@MethodName = "getLogger"]/AmbiguousName[@Name = "LoggerFactory"]
2926
]]>
3027
</value>
3128
</property>
@@ -63,10 +60,10 @@
6360
<property name="xpath">
6461
<value>
6562
<![CDATA[
66-
//TypeDeclaration
63+
//ClassBody/*
6764
[
68-
//FieldDeclaration/Type/ReferenceType/ClassOrInterfaceType[@Image='Optional'] or
69-
//FormalParameter/Type/ReferenceType/ClassOrInterfaceType[@Image='Optional']
65+
self::FieldDeclaration/ClassType[@SimpleName='Optional'] or
66+
self::*/FormalParameters/FormalParameter/ClassType[@SimpleName='Optional']
7067
]
7168
]]>
7269
</value>
@@ -111,7 +108,7 @@
111108
<value>
112109
<![CDATA[
113110
//ImportDeclaration
114-
[starts-with(Name/@Image, 'com.google.common.base.Optional')]
111+
[starts-with(@ImportedName, 'com.google.common.base.Optional')]
115112
]]>
116113
</value>
117114
</property>
@@ -134,12 +131,9 @@
134131
<property name="xpath">
135132
<value>
136133
<![CDATA[
137-
//ClassOrInterfaceDeclaration[@Public='true']
138-
//ClassOrInterfaceBodyDeclaration
139-
[MethodDeclaration[@Public='true' and @Void='false']/ResultType/Type/ReferenceType and
140-
not(
141-
Annotation/MarkerAnnotation/Name[@Image='Nullable'] or
142-
Annotation/MarkerAnnotation/Name[@Image='Nonnull']
134+
//MethodDeclaration[@EffectiveVisibility='public' and @Void=false() and ClassType and not(
135+
./ModifierList/Annotation[@SimpleName='Nullable'] or
136+
./ModifierList/Annotation[@SimpleName='Nonnull']
143137
)]
144138
]]>
145139
</value>
@@ -182,68 +176,22 @@
182176
message="Parameter type of public method is not annotated with @Nonnull or @Nullable."
183177
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
184178
<description>
185-
Parameter type of public method is not annotated with @Nonnull or @Nullable.
179+
Missing @Nonnull or @Nullable annotation on parameter of public method or constructor.
186180
This is beneficial to communicate a clear API contract and it allows for effective argument constraint validation in the IDE.
187181
</description>
188182
<priority>5</priority>
189183
<properties>
190184
<property name="xpath">
191185
<value>
192186
<![CDATA[
193-
//ClassOrInterfaceDeclaration[@Public='true']
194-
//ClassOrInterfaceBodyDeclaration
195-
/MethodDeclaration[@Public='true']
196-
/MethodDeclarator
187+
//ClassBody
188+
/*[@EffectiveVisibility='public']
197189
/FormalParameters
198190
/FormalParameter[
199-
./Type/ReferenceType and
191+
ClassType and
200192
not(
201-
./Annotation/MarkerAnnotation/Name[@Image='Nullable'] or
202-
./Annotation/MarkerAnnotation/Name[@Image='Nonnull']
203-
)]
204-
]]>
205-
</value>
206-
</property>
207-
</properties>
208-
<example>
209-
<![CDATA[
210-
public interface SomeType {
211-
// Okay
212-
void method();
213-
214-
// Okay
215-
void method( @Nullable Object parameter );
216-
217-
// Okay
218-
void method( @Nonnull Object parameter, int primitive );
219-
220-
// Nullability annotation missing
221-
void method( Object parameter );
222-
}
223-
]]>
224-
</example>
225-
</rule>
226-
<rule name="NullAnnotationMissingOnPublicConstructorParameter" language="java"
227-
message="Parameter type of public constructor is not annotated with @Nonnull or @Nullable."
228-
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
229-
<description>
230-
Parameter type of public constructor is not annotated with @Nonnull or @Nullable.
231-
This is beneficial to communicate a clear API contract and it allows for effective argument constraint validation in the IDE.
232-
</description>
233-
<priority>5</priority>
234-
<properties>
235-
<property name="xpath">
236-
<value>
237-
<![CDATA[
238-
//ClassOrInterfaceDeclaration[@Public='true']
239-
//ClassOrInterfaceBodyDeclaration
240-
/ConstructorDeclaration[@Public='true']
241-
/FormalParameters
242-
/FormalParameter[
243-
./Type/ReferenceType and
244-
not(
245-
./Annotation/MarkerAnnotation/Name[@Image='Nullable'] or
246-
./Annotation/MarkerAnnotation/Name[@Image='Nonnull']
193+
./ModifierList/Annotation[@SimpleName='Nullable'] or
194+
./ModifierList/Annotation[@SimpleName='Nonnull']
247195
)]
248196
]]>
249197
</value>
@@ -267,56 +215,39 @@
267215
]]>
268216
</example>
269217
</rule>
270-
<rule name="PrematureInstantiationInElseBlock" language="java"
271-
message="An instance is created in advance as part of an else block declaration."
272-
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
273-
<description>
274-
Do not unnecessarily compute code in advance, when it's not needed.
275-
Avoid premature instantiation.
276-
Instead of direct constructor call, use lambda notation or constructor reference, e.g. TargetType::new
277-
</description>
278-
<priority>2</priority>
279-
<properties>
280-
<property name="xpath">
281-
<value>
282-
<![CDATA[
283-
//PrimarySuffix[@Image='orElse' or @Image='getOrElse']
284-
/following-sibling::PrimarySuffix
285-
/Arguments
286-
/ArgumentList[count(./Expression)=1]
287-
/Expression
288-
/PrimaryExpression
289-
/*
290-
/AllocationExpression
291-
]]>
292-
</value>
293-
</property>
294-
</properties>
295-
</rule>
296218
<rule name="PrematureComputationOfElseBlock" language="java"
297219
message="Else block of fluent-API is computed in advance."
298220
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule">
299221
<description>
300222
Do not unnecessarily compute code in advance, when it's not needed.
301-
Instead of invoking the fallback function, reference it as lambda or function reference, e.g. this::fallback
223+
Avoid premature instantiation.
224+
Instead of direct constructor call, use lambda notation or constructor reference, e.g. TargetType::new
302225
</description>
303226
<priority>2</priority>
304227
<properties>
305228
<property name="xpath">
306229
<value>
307230
<![CDATA[
308-
//PrimarySuffix[@Image='orElse' or @Image='getOrElse']
309-
/following-sibling::PrimarySuffix[1]
310-
/Arguments
311-
/ArgumentList[count(./Expression)=1]
312-
/Expression
313-
/PrimaryExpression
314-
/*
315-
/Arguments
231+
//MethodCall[@MethodName='orElse' or @MethodName='getOrElse']
232+
/ArgumentList[not(LambdaExpression) and (.//MethodCall or .//ConstructorCall)]
316233
]]>
317234
</value>
318235
</property>
319236
</properties>
237+
<example>
238+
<![CDATA[
239+
public class SomeType {
240+
// Okay
241+
Option.of(variable).getOrElse("foo");
242+
Optional.of(variable).orElse(foo);
243+
Optional.of(variable).orElseGet(() -> "foo" + bar());
244+
245+
// Not okay
246+
Option.of(variable).getOrElse(bar());
247+
Optional.of(variable).orElse("foo" + bar());
248+
}
249+
]]>
250+
</example>
320251
</rule>
321252

322253
<!-- The following "performance"-related rules are taken from here: https://pmd.github.io/latest/pmd_rules_java_performance.html -->
@@ -425,11 +356,8 @@
425356
<![CDATA[
426357
//IfStatement[
427358
@Else=false() and
428-
./Expression/PrimaryExpression/PrimaryPrefix/Name[starts-with(@Image, "log.is") and ends-with(@Image, "Enabled")] and
429-
./Statement/Block[count(*)=1]/BlockStatement/Statement/StatementExpression/PrimaryExpression[
430-
./PrimaryPrefix/Name[starts-with(@Image, "log.")] and
431-
count(.//Arguments)=1
432-
]
359+
./MethodCall[starts-with(@MethodName, "is") and ends-with(@MethodName, "Enabled") and AmbiguousName[@Name="log"]] and
360+
./Block[@Size=1]/ExpressionStatement/MethodCall[AmbiguousName[@Name="log"] and not(.//MethodCall) and not(.//ConstructorCall)]
433361
]
434362
]]>
435363
</value>

datamodel/datamodel-metadata-generator/src/main/java/com/sap/cloud/sdk/datamodel/metadata/generator/MetadataApiSpecificUsage.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ static class Declaration
171171
@Nullable
172172
String value;
173173

174+
@Nonnull
175+
@Override
174176
public String toString()
175177
{
176178
final String typeUnqualified = getUnqualified(type);

datamodel/fluent-result/src/main/java/com/sap/cloud/sdk/result/ElementName.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.lang.annotation.RetentionPolicy;
1010
import java.lang.annotation.Target;
1111

12+
import javax.annotation.Nonnull;
13+
1214
/**
1315
* Annotation indicating the name of an element.
1416
*/
@@ -21,5 +23,6 @@
2123
*
2224
* @return The identifiable name of the field.
2325
*/
26+
@Nonnull
2427
String value();
2528
}

0 commit comments

Comments
 (0)