Class Analyzer

  • All Implemented Interfaces:
    Constants, Registry, aQute.service.reporter.Report, aQute.service.reporter.Reporter, java.io.Closeable, java.lang.AutoCloseable, java.lang.Iterable<java.lang.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.
    • Constructor Detail

      • Analyzer

        public Analyzer​(Jar jar)
                 throws java.lang.Exception
        Throws:
        java.lang.Exception
      • Analyzer

        public Analyzer​(Processor parent)
      • Analyzer

        public Analyzer()
    • Method Detail

      • getManifest

        public static java.util.Properties getManifest​(java.io.File dirOrJar)
                                                throws java.lang.Exception
        Specifically for Maven
        Throws:
        java.lang.Exception
      • analyze

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

        public java.util.Optional<java.util.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 java.util.Optional<java.util.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
      • getName

        protected java.lang.String getName​(Jar jar)
                                    throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getExtra

        protected Jar getExtra()
                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • calcManifest

        public java.util.jar.Manifest calcManifest()
                                            throws java.lang.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:
        java.io.IOException
        java.lang.Exception
      • findClasspathEntry

        public Jar findClasspathEntry​(java.lang.String bsn,
                                      java.lang.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 &r or null if not found
      • getBsn

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

        public java.lang.String _bsn​(java.lang.String[] args)
      • calculateExportsFromContents

        public java.lang.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()
      • getJar

        public Jar getJar()
      • getReferred

        public Packages getReferred()
      • getUnreachable

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

        public Packages getClasspathExports()
      • getBndVersion

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

        public long getBndLastModified()
      • getBndInfo

        public java.lang.String getBndInfo​(java.lang.String key,
                                           java.lang.String defaultValue)
      • mergeManifest

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

        public void setBase​(java.io.File file)
        Overrides:
        setBase in class Processor
      • setClasspath

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

        public void setClasspath​(java.io.File[] classpath)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • setClasspath

        public void setClasspath​(Jar[] classpath)
      • setClasspath

        public void setClasspath​(java.lang.String[] classpath)
      • setJar

        public Jar setJar​(java.io.File file)
                   throws java.io.IOException
        Set the JAR file we are going to work in. This will read the JAR in memory.
        Parameters:
        file -
        Throws:
        java.io.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​(java.lang.String name,
                                  java.lang.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 java.util.List<Jar> getJarsFromName​(java.lang.String name,
                                                   java.lang.String from)
      • close

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

        public java.lang.String _findpath​(java.lang.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 java.lang.String _findname​(java.lang.String[] args)
      • putAll

        public void putAll​(java.util.Map<java.lang.String,​java.lang.String> additional,
                           boolean force)
      • getClasspath

        public java.util.List<Jar> getClasspath()
      • addClasspath

        public void addClasspath​(Jar jar)
      • addClasspath

        public void addClasspath​(java.util.Collection<?> jars)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • addClasspath

        public void addClasspath​(java.io.File cp)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • getTarget

        public Jar getTarget()
      • cleanupVersion

        public static java.lang.String cleanupVersion​(java.lang.String version)
      • getVersionPolicy

        public java.lang.String getVersionPolicy​(boolean implemented)
      • _classes

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

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

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

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

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

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

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

        public java.util.stream.Stream<Resource> findResources​(java.util.function.Predicate<java.lang.String> matches)
      • findClass

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

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

        public boolean isNoBundle()
      • referToByBinaryName

        public void referToByBinaryName​(java.lang.String binaryClassName)
      • doRequireBnd

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

        public java.lang.String _md5​(java.lang.String[] args)
                              throws java.lang.Exception
        Throws:
        java.lang.Exception
      • _sha1

        public java.lang.String _sha1​(java.lang.String[] args)
                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getTypeRefFromPath

        public Descriptors.TypeRef getTypeRefFromPath​(java.lang.String path)
      • getClassSignature

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

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

        public aQute.bnd.signatures.FieldSignature getFieldSignature​(java.lang.String signature)
      • setDiagnostics

        public void setDiagnostics​(boolean b)
      • _ee

        public java.lang.String _ee​(java.lang.String[] args)
      • getOutputFile

        public java.io.File getOutputFile​(java.lang.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​(java.io.File output,
                            boolean force)
                     throws java.lang.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:
        java.lang.Exception
      • setDefaults

        public void setDefaults​(java.lang.String bsn,
                                Version version)
        Set default import and export instructions if none are set
      • getClassspace

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

        public java.lang.String _exports​(java.lang.String[] args)
      • _imports

        public java.lang.String _imports​(java.lang.String[] args)
      • report

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

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

        public java.lang.String validResourcePath​(java.lang.String name,
                                                  java.lang.String reportIfWrong)
        Parameters:
        name -
      • check

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

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

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

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

        public boolean assignable​(java.lang.String annoService,
                                  java.lang.String inferredService)
      • assignable

        public boolean assignable​(java.lang.String annoService,
                                  java.lang.String inferredService,
                                  boolean unknownResult)
      • assignable

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

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

        public java.util.Map<Descriptors.TypeRef,​java.lang.String> getBundleClassPathTypes()
      • getBundleClassPathEntry

        public java.util.Optional<java.lang.String> getBundleClassPathEntry​(Clazz clazz)