Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2015 The Pennsylvania State University and the University of Wisconsin
   * Systems and Internet Infrastructure Security Laboratory
   *
   * Author: Damien Octeau
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package edu.psu.cse.siis.coal;
 
 import java.io.File;
 import java.util.Set;
 
The high-level pattern for the analysis, including the analysis setup, running the analysis and processing the results. Classes implementing this should override:

Parameters:
<A> A generic CommandLineArguments type for command line arguments.
 
 public abstract class Analysis<A extends CommandLineArguments> {
  
Pattern method to perform the analysis.

Parameters:
commandLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
 
   public void performAnalysis(A commandLineArguments) {
     try {
       PropagationTimers.v()..start();
       loadModel(commandLineArguments);
       PropagationTimers.v()..end();
       PropagationTimers.v()..start();
       registerFieldTransformerFactories(commandLineArguments);
       registerArgumentValueAnalyses(commandLineArguments);
       registerMethodReturnValueAnalyses(commandLineArguments);
       setApplicationClasses(commandLineArguments);
       PropagationTimers.v()..end();
       initializeAnalysis(commandLineArguments);
       PropagationTimers.v()..start();
       PackManager.v().runPacks();
       PropagationTimers.v()..end();
       processResults(commandLineArguments);
       finalizeAnalysis(commandLineArguments);
     } catch (FatalAnalysisException e) {
       handleFatalAnalysisException(commandLineArgumentse);
     }
   }

  
Loads the COAL model to be used for the analysis. The model can be stored either in a directory with a flat structure (no subdirectories) or in a compiled (serialized) format.

Parameters:
commanLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
Throws:
FatalAnalysisException if a fatal error occurs.
See also:
ModelCompiler
 
   protected void loadModel(A commanLineArgumentsthrows FatalAnalysisException {
     try {
       if (commanLineArguments.getCompiledModel() != null) {
         Model.loadModelFromCompiledFile(commanLineArguments.getCompiledModel());
       } else {
         Model.loadModel(commanLineArguments.getModel());
       }
     } catch (ClassNotFoundException | IOException | ParseException e) {
       throw new FatalAnalysisException("Could not load model"e);
     }
   }

  
Registers the field transformer factories for the analysis. A FieldTransformer specifies how a field is modified by a method. For each possible FieldTransformer type, a FieldTransformerFactory should be declared to instantiate the FieldTransformer as appropriate.

Parameters:
commandLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
Throws:
FatalAnalysisException if a fatal error occurs.
  protected abstract void registerFieldTransformerFactories(A commandLineArguments)
      throws FatalAnalysisException;

  
Registers the method argument value analyses. FieldTransformer objects are generated by FieldTransformerFactory objects using both a COAL specification and most often a method argument value. This method specifies which ArgumentValueAnalysis is in charge of handling every argument type using the registerArgumentValueAnalysis method.

Parameters:
commandLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
Throws:
FatalAnalysisException if a fatal error occurs.
  protected abstract void registerArgumentValueAnalyses(A commandLineArguments)
      throws FatalAnalysisException;

  
Registers method return value analyses. When the return value of a method is always known and the method should not be traversed, a MethodReturnValueAnalysis can be registered to specify the return value with the registerMethodReturnValueAnalysis method.

Parameters:
commandLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
Throws:
FatalAnalysisException if a fatal error occurs.
  protected abstract void registerMethodReturnValueAnalyses(A commandLineArguments)
      throws FatalAnalysisException;

  
Registers the application classes through which the propagation should take place. By default, this is the list of classes in the input directory or jar file. By default, it also includes the list of modeled classes if it was requested on the command line.

Parameters:
commandLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
Throws:
FatalAnalysisException if a fatal error occurs.
  protected void setApplicationClasses(A commandLineArgumentsthrows FatalAnalysisException {
    AnalysisParameters.v().addAnalysisClasses(
        computeAnalysisClasses(commandLineArguments.getInput()));
    if (commandLineArguments.traverseModeled()) {
      AnalysisParameters.v().addAnalysisClasses(Model.v().getModeledTypes());
    }
  }

  
Initializes the analysis, including Soot parameters (pointer analysis, etc.).

Parameters:
commandLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
Throws:
FatalAnalysisException if a fatal error occurs.
  protected abstract void initializeAnalysis(A commandLineArgumentsthrows FatalAnalysisException;

  
Processes the results of the analysis.

Parameters:
commandLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
Throws:
FatalAnalysisException if a fatal error occurs.
  protected abstract void processResults(A commandLineArgumentsthrows FatalAnalysisException;

  
Finalizes the analysis. This should be used for example to close open files.

Parameters:
commandLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
Throws:
FatalAnalysisException if a fatal error occurs.
  protected abstract void finalizeAnalysis(A commandLineArgumentsthrows FatalAnalysisException;

  
Handles a fatal analysis exception. A FatalAnalysisException occurs when something serious prevents the analysis from proceeding. This exception is caught at a higher level to allow some things to be wrapped up (e.g., closing files or database connections). This method specifies the wrapping up process.

Parameters:
commandLineArguments A CommandLineArguments subclass object that represents the command line arguments for this analysis.
exception A FatalAnalysisException.
  protected abstract void handleFatalAnalysisException(A commandLineArguments,
      FatalAnalysisException exception);

  
Computes the set of classes in a directory or a jar file. This method computes the fully qualified Java name of all the classes under a given directory. The class files can be in multiple packages.

Parameters:
dirOrJar A directory or jar path.
Returns:
The set of classes in the input directory.
Throws:
FatalAnalysisException if something goes wrong with the file operations.
  protected Set<StringcomputeAnalysisClasses(String dirOrJarthrows FatalAnalysisException {
    try {
      File file = new File(dirOrJar);
      if (file.isDirectory()) {
        String directoryString = file.getCanonicalPath();
        int basePos = directoryString.length() + 1;
        return computeAnalysisClassesInDir(filebasePos);
      } else {
        return computeAnalysisClassesInJar(file);
      }
    } catch (IOException e) {
      e.printStackTrace();
      throw new FatalAnalysisException(e);
    }
  }

  
Helper for computing the set of classes in a directory.

Parameters:
directory A File object for the directory under which classes should be searched for.
basePos The position of the first letter for the Java package names in the directory's absolute path. For example, if the current directory is /home/foo/bar, but the top-level directory where all Java packages are located is /home/foo, then the basePos would be 10, which is the index of the b character.
Returns:
The set of classes found under the directory.
Throws:
java.io.IOException if something goes wrong with the file operations.
  private Set<StringcomputeAnalysisClassesInDir(File directoryint basePosthrows IOException {
    File[] nestedFilesAndDirs = directory.listFiles();
    Set<Stringresult = new HashSet<String>();
    for (File nestedFile : nestedFilesAndDirs) {
      if (nestedFile.isDirectory()) {
        result.addAll(computeAnalysisClassesInDir(nestedFilebasePos));
      } else {
        String canonicalPath = nestedFile.getCanonicalPath();
        if (canonicalPath.endsWith(".class")) {
          result
              .add(canonicalPath.substring(basePoscanonicalPath.length() - 6).replace('/''.'));
        }
      }
    }
    return result;
  }

  
Helper for computing the set of classes in a jar file.

Parameters:
file A File object for the jar in which classes should be searched for.
Returns:
The set of classes found in the jar.
Throws:
java.io.IOException if something goes wrong with the file operations.
  private Set<StringcomputeAnalysisClassesInJar(File filethrows IOException {
    Set<Stringresult = new HashSet<>();
    JarFile jarFile = new JarFile(file);
    Enumeration<JarEntryjarEntries = jarFile.entries();
    while (jarEntries.hasMoreElements()) {
      JarEntry jarEntry = jarEntries.nextElement();
      String entryName = jarEntry.getName();
      if (entryName.endsWith(".class")) {
        String name = entryName.substring(0, entryName.length() - 6).replace('/''.');
        result.add(name);
      }
    }
    jarFile.close();
    return result;
  }
New to GrepCode? Check out our FAQ X