Class AsmUtils

java.lang.Object
de.thetaphi.forbiddenapis.AsmUtils

public final class AsmUtils extends Object
Some static utilities for analyzing with ASM, also constants.
  • Field Details

    • REGEX_META_CHARS

      private static final String REGEX_META_CHARS
      See Also:
    • PORTABLE_RUNTIME_PACKAGE_PATTERN

      private static final Pattern PORTABLE_RUNTIME_PACKAGE_PATTERN
      Package prefixes of documented Java API (extracted from Javadocs of Java 8).
    • RUNTIME_MODULES_PATTERN

      private static final Pattern RUNTIME_MODULES_PATTERN
      Pattern that matches all module names, which are shipped by default in Java. (see: http://openjdk.java.net/projects/jigsaw/spec/sotms/): The remaining platform modules will share the 'java.' name prefix and are likely to include, e.g., java.sql for database connectivity, java.xml for XML processing, and java.logging for logging. Modules that are not defined in the Java SE 9 Platform Specification but instead specific to the JDK will, by convention, share the 'jdk.' name prefix.
    • LATEST_CLASS_VERSION

      public static final int LATEST_CLASS_VERSION
      Maximum classfile version the bundled ASM can handle.
      See Also:
    • CLASS_DOWNGRADE_LOGGED

      private static final AtomicBoolean CLASS_DOWNGRADE_LOGGED
  • Constructor Details

    • AsmUtils

      private AsmUtils()
  • Method Details

    • makePkgPrefixPattern

      private static Pattern makePkgPrefixPattern(String... prefixes)
    • isRegexMeta

      private static boolean isRegexMeta(char c)
    • isPortableRuntimeClass

      public static boolean isPortableRuntimeClass(String className)
      Returns true, if the given binary class name (dotted) is part of the documented and portable Java APIs.
    • isRuntimeModule

      public static boolean isRuntimeModule(String module)
      Returns true, if the given Java 9 module name is part of the runtime (no custom 3rd party module).
      Parameters:
      module - the module name or null, if in unnamed module
    • binaryToInternal

      public static String binaryToInternal(String clazz)
      Converts a binary class name (dotted) to the JVM internal one (slashed). Only accepts valid class names, no arrays.
    • getClassResourceName

      public static String getClassResourceName(String clazz)
      Converts a binary class name to a ".class" file resource name.
    • isGlob

      public static boolean isGlob(String s)
      Returns true is a string is a glob pattern
    • glob2Pattern

      public static Pattern glob2Pattern(String... globs)
      Returns a regex pattern that matches on any of the globs on class names (e.g., "sun.misc.**")
    • getModuleName

      public static String getModuleName(URL jrtUrl)
      Returns the module name from a jrt:/ URL; returns null if no module given or wrong URL type.
    • readStream

      private static byte[] readStream(InputStream in) throws IOException
      This method is used to read the whole stream into byte array. This allows patching.
      Throws:
      IOException
    • readAndPatchClass

      public static org.objectweb.asm.ClassReader readAndPatchClass(InputStream in, Logger logger) throws IOException
      Utility method to load class files of later Java versions by patching them, so ASM can read them.
      Throws:
      IOException
    • isExceptionInAsmClassReader

      public static boolean isExceptionInAsmClassReader(RuntimeException re)
      Returns true, if the given RuntimeException was caused by ASM's ClassReader
    • formatClassesAbbreviated

      public static String formatClassesAbbreviated(Collection<String> missingClasses)
      Formats a list of classes, abbreviated, with 2 spaces in front (for logging)