/** * Returns true if the name could be imported by this declaration. * The name must be fully qualified, the import is either on-demand * or static, that is its {@link ASTImportDeclaration#getImportedName()} * is the enclosing package or type name of the imported type or static member. */ private boolean declarationMatches(ASTImportDeclaration decl, String name) { return name.startsWith(decl.getImportedName()) && name.lastIndexOf('.') == decl.getImportedName().length(); }
@Override public Object visit(ASTImportDeclaration node, Object data) { ImportWrapper wrapper = new ImportWrapper(node.getImportedName(), node.getImportedName(), node.getImportedNameNode(), node.isStatic() && node.isImportOnDemand()); // blahhhh... this really wants to be ASTImportDeclaration to be // polymorphic... if (node.isImportOnDemand()) { if (importOnDemandImports.contains(wrapper)) { addViolation(data, node.getImportedNameNode(), node.getImportedNameNode().getImage()); } else { importOnDemandImports.add(wrapper); } } else { if (singleTypeImports.contains(wrapper)) { addViolation(data, node.getImportedNameNode(), node.getImportedNameNode().getImage()); } else { singleTypeImports.add(wrapper); } } return data; }
/** * Returns the simple name of the type or method imported by this declaration. * For on-demand imports, returns {@code null}. */ public String getImportedSimpleName() { if (isImportOnDemand) { return null; } String importName = getImportedName(); return importName.substring(importName.lastIndexOf('.') + 1); }
private boolean isTestNgClass(ASTCompilationUnit node) { List<ASTImportDeclaration> imports = node.findDescendantsOfType(ASTImportDeclaration.class); for (ASTImportDeclaration i : imports) { if (i.getImportedName() != null && i.getImportedName().startsWith("org.testng")) { return true; } } return false; }
@Override public Object visit(ASTImportDeclaration decl, Object data) { // If the import actually import a Boolean class that overrides // java.lang.Boolean if (decl.getImportedName().endsWith("Boolean") && !decl.getImportedName().equals("java.lang")) { customBoolean = true; } return super.visit(decl, data); }
/** * Returns the "package" prefix of the imported name. For type imports, including on-demand * imports, this is really the package name of the imported type(s). For static imports, * this is actually the qualified name of the enclosing type, including the type name. */ public String getPackageName() { String importName = getImportedName(); if (isImportOnDemand) { return importName; } if (importName.indexOf('.') == -1) { return ""; } int lastDot = importName.lastIndexOf('.'); return importName.substring(0, lastDot); }
@Override public Object visit(ASTImportDeclaration node, Object o) { if (node.getImportedName().indexOf("junit") != -1) { junitImported = true; } return super.visit(node, o); }
protected boolean isAllowedClass(ASTImportDeclaration node) { String importedName = node.getImportedName(); for (String clazz : getProperty(CLASSES_DESCRIPTOR)) { if (importedName.equals(clazz)) { return true; } } return false; }
@Override public Object visit(ASTImportDeclaration node, Object data) { // Is there any imported types that match ? for (Pattern pattern : this.typesMatch) { if (RegexHelper.isMatch(pattern, node.getImportedName())) { if (simpleClassname == null) { simpleClassname = new ArrayList<>(1); } simpleClassname.add(node.getImportedName()); } // FIXME: use type resolution framework to deal with star import ? } return super.visit(node, data); }
/** * Configures the type resolution for the symbol table. * * @param imports * the import declarations */ public void configureImports(final List<ASTImportDeclaration> imports) { for (ASTImportDeclaration i : imports) { if (i.isImportOnDemand()) { types.addImport(i.getImportedName() + ".*"); } else { types.addImport(i.getImportedName()); } } }
importOnDemandStaticClasses.add(JavaTypeDefinition.forClass(loadClass(strPackage))); } else { // not import on-demand String strName = anImportDeclaration.getImportedName(); String fieldName = strName.substring(strName.lastIndexOf('.') + 1); importedOnDemand.add(strPackage); } else { // not import on-demand String strName = anImportDeclaration.getImportedName(); importedClasses.put(strName, strName); importedClasses.put(strName.substring(strPackage.length() + 1), strName);
if (declarationMatches(firstMatch, importDeclaration.getImportedName())) { } else if (importDeclaration.getImportedName().endsWith(methodCalled)) { return true; String importName = firstMatch.getImportedName(); String importUnqualified = importName.substring(importName.lastIndexOf('.') + 1); if (!firstMatch.isImportOnDemand() && !firstMatch.isStatic()) { if (!firstMatch.getImportedName().equals(name) && importUnqualified.equals(unqualifiedName)) { return true;
@Override public Object visit(ASTImportDeclaration node, Object data) { String importPackage = node.getPackageName(); // Check each restricted package for (String pkg : getRestrictedPackages()) { // Is this import restricted? Use the deepest sub-package which // restricts this import. if (isContainingPackage(pkg, importPackage)) { // Is this source in a sub-package of restricted package? if (pkg.equals(thisPackage) || isContainingPackage(pkg, thisPackage)) { // Valid usage break; } else { // On demand imports automatically fail because they include // everything if (node.isImportOnDemand()) { addViolation(data, node, new Object[] { node.getImportedName(), pkg }); break; } else { if (!isAllowedClass(node)) { addViolation(data, node, new Object[] { node.getImportedName(), pkg }); break; } } } } } return data; }
if (!importDeclaration.isImportOnDemand()) { if (name.equals(importDeclaration.getImportedName())) { matches.add(importDeclaration); continue; String[] importParts = importDeclaration.getImportedName().split("\\."); String[] nameParts = name.split("\\."); if (importDeclaration.isStatic()) { String importStr = firstMatch.getImportedName() + (firstMatch.isImportOnDemand() ? ".*" : ""); String type = firstMatch.isStatic() ? "static " : "";
/** * Returns true if the name could be imported by this declaration. * The name must be fully qualified, the import is either on-demand * or static, that is its {@link ASTImportDeclaration#getImportedName()} * is the enclosing package or type name of the imported type or static member. */ private boolean declarationMatches(ASTImportDeclaration decl, String name) { return name.startsWith(decl.getImportedName()) && name.lastIndexOf('.') == decl.getImportedName().length(); }
/** * Returns the simple name of the type or method imported by this declaration. * For on-demand imports, returns {@code null}. */ public String getImportedSimpleName() { if (isImportOnDemand) { return null; } String importName = getImportedName(); return importName.substring(importName.lastIndexOf('.') + 1); }
private boolean isTestNgClass(ASTCompilationUnit node) { List<ASTImportDeclaration> imports = node.findDescendantsOfType(ASTImportDeclaration.class); for (ASTImportDeclaration i : imports) { if (i.getImportedName() != null && i.getImportedName().startsWith("org.testng")) { return true; } } return false; }
@Override public Object visit(ASTImportDeclaration node, Object o) { if (node.getImportedName().indexOf("junit") != -1) { junitImported = true; } return super.visit(node, o); }
protected boolean isAllowedClass(ASTImportDeclaration node) { String importedName = node.getImportedName(); for (String clazz : getProperty(CLASSES_DESCRIPTOR)) { if (importedName.equals(clazz)) { return true; } } return false; }
@Override public Object visit(final ASTImportDeclaration imp, final Object data) { for (final String element : ProhibitPlainJunitAssertionsRule .PROHIBITED) { if (imp.getImportedName().contains(element)) { this.addViolation(data, imp); break; } } return super.visit(imp, data); }