/** * Find frame containing declaration. * @param name IDENT ast of the declaration to find. * @param lookForMethod whether we are looking for a method name. * @return AbstractFrame containing declaration or null. */ private AbstractFrame findFrame(DetailAST name, boolean lookForMethod) { return findFrame(current.peek(), name, lookForMethod); }
/** * Checks whether a value can be assigned to a field. * A value can be assigned to a final field only in constructor block. If there is a method * block, value assignment can be performed only to non final field. * @param ast an identifier token. * @return true if a value can be assigned to a field. */ private boolean canAssignValueToClassField(DetailAST ast) { final AbstractFrame fieldUsageFrame = findFrame(ast, false); final boolean fieldUsageInConstructor = isInsideConstructorFrame(fieldUsageFrame); final AbstractFrame declarationFrame = findFrame(ast, true); final boolean finalField = ((ClassFrame) declarationFrame).hasFinalField(ast); return fieldUsageInConstructor || !finalField; }
/** * Find the class frame containing declaration. * @param name IDENT ast of the declaration to find. * @param lookForMethod whether we are looking for a method name. * @return AbstractFrame containing declaration or null. */ private AbstractFrame findClassFrame(DetailAST name, boolean lookForMethod) { AbstractFrame frame = current.peek(); while (true) { frame = findFrame(frame, name, lookForMethod); if (frame == null || frame instanceof ClassFrame) { break; } frame = frame.getParent(); } return frame; }
/** * Returns the frame where the method is declared, if the given method is used without * 'this' and null otherwise. * @param ast the IDENT ast of the name to check. * @return the frame where the method is declared, if the given method is used without * 'this' and null otherwise. */ private AbstractFrame getMethodWithoutThis(DetailAST ast) { AbstractFrame result = null; if (!validateOnlyOverlapping) { final AbstractFrame frame = findFrame(ast, true); if (frame != null && ((ClassFrame) frame).hasInstanceMethod(ast) && !((ClassFrame) frame).hasStaticMethod(ast)) { result = frame; } } return result; }
/** * Checks whether an overlapping by local variable takes place. * @param ast an identifier. * @return true if an overlapping by local variable takes place. */ private boolean isOverlappingByLocalVariable(DetailAST ast) { boolean overlapping = false; final DetailAST parent = ast.getParent(); final DetailAST sibling = ast.getNextSibling(); if (sibling != null && isAssignToken(parent.getType())) { final ClassFrame classFrame = (ClassFrame) findFrame(ast, true); final Set<DetailAST> exprIdents = getAllTokensOfType(sibling, TokenTypes.IDENT); overlapping = classFrame.containsFieldOrVariableDef(exprIdents, ast); } return overlapping; }
final AbstractFrame variableDeclarationFrame = findFrame(ast, false); final FrameType variableDeclarationFrameType = variableDeclarationFrame.getType(); final DetailAST prevSibling = ast.getPreviousSibling(); && canBeReferencedFromStaticContext(ast) && canAssignValueToClassField(ast)) { frameWhereViolationIsFound = findFrame(ast, true); && canBeReferencedFromStaticContext(ast) && canAssignValueToClassField(ast)) { frameWhereViolationIsFound = findFrame(ast, true); && isOverlappingByArgument(ast) && !isUserDefinedArrangementOfThis(variableDeclarationFrame, ast)) { frameWhereViolationIsFound = findFrame(ast, true); && !isReturnedVariable(variableDeclarationFrame, ast) && canBeReferencedFromStaticContext(ast)) { frameWhereViolationIsFound = findFrame(ast, true);
/** * Checks whether an overlapping by method or constructor argument takes place. * @param ast an identifier. * @return true if an overlapping by method or constructor argument takes place. */ private boolean isOverlappingByArgument(DetailAST ast) { boolean overlapping = false; final DetailAST parent = ast.getParent(); final DetailAST sibling = ast.getNextSibling(); if (sibling != null && isAssignToken(parent.getType())) { if (isCompoundAssignToken(parent.getType())) { overlapping = true; } else { final ClassFrame classFrame = (ClassFrame) findFrame(ast, true); final Set<DetailAST> exprIdents = getAllTokensOfType(sibling, TokenTypes.IDENT); overlapping = classFrame.containsFieldOrVariableDef(exprIdents, ast); } } return overlapping; }
AbstractFrame variableDeclarationFrame = findFrame(ident, false); boolean staticInitializationBlock = false; while (variableDeclarationFrame.getType() == FrameType.BLOCK_FRAME
/** * Find frame containing declaration. * @param name IDENT ast of the declaration to find. * @param lookForMethod whether we are looking for a method name. * @return AbstractFrame containing declaration or null. */ private AbstractFrame findFrame(DetailAST name, boolean lookForMethod) { return findFrame(current.peek(), name, lookForMethod); }
/** * Checks whether a value can be assigned to a field. * A value can be assigned to a final field only in constructor block. If there is a method * block, value assignment can be performed only to non final field. * @param ast an identifier token. * @return true if a value can be assigned to a field. */ private boolean canAssignValueToClassField(DetailAST ast) { final AbstractFrame fieldUsageFrame = findFrame(ast, false); final boolean fieldUsageInConstructor = isInsideConstructorFrame(fieldUsageFrame); final AbstractFrame declarationFrame = findFrame(ast, true); final boolean finalField = ((ClassFrame) declarationFrame).hasFinalField(ast); return fieldUsageInConstructor || !finalField; }
/** * Find the class frame containing declaration. * @param name IDENT ast of the declaration to find. * @param lookForMethod whether we are looking for a method name. * @return AbstractFrame containing declaration or null. */ private AbstractFrame findClassFrame(DetailAST name, boolean lookForMethod) { AbstractFrame frame = current.peek(); while (true) { frame = findFrame(frame, name, lookForMethod); if (frame == null || frame instanceof ClassFrame) { break; } frame = frame.getParent(); } return frame; }
/** * Returns the frame where the method is declared, if the given method is used without * 'this' and null otherwise. * @param ast the IDENT ast of the name to check. * @return the frame where the method is declared, if the given method is used without * 'this' and null otherwise. */ private AbstractFrame getMethodWithoutThis(DetailAST ast) { AbstractFrame result = null; if (!validateOnlyOverlapping) { final AbstractFrame frame = findFrame(ast, true); if (frame != null && ((ClassFrame) frame).hasInstanceMethod(ast) && !((ClassFrame) frame).hasStaticMethod(ast)) { result = frame; } } return result; }
/** * Checks whether an overlapping by local variable takes place. * @param ast an identifier. * @return true if an overlapping by local variable takes place. */ private boolean isOverlappingByLocalVariable(DetailAST ast) { boolean overlapping = false; final DetailAST parent = ast.getParent(); final DetailAST sibling = ast.getNextSibling(); if (sibling != null && isAssignToken(parent.getType())) { final ClassFrame classFrame = (ClassFrame) findFrame(ast, true); final Set<DetailAST> exprIdents = getAllTokensOfType(sibling, TokenTypes.IDENT); overlapping = classFrame.containsFieldOrVariableDef(exprIdents, ast); } return overlapping; }
final AbstractFrame variableDeclarationFrame = findFrame(ast, false); final FrameType variableDeclarationFrameType = variableDeclarationFrame.getType(); final DetailAST prevSibling = ast.getPreviousSibling(); && canBeReferencedFromStaticContext(ast) && canAssignValueToClassField(ast)) { frameWhereViolationIsFound = findFrame(ast, true); && canBeReferencedFromStaticContext(ast) && canAssignValueToClassField(ast)) { frameWhereViolationIsFound = findFrame(ast, true); && isOverlappingByArgument(ast) && !isUserDefinedArrangementOfThis(variableDeclarationFrame, ast)) { frameWhereViolationIsFound = findFrame(ast, true); && !isReturnedVariable(variableDeclarationFrame, ast) && canBeReferencedFromStaticContext(ast)) { frameWhereViolationIsFound = findFrame(ast, true);
/** * Checks whether an overlapping by method or constructor argument takes place. * @param ast an identifier. * @return true if an overlapping by method or constructor argument takes place. */ private boolean isOverlappingByArgument(DetailAST ast) { boolean overlapping = false; final DetailAST parent = ast.getParent(); final DetailAST sibling = ast.getNextSibling(); if (sibling != null && isAssignToken(parent.getType())) { if (isCompoundAssignToken(parent.getType())) { overlapping = true; } else { final ClassFrame classFrame = (ClassFrame) findFrame(ast, true); final Set<DetailAST> exprIdents = getAllTokensOfType(sibling, TokenTypes.IDENT); overlapping = classFrame.containsFieldOrVariableDef(exprIdents, ast); } } return overlapping; }
AbstractFrame variableDeclarationFrame = findFrame(ident, false); boolean staticInitializationBlock = false; while (variableDeclarationFrame.getType() == FrameType.BLOCK_FRAME