A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/commit/4cf51a6e6c2663c45cc190f4da35504ff7671adf below:

[java] FinalFieldCouldBeStatic - consider compile time array initiali… · pmd/pmd@4cf51a6 · GitHub

File tree Expand file treeCollapse file tree 3 files changed

+36

-3

lines changed

Filter options

Expand file treeCollapse file tree 3 files changed

+36

-3

lines changed Original file line number Diff line number Diff line change

@@ -66,6 +66,21 @@ public Object visit(ASTFieldAccess node, Void data) {

66 66

return null;

67 67

}

68 68 69 +

@Override

70 +

public Object visit(ASTArrayInitializer node, Void data) {

71 +

int length = node.length();

72 +

Object[] result = new Object[length];

73 +

int index = 0;

74 +

for (ASTExpression expr : node) {

75 +

if (!expr.isCompileTimeConstant()) {

76 +

return null;

77 +

}

78 +

result[index++] = expr.getConstValue();

79 +

}

80 + 81 +

return result;

82 +

}

83 + 69 84

@Override

70 85

public Object visit(ASTConditionalExpression node, Void data) {

71 86

Object condition = node.getCondition().getConstValue();

Original file line number Diff line number Diff line change

@@ -11,12 +11,14 @@

11 11

import net.sourceforge.pmd.lang.java.ast.ASTBodyDeclaration;

12 12

import net.sourceforge.pmd.lang.java.ast.ASTClassLiteral;

13 13

import net.sourceforge.pmd.lang.java.ast.ASTExpression;

14 +

import net.sourceforge.pmd.lang.java.ast.ASTFieldAccess;

14 15

import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;

15 16

import net.sourceforge.pmd.lang.java.ast.ASTInfixExpression;

16 17

import net.sourceforge.pmd.lang.java.ast.ASTInitializer;

17 18

import net.sourceforge.pmd.lang.java.ast.ASTLiteral;

18 19

import net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement;

19 20

import net.sourceforge.pmd.lang.java.ast.ASTTypeExpression;

21 +

import net.sourceforge.pmd.lang.java.ast.ASTVariableAccess;

20 22

import net.sourceforge.pmd.lang.java.ast.ASTVariableId;

21 23

import net.sourceforge.pmd.lang.java.ast.JModifier;

22 24

import net.sourceforge.pmd.lang.java.ast.ModifierOwner;

@@ -50,8 +52,19 @@ public Object visit(ASTFieldDeclaration node, Object data) {

50 52

}

51 53 52 54

private boolean isAllowedExpression(ASTExpression e) {

53 -

if (e instanceof ASTLiteral || e instanceof ASTClassLiteral || e instanceof ASTTypeExpression || e.getConstValue() != null) {

55 +

if (e instanceof ASTLiteral || e instanceof ASTClassLiteral || e instanceof ASTTypeExpression || e.isCompileTimeConstant()) {

54 56

return true;

57 +

} else if (e instanceof ASTFieldAccess

58 +

&& ((ASTFieldAccess) e).getName().equals("length")

59 +

&& ((ASTFieldAccess) e).getQualifier().getTypeMirror().isArray()) {

60 +

JVariableSymbol arrayDeclarationSymbol = ((ASTVariableAccess) ((ASTFieldAccess) e).getQualifier()).getReferencedSym();

61 +

if (arrayDeclarationSymbol != null

62 +

&& arrayDeclarationSymbol.isField()

63 +

&& ((JFieldSymbol) arrayDeclarationSymbol).isStatic()

64 +

&& arrayDeclarationSymbol.tryGetNode() != null) {

65 +

ASTVariableId arrayVarId = arrayDeclarationSymbol.tryGetNode();

66 +

return arrayVarId != null && arrayVarId.getInitializer() != null && arrayVarId.getInitializer().isCompileTimeConstant();

67 +

}

55 68

} else if (e instanceof ASTNamedReferenceExpr) {

56 69

JVariableSymbol sym = ((ASTNamedReferenceExpr) e).getReferencedSym();

57 70

return sym != null && sym.isField() && ((JFieldSymbol) sym).isStatic();

Original file line number Diff line number Diff line change

@@ -357,12 +357,17 @@ class Literals {

357 357 358 358

<test-code>

359 359

<description>Static array property length</description>

360 -

<expected-problems>1</expected-problems>

361 -

<expected-linenumbers>3</expected-linenumbers>

360 +

<expected-problems>2</expected-problems>

361 +

<expected-linenumbers>3,5</expected-linenumbers>

362 362

<code><![CDATA[

363 363

class ArrayLength {

364 364

private static final int[] myArray = { 1, 2, 3 };

365 365

public final int myArrayLength = myArray.length;

366 + 367 +

private final int[] nonStaticArray = { 1, 2, 3 };

368 +

public final int nonStaticArrayLength = nonStaticArray.length; // can't be made static, unless nonStaticArray is static as well

369 + 370 +

private static final int[] emptyArray = {};

366 371

}

367 372

]]></code>

368 373

</test-code>

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4