/** * Get name of class (with qualified package if specified) in {@code ast}. * @param ast ast to extract class name from * @return qualified name */ private static String extractQualifiedName(DetailAST ast) { return FullIdent.createFullIdent(ast).getText(); }
/** * Collects the details of imports. * @param ast node containing the import details */ private void processImport(DetailAST ast) { final FullIdent name = FullIdent.createFullIdentBelow(ast); imports.add(name.getText()); }
/** * Converts FullIdent to Token. * @param fullIdent full ident to convert. */ public Token(FullIdent fullIdent) { text = fullIdent.getText(); lineNo = fullIdent.getLineNo(); columnNo = fullIdent.getColumnNo(); }
/** * Returns import text. * @param ast ast node that represents import * @return String that represents importing class */ private static String getImportText(DetailAST ast) { final FullIdent imp; if (ast.getType() == TokenTypes.IMPORT) { imp = FullIdent.createFullIdentBelow(ast); } else { // know it is a static import imp = FullIdent.createFullIdent(ast .getFirstChild().getNextSibling()); } return imp.getText(); }
/** * Checks the identifier of the given type. * @param type node to check. */ private void checkIdent(DetailAST type) { final FullIdent ident = FullIdent.createFullIdent(type); if (isMatchingClassName(ident.getText())) { log(ident.getDetailAst(), MSG_KEY, ident.getText()); } }
@Override public void visitToken(DetailAST ast) { if (ast.getType() == TokenTypes.PACKAGE_DEF) { pkgName = FullIdent.createFullIdent( ast.getLastChild().getPreviousSibling()).getText(); final FullIdent imp = FullIdent.createFullIdentBelow(ast); if (isFromPackage(imp.getText(), "java.lang")) { log(ast, MSG_LANG, imp.getText()); else if (pkgName != null && isFromPackage(imp.getText(), pkgName)) { log(ast, MSG_SAME, imp.getText()); imports.stream().filter(full -> imp.getText().equals(full.getText())) .forEach(full -> log(ast, MSG_DUPLICATE, full.getLineNo(), imp.getText())); FullIdent.createFullIdent( ast.getLastChild().getPreviousSibling()); staticImports.stream().filter(full -> imp.getText().equals(full.getText())) .forEach(full -> log(ast, MSG_DUPLICATE, full.getLineNo(), imp.getText()));
@Override public void beginTree(DetailAST rootAST) { packageName = FullIdent.createFullIdent(null); currentClass = null; classDepth = 0; }
@Override public void visitToken(DetailAST ast) { defined = true; if (matchDirectoryStructure) { final DetailAST packageNameAst = ast.getLastChild().getPreviousSibling(); final FullIdent fullIdent = FullIdent.createFullIdent(packageNameAst); final String packageName = fullIdent.getText().replace('.', File.separatorChar); final String directoryName = getDirectoryName(); if (!directoryName.endsWith(packageName)) { log(fullIdent.getLineNo(), MSG_KEY_MISMATCH, packageName); } } }
/** * Check import statements. * @param className name of the class * @return value of illegal instantiated type */ private String checkImportStatements(String className) { String illegalType = null; // import statements for (FullIdent importLineText : imports) { String importArg = importLineText.getText(); if (importArg.endsWith(".*")) { importArg = importArg.substring(0, importArg.length() - 1) + className; } if (CommonUtil.baseClassName(importArg).equals(className) && classes.contains(importArg)) { illegalType = importArg; break; } } return illegalType; }
ident = FullIdent.createFullIdentBelow(ast); isStatic = false; ident = FullIdent.createFullIdent(ast.getFirstChild() .getNextSibling()); isStatic = true;
@Override public void finishTree(DetailAST rootAST) { // loop over all the imports to see if referenced. imports.stream() .filter(imprt -> isUnusedImport(imprt.getText())) .forEach(imprt -> log(imprt.getDetailAst(), MSG_KEY, imprt.getText())); }
@Test public void testGetDetailAst() throws Exception { final FileText testFileText = new FileText( new File(getPath("InputFullIdentTestArrayType.java")).getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); final DetailAST packageDefinitionNode = JavaParser.parse(new FileContents(testFileText)); final DetailAST packageName = packageDefinitionNode.getFirstChild().getNextSibling(); final FullIdent ident = FullIdent.createFullIdent(packageName); Assert.assertEquals("Invalid full indent", "com[1x8]", ident.getDetailAst().toString()); }
/** * Perform processing for an import token. * @param ast the import token */ private void processImport(DetailAST ast) { final FullIdent name = FullIdent.createFullIdentBelow(ast); // Note: different from UnusedImportsCheck.processImport(), // '.*' imports are also added here imports.add(name); }
@Test public void testWithArrayCreateFullIdentWithArrayDeclare() throws Exception { final FileText testFileText = new FileText( new File(getPath("InputFullIdentTestArrayType.java")).getAbsoluteFile(), System.getProperty("file.encoding", StandardCharsets.UTF_8.name())); final DetailAST packageDefinitionNode = JavaParser.parse(new FileContents(testFileText)); final DetailAST arrayDeclarator = packageDefinitionNode.getNextSibling() .findFirstToken(TokenTypes.OBJBLOCK) .findFirstToken(TokenTypes.VARIABLE_DEF) .findFirstToken(TokenTypes.TYPE) .getFirstChild(); final FullIdent ident = FullIdent.createFullIdent(arrayDeclarator); Assert.assertEquals("Invalid full indent", "int[][][5x12]", ident.toString()); }
@Override public void visitToken(DetailAST ast) { final FullIdent imp; if (ast.getType() == TokenTypes.IMPORT) { imp = FullIdent.createFullIdentBelow(ast); } else { imp = FullIdent.createFullIdent( ast.getFirstChild().getNextSibling()); } if (isIllegalImport(imp.getText())) { log(ast, MSG_KEY, imp.getText()); } }
@Override public void visitToken(DetailAST ast) { final DetailAST nameAST = ast.getLastChild().getPreviousSibling(); final FullIdent full = FullIdent.createFullIdent(nameAST); if (!format.matcher(full.getText()).find()) { log(full.getDetailAst(), MSG_KEY, full.getText(), format.pattern()); } }
@Override public void visitToken(DetailAST ast) { if (ast.getType() == TokenTypes.PACKAGE_DEF) { pkgName = FullIdent.createFullIdent( ast.getLastChild().getPreviousSibling()).getText(); final FullIdent imp = FullIdent.createFullIdentBelow(ast); if (isFromPackage(imp.getText(), "java.lang")) { log(ast, MSG_LANG, imp.getText()); else if (pkgName != null && isFromPackage(imp.getText(), pkgName)) { log(ast, MSG_SAME, imp.getText()); imports.stream().filter(full -> imp.getText().equals(full.getText())) .forEach(full -> log(ast, MSG_DUPLICATE, full.getLineNo(), imp.getText())); FullIdent.createFullIdent( ast.getLastChild().getPreviousSibling()); staticImports.stream().filter(full -> imp.getText().equals(full.getText())) .forEach(full -> log(ast, MSG_DUPLICATE, full.getLineNo(), imp.getText()));
@Override public void beginTree(DetailAST rootAST) { packageFullIdent = FullIdent.createFullIdent(null); imports.clear(); // add java.lang.* since it's always imported imports.add("java.lang.*"); classResolver = null; currentClassName = ""; typeParams.clear(); }
@Override public void visitToken(DetailAST ast) { defined = true; if (matchDirectoryStructure) { final DetailAST packageNameAst = ast.getLastChild().getPreviousSibling(); final FullIdent fullIdent = FullIdent.createFullIdent(packageNameAst); final String packageName = fullIdent.getText().replace('.', File.separatorChar); final String directoryName = getDirectoryName(); if (!directoryName.endsWith(packageName)) { log(fullIdent.getLineNo(), MSG_KEY_MISMATCH, packageName); } } }
/** * Visits type. * @param ast type to process. */ public void visitType(DetailAST ast) { final String fullTypeName = CheckUtil.createFullType(ast).getText(); addReferencedClassName(fullTypeName); }