private static boolean onlySynchronizedUsages(Symbol.VariableSymbol variable) { List<IdentifierTree> usages = variable.usages(); if (usages.isEmpty()) { return false; } for (IdentifierTree usage : usages) { SynchronizedStatementTree synchronizedStatementTree = getParentSynchronizedStatement(usage); if (synchronizedStatementTree == null) { // used outside a synchronized statement return false; } else { ExpressionTree expression = synchronizedStatementTree.expression(); if (!expression.is(Tree.Kind.IDENTIFIER) || !variable.equals(((IdentifierTree) expression).symbol())) { // variable is not the expression synchronized return false; } // check other usages } } return true; }
private static boolean onlySynchronizedUsages(Symbol.VariableSymbol variable) { List<IdentifierTree> usages = variable.usages(); if (usages.isEmpty()) { return false; } for (IdentifierTree usage : usages) { SynchronizedStatementTree synchronizedStatementTree = getParentSynchronizedStatement(usage); if (synchronizedStatementTree == null) { // used outside a synchronized statement return false; } else { ExpressionTree expression = synchronizedStatementTree.expression(); if (!expression.is(Tree.Kind.IDENTIFIER) || !variable.equals(((IdentifierTree) expression).symbol())) { // variable is not the expression synchronized return false; } // check other usages } } return true; }
private static boolean isImmutableFinalVariable(Symbol.VariableSymbol symbol) { if (symbol.isFinal()) { VariableTree declaration = symbol.declaration(); // symbol is private, so declaration can only be null if assignment is done in static block ExpressionTree initializer = declaration.initializer(); if (initializer != null) { return !isMutableType(initializer) || isEmptyArray(initializer); } return !assignementsOfMutableType(symbol.usages()); } return false; }
private static boolean isImmutableFinalVariable(Symbol.VariableSymbol symbol) { if (symbol.isFinal()) { VariableTree declaration = symbol.declaration(); // symbol is private, so declaration can only be null if assignment is done in static block ExpressionTree initializer = declaration.initializer(); if (initializer != null) { return !isMutableType(initializer) || isEmptyArray(initializer); } return !assignementsOfMutableType(symbol.usages()); } return false; }