@Override public void beginTree(DetailAST rootAST) { packageName = FullIdent.createFullIdent(null); currentClass = null; classDepth = 0; }
@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(); }
/** * 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(); }
/** * Creates a new FullIdent starting from the child of the specified node. * @param ast the parent node from where to start from * @return a {@code FullIdent} value */ public static FullIdent createFullIdentBelow(DetailAST ast) { return createFullIdent(ast.getFirstChild()); }
/** * Whether the type is java.lang.String. * @param typeAst the type to check. * @return true, if the type is java.lang.String. */ private static boolean isStringType(DetailAST typeAst) { final FullIdent type = FullIdent.createFullIdent(typeAst); return "String".equals(type.getText()) || "java.lang.String".equals(type.getText()); }
/** * Registers given import. This allows us to track imported classes. * @param imp import definition. */ public void registerImport(DetailAST imp) { final FullIdent ident = FullIdent.createFullIdent( imp.getLastChild().getPreviousSibling()); final String fullName = ident.getText(); final int lastDot = fullName.lastIndexOf(DOT); importedClassPackage.put(fullName.substring(lastDot + 1), fullName); }
/** * Sets current package. * @param packageDef node for package definition */ private void visitPackageDef(DetailAST packageDef) { packageName = FullIdent.createFullIdent(packageDef.getLastChild() .getPreviousSibling()); }
/** * Gets the full import identifier. If the import is a starred import and * it's not excluded then a violation is logged. * @param startingDot the starting dot for the import statement */ private void logsStarredImportViolation(DetailAST startingDot) { final FullIdent name = FullIdent.createFullIdent(startingDot); final String importText = name.getText(); if (importText.endsWith(STAR_IMPORT_SUFFIX) && !excludes.contains(importText)) { log(startingDot.getLineNo(), MSG_KEY, importText); } }
/** * Collects the details of a package. * @param ast node containing the package details */ private void processPackage(DetailAST ast) { final DetailAST nameAST = ast.getLastChild().getPreviousSibling(); packageFullIdent = FullIdent.createFullIdent(nameAST); }
/** * Collects the details of static imports. * @param ast node containing the static import details */ private void processStaticImport(DetailAST ast) { final FullIdent name = FullIdent.createFullIdent( ast.getFirstChild().getNextSibling()); if (!name.getText().endsWith(STAR_IMPORT_SUFFIX)) { imports.add(name); } }
/** * Adds new referenced class. * @param ast a node which represents referenced class. */ private void addReferencedClassName(DetailAST ast) { final String fullIdentName = FullIdent.createFullIdent(ast).getText(); addReferencedClassName(fullIdentName); }
/** * Forms import full path. * @param token * current token. * @return full path or null. */ private static String getFullImportIdent(DetailAST token) { String ident = ""; if (token != null) { ident = FullIdent.createFullIdent(token.findFirstToken(TokenTypes.DOT)).getText(); } return ident; }
/** * Perform processing for an package token. * @param ast the package token */ private void processPackageDef(DetailAST ast) { final DetailAST packageNameAST = ast.getLastChild() .getPreviousSibling(); final FullIdent packageIdent = FullIdent.createFullIdent(packageNameAST); pkgName = packageIdent.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()); } }
/** * Returns package text. * @param ast PACKAGE_DEF ast node * @return String that represents full package name */ private static String getPackageText(DetailAST ast) { final DetailAST nameAST = ast.getLastChild().getPreviousSibling(); return FullIdent.createFullIdent(nameAST).getText(); }
/** * Stores package of current class we check. * @param pkg package definition. */ private void visitPackageDef(DetailAST pkg) { final FullIdent ident = FullIdent.createFullIdent(pkg.getLastChild().getPreviousSibling()); fileContext.setPackageName(ident.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(final DetailAST ast) { final DetailAST startingDot = ast.getFirstChild().getNextSibling(); final FullIdent name = FullIdent.createFullIdent(startingDot); if (!isExempt(name.getText())) { log(startingDot.getLineNo(), MSG_KEY, name.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()); }
@Test public void testToString() { final DetailAST ast = new DetailAST(); ast.setType(TokenTypes.LITERAL_NEW); ast.setColumnNo(14); ast.setLineNo(15); ast.setText("MyTest"); final FullIdent indent = FullIdent.createFullIdent(ast); Assert.assertEquals("Invalid full indent", "MyTest[15x14]", indent.toString()); }