if (firstMatch.isImportOnDemand() && firstMatch.isStatic()) { final String methodCalled = name.substring(name.indexOf('.') + 1); if (!Objects.equals(importDeclaration, firstMatch) && importDeclaration.isStatic()) { if (declarationMatches(firstMatch, importDeclaration.getImportedName())) { if (importDeclaration.isImportOnDemand()) { if (importDeclaration.getType() != null) { for (final Method m : importDeclaration.getType().getMethods()) { if (m.getName().equals(methodCalled)) { return true; } else if (importDeclaration.getImportedName().endsWith(methodCalled)) { return true; if (firstMatch.isImportOnDemand() && !firstMatch.isStatic()) { for (ASTImportDeclaration importDeclaration : imports) { if (importDeclaration != firstMatch // NOPMD && !importDeclaration.isStatic() && !importDeclaration.isImportOnDemand()) { if (!importDeclaration.getPackageName().equals(firstMatch.getPackageName()) && importDeclaration.getImportedSimpleName().equals(unqualifiedName)) { return true; String importName = firstMatch.getImportedName(); String importUnqualified = importName.substring(importName.lastIndexOf('.') + 1); if (!firstMatch.isImportOnDemand() && !firstMatch.isStatic()) {
private boolean hasImports(ASTCompilationUnit cu, String className) { List<ASTImportDeclaration> imports = cu.findDescendantsOfType(ASTImportDeclaration.class); for (ASTImportDeclaration importDeclaration : imports) { ASTName name = importDeclaration.getFirstChildOfType(ASTName.class); if (name != null && name.hasImageEqualTo(className)) { return true; } } return false; } }
@Override public Object visit(ASTImportDeclaration node, Object data) { ASTName importedType = (ASTName) node.jjtGetChild(0); if (importedType.getType() != null) { node.setType(importedType.getType()); } else { populateType(node, importedType.getImage()); } if (node.getType() != null) { node.setPackage(node.getType().getPackage()); } // no need to visit children, the only child, ASTName, will have no type return 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()); } } }
@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 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) { 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; }
String strPackage = anImportDeclaration.getPackageName(); if (anImportDeclaration.isStatic()) { if (anImportDeclaration.isImportOnDemand()) { importOnDemandStaticClasses.add(JavaTypeDefinition.forClass(loadClass(strPackage))); } else { // not import on-demand String strName = anImportDeclaration.getImportedName(); String fieldName = strName.substring(strName.lastIndexOf('.') + 1); if (anImportDeclaration.isImportOnDemand()) { importedOnDemand.add(strPackage); } else { // not import on-demand String strName = anImportDeclaration.getImportedName(); importedClasses.put(strName, strName); importedClasses.put(strName.substring(strPackage.length() + 1), strName);
@Override public Object visit(ASTImportDeclaration node, Object data) { if (node.isImportOnDemand()) { ASTName importedType = (ASTName) node.jjtGetChild(0); imports.add(new ImportWrapper(importedType.getImage(), null, node, node.getType(), node.isStatic())); } else { if (!node.isImportOnDemand()) { ASTName importedType = (ASTName) node.jjtGetChild(0); String className; if (isQualifiedName(importedType)) { int lastDot = importedType.getImage().lastIndexOf('.') + 1; className = importedType.getImage().substring(lastDot); } else { className = importedType.getImage(); } imports.add(new ImportWrapper(importedType.getImage(), className, node)); } } 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()) { if (importDeclaration.isImportOnDemand()) { } else if (!importDeclaration.isImportOnDemand()) { String importStr = firstMatch.getImportedName() + (firstMatch.isImportOnDemand() ? ".*" : ""); String type = firstMatch.isStatic() ? "static " : "";
@Override public Object visit(ASTImportDeclaration node, Object data) { if (node.isStatic()) { return data; } String img = node.jjtGetChild(0).getImage(); if (img.startsWith(IMPORT_JAVA_LANG)) { if (!IMPORT_JAVA_LANG.equals(img)) { if (img.indexOf('.', IMPORT_JAVA_LANG.length() + 1) != -1 || node.isImportOnDemand()) { // Importing from a subpackage / inner class return data; } } addViolation(data, node); } return data; } }
final public void ImportDeclaration() throws ParseException { ASTImportDeclaration jjtn000 = new ASTImportDeclaration(this, JJTIMPORTDECLARATION); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1)); try { jj_consume_token(IMPORT); case STATIC: jj_consume_token(STATIC); checkForBadStaticImportUsage();jjtn000.setStatic(); break; default: jj_consume_token(DOT); jj_consume_token(STAR); jjtn000.setImportOnDemand(); break; default: if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
public ASTName getImportedNameNode() { return (ASTName) jjtGetChild(0); }
@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 (((ASTImportDeclaration) node).isImportOnDemand()) { extras.add("on demand"); if (((ASTImportDeclaration) node).isStatic()) { extras.add("static");
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; }
/** * 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); }
/** * 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()) { importOnDemandStaticClasses.add(JavaTypeDefinition.forClass(loadClass(strPackage))); } else { // not import on-demand String strName = anImportDeclaration.getImportedName(); String fieldName = strName.substring(strName.lastIndexOf('.') + 1); if (anImportDeclaration.isImportOnDemand()) { importedOnDemand.add(strPackage); } else { // not import on-demand String strName = anImportDeclaration.getImportedName(); importedClasses.put(strName, strName); importedClasses.put(strName.substring(strPackage.length() + 1), strName);
@Override public Object visit(ASTImportDeclaration node, Object data) { if (node.isImportOnDemand()) { ASTName importedType = (ASTName) node.jjtGetChild(0); imports.add(new ImportWrapper(importedType.getImage(), null, node, node.getType(), node.isStatic())); } else { if (!node.isImportOnDemand()) { ASTName importedType = (ASTName) node.jjtGetChild(0); String className; if (isQualifiedName(importedType)) { int lastDot = importedType.getImage().lastIndexOf('.') + 1; className = importedType.getImage().substring(lastDot); } else { className = importedType.getImage(); } imports.add(new ImportWrapper(importedType.getImage(), className, node)); } } return data; }