Java Code Examples for proguard.classfile.ClassPool#classesAccept()

The following examples show how to use proguard.classfile.ClassPool#classesAccept() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Performs preverification of the given program class pool.
 */
public void execute(ClassPool programClassPool)
{
    // Clean up any old visitor info.
    programClassPool.classesAccept(new ClassCleaner());

    // Preverify all methods.
    ClassVisitor preverifier =
        new AllMethodVisitor(
        new AllAttributeVisitor(
        new CodePreverifier(configuration.microEdition)));

    // In Java Standard Edition, only class files from Java 6 or higher
    // should be preverified.
    if (!configuration.microEdition)
    {
        preverifier =
            new ClassVersionFilter(ClassConstants.INTERNAL_CLASS_VERSION_1_6,
                                   Integer.MAX_VALUE,
                                   preverifier);
    }

    programClassPool.classesAccept(preverifier);
}
 
Example 2
/**
 * Performs subroutine inlining of the given program class pool.
 */
public void execute(ClassPool programClassPool)
{
    // Clean up any old visitor info.
    programClassPool.classesAccept(new ClassCleaner());

    // Inline all subroutines.
    ClassVisitor inliner =
        new AllMethodVisitor(
        new AllAttributeVisitor(
        new CodeSubroutineInliner()));

    // In Java Standard Edition, only class files from Java 6 or higher
    // should be preverified.
    if (!configuration.microEdition)
    {
        inliner =
            new ClassVersionFilter(ClassConstants.INTERNAL_CLASS_VERSION_1_6,
                                   Integer.MAX_VALUE,
                                   inliner);
    }

    programClassPool.classesAccept(inliner);
}
 
Example 3
/**
 * Prints out the seeds for the classes in the given program class pool.
 * @param configuration the configuration containing the keep options.
 * @throws IOException if an IO error occurs while writing the configuration.
 */
public void write(Configuration configuration,
                  ClassPool     programClassPool,
                  ClassPool     libraryClassPool) throws IOException
{
    // Check if we have at least some keep commands.
    if (configuration.keep == null)
    {
        throw new IOException("You have to specify '-keep' options if you want to write out kept elements with '-printseeds'.");
    }

    // Clean up any old processing info.
    programClassPool.classesAccept(new ClassCleaner());
    libraryClassPool.classesAccept(new ClassCleaner());

    // Create a visitor for printing out the seeds. We're  printing out
    // the program elements that are preserved against shrinking,
    // optimization, or obfuscation.
    KeepMarker keepMarker = new KeepMarker();
    ClassPoolVisitor classPoolvisitor =
        new KeepClassSpecificationVisitorFactory(true, true, true)
            .createClassPoolVisitor(configuration.keep,
                                    keepMarker,
                                    keepMarker,
                                    keepMarker,
                                    null);

    // Mark the seeds.
    programClassPool.accept(classPoolvisitor);
    libraryClassPool.accept(classPoolvisitor);

    // Print out the seeds.
    SimpleClassPrinter printer = new SimpleClassPrinter(false, printWriter);
    programClassPool.classesAcceptAlphabetically(
        new MultiClassVisitor(
            new KeptClassFilter(printer),
            new AllMemberVisitor(new KeptMemberFilter(printer))
        ));
}
 
Example 4
Source Project: proguard   File: Targeter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the target version on classes in the given program class pool.
 */
public void execute(ClassPool programClassPool) throws IOException
{
    Set newerClassVersions = configuration.warn != null ? null : new HashSet();

    programClassPool.classesAccept(new ClassVersionSetter(configuration.targetClassVersion,
                                                          newerClassVersions));

    if (newerClassVersions != null &&
        newerClassVersions.size() > 0)
    {
        System.err.print("Warning: some classes have more recent versions (");

        Iterator iterator = newerClassVersions.iterator();
        while (iterator.hasNext())
        {
            Integer classVersion = (Integer)iterator.next();
            System.err.print(ClassUtil.externalClassVersion(classVersion.intValue()));

            if (iterator.hasNext())
            {
                System.err.print(",");
            }
        }

        System.err.println(")");
        System.err.println("         than the target version ("+ClassUtil.externalClassVersion(configuration.targetClassVersion)+").");

        if (!configuration.ignoreWarnings)
        {
            System.err.println("         If you are sure this is not a problem,");
            System.err.println("         you could try your luck using the '-ignorewarnings' option.");
            throw new IOException("Please correct the above warnings first.");
        }
    }
}
 
Example 5
/**
 * Prints out the seeds for the classes in the given program class pool.
 * @param configuration the configuration containing the keep options.
 * @throws IOException if an IO error occurs while writing the configuration.
 */
public void write(Configuration configuration,
                  ClassPool programClassPool,
                  ClassPool libraryClassPool) throws IOException
{
    // Check if we have at least some keep commands.
    if (configuration.keep == null)
    {
        throw new IOException("You have to specify '-keep' options for the shrinking step.");
    }

    // Clean up any old visitor info.
    programClassPool.classesAccept(new ClassCleaner());
    libraryClassPool.classesAccept(new ClassCleaner());

    // Create a visitor for printing out the seeds. We're  printing out
    // the program elements that are preserved against shrinking,
    // optimization, or obfuscation.
    KeepMarker keepMarker = new KeepMarker();
    ClassPoolVisitor classPoolvisitor =
        ClassSpecificationVisitorFactory.createClassPoolVisitor(configuration.keep,
                                                                keepMarker,
                                                                keepMarker,
                                                                true,
                                                                true,
                                                                true);
    // Mark the seeds.
    programClassPool.accept(classPoolvisitor);
    libraryClassPool.accept(classPoolvisitor);

    // Print out the seeds.
    SimpleClassPrinter printer = new SimpleClassPrinter(false, ps);
    programClassPool.classesAcceptAlphabetically(new MultiClassVisitor(
        new ClassVisitor[]
        {
            new KeptClassFilter(printer),
            new AllMemberVisitor(new KeptMemberFilter(printer))
        }));
}
 
Example 6
public void visitClassPool(ClassPool classPool)
{
    // Go over all classes and their methods, marking if they have side
    // effects, until no new cases can be found.
    do
    {
        newSideEffectCount = 0;

        // Go over all classes and their methods once.
        classPool.classesAccept(this);
    }
    while (newSideEffectCount > 0);
}
 
Example 7
Source Project: java-n-IDE-for-Android   File: Targeter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sets the target version on classes in the given program class pool.
 */
public void execute(ClassPool programClassPool) throws IOException
{
    Set newerClassVersions = configuration.warn != null ? null : new HashSet();

    programClassPool.classesAccept(new ClassVersionSetter(configuration.targetClassVersion,
                                                          newerClassVersions));

    if (newerClassVersions != null &&
        newerClassVersions.size() > 0)
    {
        System.err.print("Warning: some classes have more recent versions (");

        Iterator iterator = newerClassVersions.iterator();
        while (iterator.hasNext())
        {
            Integer classVersion = (Integer)iterator.next();
            System.err.print(ClassUtil.externalClassVersion(classVersion.intValue()));

            if (iterator.hasNext())
            {
                System.err.print(",");
            }
        }

        System.err.println(")");
        System.err.println("         than the target version ("+ClassUtil.externalClassVersion(configuration.targetClassVersion)+").");

        if (!configuration.ignoreWarnings)
        {
            System.err.println("         If you are sure this is not a problem,");
            System.err.println("         you could try your luck using the '-ignorewarnings' option.");
            throw new IOException("Please correct the above warnings first.");
        }
    }
}
 
Example 8
/**
 * Creates a new ClassObfuscator.
 * @param programClassPool        the class pool in which class names
 *                                have to be unique.
 * @param classNameFactory        the optional class obfuscation dictionary.
 * @param packageNameFactory      the optional package obfuscation
 *                                dictionary.
 * @param useMixedCaseClassNames  specifies whether obfuscated packages and
 *                                classes can get mixed-case names.
 * @param keepPackageNames        the optional filter for which matching
 *                                package names are kept.
 * @param flattenPackageHierarchy the base package if the obfuscated package
 *                                hierarchy is to be flattened.
 * @param repackageClasses        the base package if the obfuscated classes
 *                                are to be repackaged.
 * @param allowAccessModification specifies whether obfuscated classes can
 *                                be freely moved between packages.
 */
public ClassObfuscator(ClassPool programClassPool,
                       DictionaryNameFactory classNameFactory,
                       DictionaryNameFactory packageNameFactory,
                       boolean               useMixedCaseClassNames,
                       List                  keepPackageNames,
                       String                flattenPackageHierarchy,
                       String                repackageClasses,
                       boolean               allowAccessModification)
{
    this.classNameFactory   = classNameFactory;
    this.packageNameFactory = packageNameFactory;

    // First append the package separator if necessary.
    if (flattenPackageHierarchy != null &&
        flattenPackageHierarchy.length() > 0)
    {
        flattenPackageHierarchy += ClassConstants.INTERNAL_PACKAGE_SEPARATOR;
    }

    // First append the package separator if necessary.
    if (repackageClasses != null &&
        repackageClasses.length() > 0)
    {
        repackageClasses += ClassConstants.INTERNAL_PACKAGE_SEPARATOR;
    }

    this.useMixedCaseClassNames  = useMixedCaseClassNames;
    this.keepPackageNamesMatcher = keepPackageNames == null ? null :
        new ListParser(new FileNameParser()).parse(keepPackageNames);
    this.flattenPackageHierarchy = flattenPackageHierarchy;
    this.repackageClasses        = repackageClasses;
    this.allowAccessModification = allowAccessModification;

    // Map the root package onto the root package.
    packagePrefixMap.put("", "");

    // Collect all names that have been taken already.
    programClassPool.classesAccept(new MyKeepCollector());
}
 
Example 9
/**
 * Prints out the seeds for the classes in the given program class pool.
 * @param configuration the configuration containing the keep options.
 * @throws IOException if an IO error occurs while writing the configuration.
 */
public void write(Configuration configuration,
                  ClassPool     programClassPool,
                  ClassPool     libraryClassPool) throws IOException
{
    // Check if we have at least some keep commands.
    if (configuration.keep == null)
    {
        throw new IOException("You have to specify '-keep' options for the shrinking step.");
    }

    // Clean up any old visitor info.
    programClassPool.classesAccept(new ClassCleaner());
    libraryClassPool.classesAccept(new ClassCleaner());

    // Create a visitor for printing out the seeds. We're  printing out
    // the program elements that are preserved against shrinking,
    // optimization, or obfuscation.
    KeepMarker keepMarker = new KeepMarker();
    ClassPoolVisitor classPoolvisitor =
        ClassSpecificationVisitorFactory.createClassPoolVisitor(configuration.keep,
                                                                keepMarker,
                                                                keepMarker,
                                                                true,
                                                                true,
                                                                true);
    // Mark the seeds.
    programClassPool.accept(classPoolvisitor);
    libraryClassPool.accept(classPoolvisitor);

    // Print out the seeds.
    SimpleClassPrinter printer = new SimpleClassPrinter(false, ps);
    programClassPool.classesAcceptAlphabetically(new MultiClassVisitor(
        new ClassVisitor[]
        {
            new KeptClassFilter(printer),
            new AllMemberVisitor(new KeptMemberFilter(printer))
        }));
}
 
Example 10
Source Project: proguard   File: Targeter.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the target version on classes in the given program class pool.
 */
public void execute(ClassPool programClassPool) throws IOException
{
    Set newerClassVersions = configuration.warn != null ? null : new HashSet();

    programClassPool.classesAccept(new ClassVersionSetter(configuration.targetClassVersion,
                                                          newerClassVersions));

    if (newerClassVersions != null &&
        newerClassVersions.size() > 0)
    {
        System.err.print("Warning: some classes have more recent versions (");

        Iterator iterator = newerClassVersions.iterator();
        while (iterator.hasNext())
        {
            Integer classVersion = (Integer)iterator.next();
            System.err.print(ClassUtil.externalClassVersion(classVersion.intValue()));

            if (iterator.hasNext())
            {
                System.err.print(",");
            }
        }

        System.err.println(")");
        System.err.println("         than the target version ("+ClassUtil.externalClassVersion(configuration.targetClassVersion)+").");

        if (!configuration.ignoreWarnings)
        {
            System.err.println("         If you are sure this is not a problem,");
            System.err.println("         you could try your luck using the '-ignorewarnings' option.");
            throw new IOException("Please correct the above warnings first.");
        }
    }
}
 
Example 11
Source Project: bazel   File: SeedPrinter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prints out the seeds for the classes in the given program class pool.
 * @param configuration the configuration containing the keep options.
 * @throws IOException if an IO error occurs while writing the configuration.
 */
public void write(Configuration configuration,
                  ClassPool     programClassPool,
                  ClassPool     libraryClassPool) throws IOException
{
    // Check if we have at least some keep commands.
    if (configuration.keep == null)
    {
        throw new IOException("You have to specify '-keep' options for the shrinking step.");
    }

    // Clean up any old visitor info.
    programClassPool.classesAccept(new ClassCleaner());
    libraryClassPool.classesAccept(new ClassCleaner());

    // Create a visitor for printing out the seeds. We're  printing out
    // the program elements that are preserved against shrinking,
    // optimization, or obfuscation.
    KeepMarker keepMarker = new KeepMarker();
    ClassPoolVisitor classPoolvisitor =
        ClassSpecificationVisitorFactory.createClassPoolVisitor(configuration.keep,
                                                                keepMarker,
                                                                keepMarker,
                                                                true,
                                                                true,
                                                                true);
    // Mark the seeds.
    programClassPool.accept(classPoolvisitor);
    libraryClassPool.accept(classPoolvisitor);

    // Print out the seeds.
    SimpleClassPrinter printer = new SimpleClassPrinter(false, ps);
    programClassPool.classesAcceptAlphabetically(new MultiClassVisitor(
        new ClassVisitor[]
        {
            new KeptClassFilter(printer),
            new AllMemberVisitor(new KeptMemberFilter(printer))
        }));
}
 
Example 12
Source Project: bazel   File: Targeter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sets the target version on classes in the given program class pool.
 */
public void execute(ClassPool programClassPool) throws IOException
{
    Set newerClassVersions = configuration.warn != null ? null : new HashSet();

    programClassPool.classesAccept(new ClassVersionSetter(configuration.targetClassVersion,
                                                          newerClassVersions));

    if (newerClassVersions != null &&
        newerClassVersions.size() > 0)
    {
        System.err.print("Warning: some classes have more recent versions (");

        Iterator iterator = newerClassVersions.iterator();
        while (iterator.hasNext())
        {
            Integer classVersion = (Integer)iterator.next();
            System.err.print(ClassUtil.externalClassVersion(classVersion.intValue()));

            if (iterator.hasNext())
            {
                System.err.print(",");
            }
        }

        System.err.println(")");
        System.err.println("         than the target version ("+ClassUtil.externalClassVersion(configuration.targetClassVersion)+").");

        if (!configuration.ignoreWarnings)
        {
            System.err.println("         If you are sure this is not a problem,");
            System.err.println("         you could try your luck using the '-ignorewarnings' option.");
            throw new IOException("Please correct the above warnings first.");
        }
    }
}
 
Example 13
/**
 * Marks classes, resources, resource files and native libraries as being
 * used, based on the configuration.
 *
 * @param programClassPool  the program class pool.
 * @param libraryClassPool  the library class pool.
 * @param resourceFilePool  the resource file pool.
 * @param simpleUsageMarker the usage marker for marking any visitor
 *                          accepters.
 * @param classUsageMarker  the usage marker for recursively marking
 *                          classes.
 */
public void mark(ClassPool         programClassPool,
                 ClassPool         libraryClassPool,
                 ResourceFilePool  resourceFilePool,
                 SimpleUsageMarker simpleUsageMarker,
                 ClassUsageMarker  classUsageMarker)
{
    // Mark the seeds.
    libraryClassPool.classesAccept(classUsageMarker);

    // Mark classes that have to be kept.
    programClassPool.classesAccept(
        new MultiClassVisitor(
            new ClassProcessingFlagFilter(ProcessingFlags.DONT_SHRINK, 0,
                                          classUsageMarker),
            new AllMemberVisitor(
            new MemberProcessingFlagFilter(ProcessingFlags.DONT_SHRINK, 0,
            classUsageMarker))
        ));

    // Mark the elements of Kotlin metadata that need to be kept.
    if (configuration.keepKotlinMetadata)
    {
        // Mark Kotlin things that refer to classes that have been marked as being kept now.
        programClassPool.classesAccept(
            new ReferencedKotlinMetadataVisitor(
            classUsageMarker));
    }

    // Mark the inner class and annotation information that has to be kept.
    programClassPool.classesAccept(
        new UsedClassFilter(simpleUsageMarker,
        new AllAttributeVisitor(true,
        new MultiAttributeVisitor(
            new InnerUsageMarker(classUsageMarker),
            new NestUsageMarker(classUsageMarker),
            new AnnotationUsageMarker(classUsageMarker),
            new LocalVariableTypeUsageMarker(classUsageMarker)
        ))));

    // Mark interfaces that have to be kept.
    programClassPool.classesAccept(new InterfaceUsageMarker(classUsageMarker));

    if (configuration.keepKotlinMetadata)
    {
        // Mark the used Kotlin modules.
        resourceFilePool.resourceFilesAccept(
            new ResourceFileNameFilter(KotlinConstants.MODULE.FILE_EXPRESSION,
            new ResourceFileProcessingFlagFilter(0, ProcessingFlags.DONT_PROCESS_KOTLIN_MODULE,
            new KotlinModuleUsageMarker(simpleUsageMarker))));
    }

    // Check if the Gson optimization is enabled.
    StringMatcher filter = configuration.optimizations != null ?
        new ListParser(new NameParser()).parse(configuration.optimizations) :
        new ConstantMatcher(true);
    boolean libraryGson = filter.matches(Optimizer.LIBRARY_GSON);

    if (configuration.optimize && libraryGson)
    {
        // Setup Gson context that represents how Gson is used in program
        // class pool.
        GsonContext gsonContext = new GsonContext();
        gsonContext.setupFor(programClassPool, libraryClassPool, null);

        // Mark domain classes and fields that are involved in GSON library
        // invocations.
        if (gsonContext.gsonRuntimeSettings.excludeFieldsWithModifiers)
        {
            // When fields are excluded based on modifier, we have to keep all
            // fields.
            gsonContext.gsonDomainClassPool.classesAccept(
                new MultiClassVisitor(
                    classUsageMarker,
                    new AllFieldVisitor(classUsageMarker)));
        }
        else
        {
            // When fields are not excluded based on modifier, we can keep only
            // the fields for which we have injected (de)serialization code.
            gsonContext.gsonDomainClassPool.classesAccept(
                new OptimizedJsonFieldVisitor(classUsageMarker,
                                              classUsageMarker));
        }
    }
}
 
Example 14
/**
 * Sets up the Gson context for the given program class pool.
 * Notes will be printed to the given printer if provided.
 *
 * @param programClassPool the program class pool
 * @param libraryClassPool the library class pool
 * @param warningPrinter   the optional warning printer to which notes
 *                         can be printed.
 */
public void setupFor(ClassPool      programClassPool,
                     ClassPool      libraryClassPool,
                     WarningPrinter warningPrinter)
{
    // Only apply remaining optimizations to classes that are not part of
    // Gson itself.
    ClassPool filteredClasses = new ClassPool();
    programClassPool.classesAccept(
        new ClassNameFilter("!com/google/gson/**",
                            new ClassPoolFiller(filteredClasses)));

    // Find all GsonBuilder invocations.
    gsonRuntimeSettings      = new GsonRuntimeSettings();
    GsonBuilderInvocationFinder gsonBuilderInvocationFinder =
        new GsonBuilderInvocationFinder(
            programClassPool,
            libraryClassPool,
            gsonRuntimeSettings,
            new ClassPoolFiller(gsonRuntimeSettings.instanceCreatorClassPool),
            new ClassPoolFiller(gsonRuntimeSettings.typeAdapterClassPool));

    filteredClasses.classesAccept(
        new AllMethodVisitor(
        new AllAttributeVisitor(
        new AllInstructionVisitor(gsonBuilderInvocationFinder))));

    // Find all Gson invocations.
    gsonDomainClassPool = new ClassPool();
    GsonDomainClassFinder domainClassFinder  =
        new GsonDomainClassFinder(gsonRuntimeSettings,
                                  gsonDomainClassPool,
                                  warningPrinter);

    filteredClasses.accept(
        new AllClassVisitor(
        new AllMethodVisitor(
        new AllAttributeVisitor(
        new AllInstructionVisitor(
        new MultiInstructionVisitor(
            new GsonSerializationInvocationFinder(programClassPool,
                                                  libraryClassPool,
                                                  domainClassFinder,
                                                  warningPrinter),
            new GsonDeserializationInvocationFinder(programClassPool,
                                                    libraryClassPool,
                                                    domainClassFinder,
                                                    warningPrinter)))))));
}
 
Example 15
public void visitClassPool(ClassPool classPool)
{
    classPool.classesAccept(classVisitor);
}
 
Example 16
public void visitClassPool(ClassPool classPool)
{
    classPool.classesAccept(classVisitor);
}
 
Example 17
Source Project: bazel   File: AllClassVisitor.java    License: Apache License 2.0 4 votes vote down vote up
public void visitClassPool(ClassPool classPool)
{
    classPool.classesAccept(classVisitor);
}
 
Example 18
Source Project: atlas   File: BundleProguardDumper.java    License: Apache License 2.0 3 votes vote down vote up
public static Map<String, ClazzRefInfo> dump(ProGuard proGuard, Set<String> defaultClasses) throws Exception {

        ClassPool classPool = (ClassPool)ReflectUtils.getField(proGuard, "programClassPool");
        ClassPool libClassPool = (ClassPool)ReflectUtils.getField(proGuard, "libraryClassPool");

        VisitorDTO visitorDTO = new VisitorDTO(defaultClasses, classPool, libClassPool);

        classPool.classesAccept(new ClassStructVisitor(visitorDTO));
        classPool.classesAccept(new ClassDetailVisitor(visitorDTO));
        visitorDTO.addSuperRefInfo();

        return visitorDTO.clazzRefInfoMap;
    }