public static boolean importsPackage(ASTCompilationUnit node, String packageName) { List<ASTImportDeclaration> nodes = node.findChildrenOfType(ASTImportDeclaration.class); for (ASTImportDeclaration n : nodes) { if (n.getPackageName().startsWith(packageName)) { return true; } } return false; }
@Override public Object visit(ASTImportDeclaration importDecl, Object data) { String packageName = importDecl.getScope().getEnclosingScope(SourceFileScope.class).getPackageName(); if (packageName != null && packageName.equals(importDecl.getPackageName())) { addViolation(data, importDecl); } // special case if (packageName == null && StringUtils.isBlank(importDecl.getPackageName())) { addViolation(data, importDecl); } return data; } }
String strPackage = anImportDeclaration.getPackageName(); if (anImportDeclaration.isStatic()) { if (anImportDeclaration.isImportOnDemand()) {
if (!importDeclaration.getPackageName().equals(firstMatch.getPackageName()) && importDeclaration.getImportedSimpleName().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; }
public static boolean importsPackage(ASTCompilationUnit node, String packageName) { List<ASTImportDeclaration> nodes = node.findChildrenOfType(ASTImportDeclaration.class); for (ASTImportDeclaration n : nodes) { if (n.getPackageName().startsWith(packageName)) { return true; } } return false; }
@Override public Object visit(ASTImportDeclaration importDecl, Object data) { String packageName = importDecl.getScope().getEnclosingScope(SourceFileScope.class).getPackageName(); if (packageName != null && packageName.equals(importDecl.getPackageName())) { addViolation(data, importDecl); } // special case if (packageName == null && StringUtils.isBlank(importDecl.getPackageName())) { addViolation(data, importDecl); } return data; } }
/** * If the outer class wasn't found then we'll get in here */ private void populateImports(ASTCompilationUnit node) { List<ASTImportDeclaration> theImportDeclarations = node.findChildrenOfType(ASTImportDeclaration.class); importedClasses.putAll(JAVA_LANG); // go through the imports for (ASTImportDeclaration anImportDeclaration : theImportDeclarations) { String strPackage = anImportDeclaration.getPackageName(); if (anImportDeclaration.isImportOnDemand()) { importedOnDemand.add(strPackage); } else if (!anImportDeclaration.isImportOnDemand()) { String strName = anImportDeclaration.getImportedName(); importedClasses.put(strName, strName); importedClasses.put(strName.substring(strPackage.length() + 1), strName); } } }
String strPackage = anImportDeclaration.getPackageName(); if (anImportDeclaration.isStatic()) { if (anImportDeclaration.isImportOnDemand()) {
if (!importDeclaration.getPackageName().equals(firstMatch.getPackageName()) && importDeclaration.getImportedSimpleName().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; }