public <S extends SourceCode> S getParent(Class<S> sourceCode) { if (parent == null) { return null; } if (parent.getClass().equals(sourceCode)) { return (S) parent; } return parent.getParent(sourceCode); }
public <S extends SourceCode> S getAncestor(Class<S> withClass) { S ancestor = getParent(withClass); if (ancestor != null) { S parentAncestor = ancestor.getAncestor(withClass); if (parentAncestor != null) { ancestor = parentAncestor; } } return ancestor; }
@Override public void log(CheckMessage message) { if (peekSourceCode() instanceof SourceFile) { peekSourceCode().log(message); } else if (peekSourceCode().getParent(SourceFile.class) != null) { peekSourceCode().getParent(SourceFile.class).log(message); } else { throw new IllegalStateException("Unable to log a check message on source code '" + (peekSourceCode() == null ? "[NULL]" : peekSourceCode().getKey()) + "'"); } }
/** * {@inheritDoc} */ @Override public void log(CheckMessage message) { if (peekSourceCode() instanceof SourceFile) { peekSourceCode().log(message); } else if (peekSourceCode().getParent(SourceFile.class) != null) { peekSourceCode().getParent(SourceFile.class).log(message); } else { throw new IllegalStateException("Unable to log a check message on source code '" + (peekSourceCode() == null ? "[NULL]" : peekSourceCode().getKey()) + "'"); } }
@Override public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode) { AstNode identifier = astNode.getFirstChild(EcmaScriptTokenType.IDENTIFIER, EcmaScriptGrammar.PROPERTY_NAME, Kind.IDENTIFIER); final String functionName = identifier == null ? "anonymous" : identifier.getTokenValue(); final String fileKey = parentSourceCode.isType(SourceFile.class) ? parentSourceCode.getKey() : parentSourceCode.getParent(SourceFile.class).getKey(); SourceFunction function = new SourceFunction(fileKey + ":" + functionName + ":" + astNode.getToken().getLine() + ":" + astNode.getToken().getColumn()); function.setStartAtLine(astNode.getTokenLine()); return function; } }, FUNCTION_NODES));