Class Analyzer

All Implemented Interfaces:
Constants, Registry, aQute.service.reporter.Report, aQute.service.reporter.Reporter, Closeable, AutoCloseable, Iterable<String>
Direct Known Subclasses:
Builder

public class Analyzer extends Processor
This class can calculate the required headers for a (potential) JAR file. It analyzes a directory or JAR for the packages that are contained and that are referred to by the bytecodes. The user can the use regular expressions to define the attributes and directives. The matching is not fully regex for convenience. A * and ? get a . prefixed and dots are escaped.
*;auto=true             any
org.acme.*;auto=true    org.acme.xyz
org.[abc]*;auto=true    org.acme.xyz
Additional, the package instruction can start with a '=' or a '!'. The '!' indicates negation. Any matching package is removed. The '=' is literal, the expression will be copied verbatim and no matching will take place. Any headers in the given properties are used in the output properties.
  • Field Details

  • Constructor Details

  • Method Details

    • setTypeSpecificPlugins

      protected void setTypeSpecificPlugins(PluginsContainer pluginsContainer)
      Overrides:
      setTypeSpecificPlugins in class Processor
    • getManifest

      public static Properties getManifest(File dirOrJar) throws Exception
      Specifically for Maven
      Throws:
      Exception
    • analyze

      public void analyze() throws Exception
      Calculates the data structures for generating a manifest.
      Throws:
      IOException
      Exception
    • reset

      public void reset()
    • getHostPackages

      public Optional<Set<Descriptors.PackageRef>> getHostPackages()
      Get the packages from the host if this is a fragment bundle
      Returns:
      the host packages or an empty set if not a fragment
    • getRequireBundlePackages

      public Optional<Set<Descriptors.PackageRef>> getRequireBundlePackages()
      Find the packages belonging to the required bundles
      Returns:
      the packages from the required bundles, with no Require-Bundle return an empty Optional
    • getPackageInfo

      public Clazz getPackageInfo(Descriptors.PackageRef packageRef)
    • getName

      protected String getName(Jar jar) throws Exception
      Throws:
      Exception
    • getExtra

      protected Jar getExtra() throws Exception
      Throws:
      Exception
    • calcManifest

      public Manifest calcManifest() throws Exception
      One of the main workhorses of this class. This will analyze the current setup and calculate a new manifest according to this setup.
      Throws:
      IOException
      Exception
    • findClasspathEntry

      public Jar findClasspathEntry(String bsn, String r)
      Find a class path entry based on bsn and versionrange
      Parameters:
      bsn - The bundle symbolic name
      r - The version range specified like in OSGi (version => [version,infinite))
      Returns:
      first JAR that matches bsn invalid input: '&r' or null if not found
    • getBsn

      public String getBsn()
      Clear the key part of a header. I.e. remove everything from the first ';'
    • _bsn

      public String _bsn(String[] args)
    • calculateExportsFromContents

      public String calculateExportsFromContents(Jar bundle)
      Calculate an export header solely based on the contents of a JAR file
      Parameters:
      bundle - The jar file to analyze
    • getContained

      public Packages getContained()
    • getExports

      public Packages getExports()
    • getImports

      public Packages getImports()
    • getPrivates

      public Set<Descriptors.PackageRef> getPrivates()
    • getJar

      public Jar getJar()
    • getReferred

      public Packages getReferred()
    • getUnreachable

      public Set<Descriptors.PackageRef> getUnreachable()
      Return the set of unreachable code depending on exports and the bundle activator.
    • getUses

    • getAPIUses

    • getClasspathExports

      public Packages getClasspathExports()
    • getBndVersion

      public String getBndVersion()
      Get the version for this bnd
      Returns:
      version or unknown.
    • getBndLastModified

      public long getBndLastModified()
    • getBndInfo

      public String getBndInfo(String key, String defaultValue)
    • mergeManifest

      public void mergeManifest(Manifest manifest) throws IOException
      Merge the existing manifest with the instructions but do not override existing properties.
      Parameters:
      manifest - The manifest to merge with
      Throws:
      IOException
    • setBase

      public void setBase(File file)
      Overrides:
      setBase in class Processor
    • setClasspath

      public void setClasspath(Collection<?> classpath) throws IOException
      Set the classpath for this analyzer by file.
      Parameters:
      classpath -
      Throws:
      IOException
    • setClasspath

      public void setClasspath(File[] classpath) throws IOException
      Throws:
      IOException
    • setClasspath

      public void setClasspath(Jar[] classpath)
    • setClasspath

      public void setClasspath(String[] classpath)
    • setJar

      public Jar setJar(File file) throws IOException
      Set the JAR file we are going to work in. This will read the JAR in memory.
      Parameters:
      file -
      Throws:
      IOException
    • setJar

      public Jar setJar(Jar jar)
      Set the JAR directly we are going to work on.
      Parameters:
      jar -
    • begin

      protected void begin()
      Overrides:
      begin in class Processor
    • getJarFromName

      public Jar getJarFromName(String name, String from)
      Try to get a Jar from a file name/path or a url, or in last resort from the classpath name part of their files.
      Overrides:
      getJarFromName in class Processor
      Parameters:
      name - URL or filename relative to the base
      from - Message identifying the caller for errors
      Returns:
      null or a Jar with the contents for the name
    • getJarsFromName

      public List<Jar> getJarsFromName(String name, String from)
    • referred

      public boolean referred(Descriptors.PackageRef packageName)
    • doUses

      protected void doUses(Descriptors.PackageRef packageRef, Packages exports, Map<Descriptors.PackageRef, List<Descriptors.PackageRef>> uses, Packages imports)
      Parameters:
      packageRef -
      exports -
      uses -
      imports -
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class Processor
      Throws:
      IOException
    • _findpath

      public String _findpath(String[] args)
      Findpath looks through the contents of the JAR and finds paths that end with the given regular expression ${findpath (; reg-expr (; replacement)? )? }
      Parameters:
      args -
    • _findname

      public String _findname(String[] args)
    • putAll

      public void putAll(Map<String,String> additional, boolean force)
    • getClasspath

      public List<Jar> getClasspath()
    • addClasspath

      public void addClasspath(Jar jar)
    • addClasspath

      public void addClasspath(Collection<?> jars) throws IOException
      Throws:
      IOException
    • addClasspath

      public void addClasspath(File cp) throws IOException
      Throws:
      IOException
    • clear

      public void clear()
      Overrides:
      clear in class Processor
    • forceRefresh

      public void forceRefresh()
      Overrides:
      forceRefresh in class Processor
    • getTarget

      public Jar getTarget()
    • cleanupVersion

      public static String cleanupVersion(String version)
    • getVersionPolicy

      public String getVersionPolicy(boolean implemented)
    • _classes

      public String _classes(String... args) throws Exception
      Throws:
      Exception
    • getClasses

      public Collection<Clazz> getClasses(String... args) throws Exception
      Throws:
      Exception
    • _packages

      public String _packages(String... args) throws Exception
      Throws:
      Exception
    • getPackages

      public Collection<Descriptors.PackageRef> getPackages(Packages scope, String... args) throws Exception
      Throws:
      Exception
    • _exporters

      public String _exporters(String[] args) throws Exception
      Get the exporter of a package ...
      Throws:
      Exception
    • getClassspace

      public Map<Descriptors.TypeRef, Clazz> getClassspace()
    • _packageattribute

      public String _packageattribute(String[] args)
      Return an attribute of a package
    • findResource

      public Resource findResource(String path)
      Locate a resource on the class path.
      Parameters:
      path - Path of the reosurce
      Returns:
      A resource or null
    • findResources

      public Stream<Resource> findResources(Predicate<String> matches)
    • findClass

      public Clazz findClass(Descriptors.TypeRef typeRef) throws Exception
      Find a clazz on the class path. This class has been parsed.
      Throws:
      Exception
    • getVersion

      public String getVersion()
      Answer the bundle version.
    • isNoBundle

      public boolean isNoBundle()
    • referTo

      public void referTo(Descriptors.TypeRef ref)
    • nonClassReferTo

      public void nonClassReferTo(Descriptors.TypeRef ref)
    • referToByBinaryName

      public void referToByBinaryName(String binaryClassName)
    • doRequireBnd

      protected void doRequireBnd()
      Ensure that we are running on the correct bnd.
    • _md5

      public String _md5(String[] args) throws Exception
      Throws:
      Exception
    • _sha1

      public String _sha1(String[] args) throws Exception
      Throws:
      Exception
    • getDescriptor

      public Descriptors.Descriptor getDescriptor(String descriptor)
    • getTypeRef

      public Descriptors.TypeRef getTypeRef(String binaryClassName)
    • getPackageRef

      public Descriptors.PackageRef getPackageRef(String binaryName)
    • getTypeRefFrom

      public Descriptors.TypeRef getTypeRefFrom(Class<?> clazz)
    • getTypeRefFromFQN

      public Descriptors.TypeRef getTypeRefFromFQN(String fqn)
    • getTypeRefFromPath

      public Descriptors.TypeRef getTypeRefFromPath(String path)
    • getClassSignature

      public aQute.bnd.signatures.ClassSignature getClassSignature(String signature)
    • getMethodSignature

      public aQute.bnd.signatures.MethodSignature getMethodSignature(String signature)
    • getFieldSignature

      public aQute.bnd.signatures.FieldSignature getFieldSignature(String signature)
    • isImported

      public boolean isImported(Descriptors.PackageRef packageRef)
    • setDiagnostics

      public void setDiagnostics(boolean b)
    • getLowestEE

      public Clazz.JAVA getLowestEE()
    • getHighestEE

      public Clazz.JAVA getHighestEE()
    • _ee

      public String _ee(String[] args)
    • getOutputFile

      public File getOutputFile(String output)
      Calculate the output file for the given target. The strategy is:
      parameter given if not null and not directory if directory, this will be
      the output directory based on bsn-version.jar name of the source file if
      exists Untitled-[n]
      
      Parameters:
      output - may be null, otherwise a file path relative to base
    • save

      public boolean save(File output, boolean force) throws Exception
      Utility function to carefully save the file. Will create a backup if the source file has the same path as the output. It will also only save if the file was modified or the force flag is true
      Parameters:
      output - the output file, if null getOutputFile(String) is used.
      force - if it needs to be overwritten
      Throws:
      Exception
    • setDefaults

      public void setDefaults(String bsn, Version version)
      Set default import and export instructions if none are set
    • cleanupUses

      Remove the own references and optional java references from the uses lib
      Parameters:
      apiUses -
      removeJava -
    • getClassspace

      public Set<Clazz> getClassspace(Descriptors.PackageRef source)
      Return the classes for a given source package.
      Parameters:
      source - the source package
      Returns:
      a set of classes for the requested package.
    • getXRef

      public Map<Clazz.Def, List<Descriptors.TypeRef>> getXRef(Descriptors.PackageRef source, Collection<Descriptors.PackageRef> dest, int sourceModifiers) throws Exception
      Create a cross reference from package source, to packages in dest
      Parameters:
      source -
      dest -
      sourceModifiers -
      Throws:
      Exception
    • _exports

      public String _exports(String[] args)
    • _imports

      public String _imports(String[] args)
    • report

      public void report(Map<String,Object> table) throws Exception
      Report the details of this analyzer
      Overrides:
      report in class Processor
      Parameters:
      table -
      Throws:
      Exception
    • getEEs

      public SortedSet<Clazz.JAVA> getEEs()
      Return the EEs
    • validResourcePath

      public String validResourcePath(String name, String reportIfWrong)
      Parameters:
      name -
    • check

      public boolean check(Analyzer.Check key)
      Check if we have an a check option
    • getSourceFileFor

      public String getSourceFileFor(Descriptors.TypeRef type) throws Exception
      Find the source file for this type
      Parameters:
      type -
      Throws:
      Exception
    • getSourceFileFor

      public String getSourceFileFor(Descriptors.TypeRef type, Collection<File> sourcePath) throws Exception
      Throws:
      Exception
    • setTypeLocation

      public void setTypeLocation(aQute.service.reporter.Reporter.SetLocation location, Descriptors.TypeRef type) throws Exception
      Set location information for a type.
      Throws:
      Exception
    • assignable

      public boolean assignable(String annoService, String inferredService)
    • assignable

      public boolean assignable(String annoService, String inferredService, boolean unknownResult)
    • assignable

      public boolean assignable(Clazz annoServiceClazz, Clazz inferredServiceClazz)
    • assignable

      public boolean assignable(Clazz annoServiceClazz, Clazz inferredServiceClazz, boolean unknownResult)
    • getBundleClassPathTypes

      public Map<Descriptors.TypeRef, String> getBundleClassPathTypes()
    • getBundleClassPathEntry

      public Optional<String> getBundleClassPathEntry(Clazz clazz)
    • addDelta

      public void addDelta(Jar delta)
      Add the content of the JAR to the current JAR and reset the analyzed flag so that a new manifest will be calculated with the files in the JAR included.
      Parameters:
      delta - a set of resources added to our jar and then set to be reanalyzed
    • addAnnotation

      public void addAnnotation(Annotation ann, Descriptors.TypeRef c) throws Exception
      Useful to reuse the annotation processing. The Annotation can be created from other sources than Java code. This is mostly useful for the annotations that generate Manifest headers.
      Throws:
      Exception
    • addClasspathDefault

      public void addClasspathDefault(Class<?> type)
      Get a class from our own class path
      Parameters:
      type - a local type on the bnd classpath