Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2009 Future Invent Informationsmanagement GmbH. All rights reserved. <http://www.fuin.org/> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see <http://www.gnu.org/licenses/>.
 
 package org.fuin.units4j.dependency;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.objectweb.asm.ClassReader;

Analyzes package dependencies.
 
 public final class DependencyAnalyzer {
 
     private final List<DependencyErrordependencyErrors;
 
     private final Dependencies dependencies;

    
Constructor with XML file.

Parameters:
dependenciesFile XML file with allowed or forbidden dependencies - Cannot be null.
Throws:
InvalidDependenciesDefinitionException Formal correct (XML) but invalid dependency definition.
 
     public DependencyAnalyzer(final File dependenciesFile)
             throws InvalidDependenciesDefinitionException {
         this(Utils.load(dependenciesFile));
     }

    
Constructor with XML resource.

Parameters:
clasz Class to use for loading the resource - Cannot be null.
dependenciesFilePathAndName XML resource (path/name) with allowed or forbidden dependencies - Cannot be null.
Throws:
InvalidDependenciesDefinitionException Formal correct (XML) but invalid dependency definition.
 
     public DependencyAnalyzer(final Class<?> claszfinal String dependenciesFilePathAndName)
             throws InvalidDependenciesDefinitionException {
         this(Utils.load(claszdependenciesFilePathAndName));
     }

    
Constructor with dependency definition.

Parameters:
dependencies Definition of allowed or forbidden dependencies - Cannot be null.
Throws:
InvalidDependenciesDefinitionException Formal correct (XML) but invalid dependency definition.
 
     public DependencyAnalyzer(final Dependencies dependencies)
             throws InvalidDependenciesDefinitionException {
         super();
         Utils4J.checkNotNull("dependencies"dependencies);
         this. = dependencies;
         this..validate();
          = new ArrayList<DependencyError>();
     }

    
Analyze the dependencies of all classes in the directory and it's sub directories.

Parameters:
classesDir Directory with ".class" files.
Throws:
IOException Error reading a class file.
    private void analyzeDir(final File classesDirthrows IOException {
        final File[] files = classesDir.listFiles();
        for (int i = 0; i < files.lengthi++) {
            if (files[i].isDirectory()) {
                analyzeDir(files[i]);
            } else {
                if (files[i].getName().endsWith(".class")) {
                    analyzeClass(files[i]);
                }
            }
        }
    }

    
Checks the dependencies for a package from the "allowed" section.

Parameters:
dependencies Dependency definition to use.
allowedPkg Package with allowed imports.
classInfo Information extracted from the class.
Returns:
List of errors - may be empty but is never null.
    private List<DependencyErrorcheckAllowedSection(final Dependencies dependencies,
            final Package<DependsOnallowedPkgfinal ClassInfo classInfo) {
        final List<DependencyErrorerrors = new ArrayList<DependencyError>();
        final Iterator<Stringit = classInfo.getImports().iterator();
        while (it.hasNext()) {
            final String importedPkg = it.next();
            if (!importedPkg.equals(allowedPkg.getName())
                    && !dependencies.isAlwaysAllowed(importedPkg)) {
                final DependsOn dep = Utils.findAllowedByName(allowedPkg.getDependencies(),
                        importedPkg);
                if (dep == null) {
                    errors.add(new DependencyError(classInfo.getName(), importedPkgallowedPkg
                            .getComment()));
                }
            }
        }
        return errors;
    }

    
Checks the dependencies for a package from the "forbidden" section.

Parameters:
dependencies Dependency definition to use.
forbiddenPkg Package with forbidden imports.
classInfo Information extracted from the class.
Returns:
List of errors - may be empty but is never null.
    private static List<DependencyErrorcheckForbiddenSection(final Dependencies dependencies,
            final Package<NotDependsOnforbiddenPkgfinal ClassInfo classInfo) {
        final List<DependencyErrorerrors = new ArrayList<DependencyError>();
        final Iterator<Stringit = classInfo.getImports().iterator();
        while (it.hasNext()) {
            final String importedPkg = it.next();
            if (!importedPkg.equals(classInfo.getPackageName())) {
                final NotDependsOn ndo = Utils.findForbiddenByName(
                        dependencies.getAlwaysForbidden(), importedPkg);
                if (ndo != null) {
                    errors.add(new DependencyError(classInfo.getName(), importedPkgndo
                            .getComment()));
                } else {
                    final NotDependsOn dep = Utils.findForbiddenByName(
                            forbiddenPkg.getDependencies(), importedPkg);
                    if (dep != null) {
                        final String comment;
                        if (dep.getComment() == null) {
                            comment = forbiddenPkg.getComment();
                        } else {
                            comment = dep.getComment();
                        }
                        errors.add(new DependencyError(classInfo.getName(), importedPkgcomment));
                    }
                }
            }
        }
        return errors;
    }

    
Returns the name of the file without path an extension.

Parameters:
filename Filename to extract the name from.
Returns:
Simple name.
    private static String nameOnly(final String filename) {
        final int p = filename.lastIndexOf('.');
        if (p == -1) {
            return filename;
        }
        return filename.substring(0, p);
    }

    
Analyze the dependencies of a class.

Parameters:
classFile Class file to check.
Throws:
IOException Error loading the file.
    private void analyzeClass(final File classFilethrows IOException {
        final ClassInfo classInfo = new ClassInfo(classFile);
        final Package<DependsOnallowedPkg = .findAllowedByName(classInfo
                .getPackageName());
        if (allowedPkg == null) {
            final Package<NotDependsOnforbiddenPkg = .findForbiddenByName(classInfo
                    .getPackageName());
            if (forbiddenPkg == null) {
                .addAll(checkAlwaysForbiddenSection(classInfo));
            } else {
                
                        .addAll(checkForbiddenSection(forbiddenPkgclassInfo));
            }
        } else {
            .addAll(checkAllowedSection(allowedPkgclassInfo));
        }
    }

    
Checks if any of the imports is listed in the "alwaysForbidden" section.

Parameters:
dependencies Dependencies to use.
classInfo Information extracted from the class.
Returns:
List of errors - may be empty but is never null.
            final Dependencies dependenciesfinal ClassInfo classInfo) {
        final List<DependencyErrorerrors = new ArrayList<DependencyError>();
        final Iterator<StringimportedPackages = classInfo.getImports().iterator();
        while (importedPackages.hasNext()) {
            final String importedPackage = importedPackages.next();
            final NotDependsOn ndo = Utils.findForbiddenByName(dependencies.getAlwaysForbidden(),
                    importedPackage);
            if (ndo != null) {
                errors.add(new DependencyError(classInfo.getName(), importedPackagendo
                        .getComment()));
            }
        }
        return errors;
    }

    
Analyze the dependencies for all classes in the directory and it's sub directories.

Parameters:
classesDir Directory where the "*.class" files are located (something like "bin" or "classes").
Throws:
IOException Error reading the classes.
    public final void analyze(final File classesDirthrows IOException {
        .clear();
        analyzeDir(classesDir);
    }

    
Returns the list of dependency errors from last call to analyze(File).

Returns:
List of errors - Always non-null but may be empty.
    public final List<DependencyErrorgetDependencyErrors() {
        return ;
    }

    
Returns the dependency definition.

Returns:
Definition of allowed or forbidden dependencies - Never null.
    public final Dependencies getDependencies() {
        return ;
    }

    
Information about a class extracted from a class file.
    private static class ClassInfo {
        private final String packageName;
        private final String simpleName;
        private final Set<Stringimports;

        
Constructor with class file to load.

Parameters:
classFile Java ".class" file.
Throws:
IOException Error reading the file.
        public ClassInfo(final File classFilethrows IOException {
            final InputStream in = new BufferedInputStream(new FileInputStream(classFile));
            try {
                final DependencyVisitor visitor = new DependencyVisitor();
                new ClassReader(in).accept(visitor, 0);
                final Map<StringMap<StringInteger>> globals = visitor.getGlobals();
                final Set<StringjarPackages = globals.keySet();
                 = jarPackages.iterator().next().replace('/''.');
                 = nameOnly(classFile.getName());
                 = new HashSet<String>();
                final Iterator<Stringit = visitor.getPackages().iterator();
                while (it.hasNext()) {
                    .add(it.next().replace('/''.'));
                }
            } finally {
                in.close();
            }
        }

        
Returns the name of the package.

Returns:
The package of the class.
        public final String getPackageName() {
            return ;
        }

        
Returns a list of imported package names.

Returns:
Names imported by the class.
        public final Set<StringgetImports() {
            return ;
        }

        
Full qualified name of the class.

Returns:
Class name.
        public final String getName() {
            return  + "." + ;
        }
    }
New to GrepCode? Check out our FAQ X