Java Code Examples for proguard.classfile.ProgramClass

The following examples show how to use proguard.classfile.ProgramClass. 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
public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
{
    // Do we know the referenced class?
    Clazz referencedClass = classConstant.referencedClass;
    if (referencedClass != null)
    {
        // Has the class name changed?
        String className    = classConstant.getName(clazz);
        String newClassName = newClassName(className, referencedClass);
        if (!className.equals(newClassName))
        {
            // Refer to a new Utf8 entry.
            classConstant.u2nameIndex =
                new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newClassName);
        }
    }
}
 
Example 2
public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
{
    // Does the string refer to a class, due to a Class.forName construct?
    Clazz referencedClass  = stringConstant.referencedClass;
    Member referencedMember = stringConstant.referencedMember;
    if (referencedClass  != null &&
        referencedMember == null)
    {
        // Reconstruct the new class name.
        String externalClassName    = stringConstant.getString(clazz);
        String internalClassName    = ClassUtil.internalClassName(externalClassName);
        String newInternalClassName = newClassName(internalClassName,
                                                   referencedClass);

        // Update the String entry if required.
        if (!newInternalClassName.equals(internalClassName))
        {
            String newExternalClassName = ClassUtil.externalClassName(newInternalClassName);

            // Refer to a new Utf8 entry.
            stringConstant.u2stringIndex =
                new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newExternalClassName);
        }
    }
}
 
Example 3
public void visitFieldrefConstant(Clazz clazz, FieldrefConstant fieldrefConstant)
{
    // Do we know the referenced field?
    Member referencedMember = fieldrefConstant.referencedMember;
    if (referencedMember != null)
    {
        Clazz referencedClass = fieldrefConstant.referencedClass;

        // Does it have a new name or type?
        String newName = referencedMember.getName(referencedClass);
        String newType = referencedMember.getDescriptor(referencedClass);

        if (!fieldrefConstant.getName(clazz).equals(newName) ||
            !fieldrefConstant.getType(clazz).equals(newType))
        {
            if (DEBUG)
            {
                debug(clazz, fieldrefConstant, referencedClass, referencedMember);
            }

            // Update the name and type index.
            fieldrefConstant.u2nameAndTypeIndex =
                new ConstantPoolEditor((ProgramClass)clazz).addNameAndTypeConstant(newName, newType);
        }
    }
}
 
Example 4
public void visitProgramClass(ProgramClass programClass)
{
    // Mark all referenced class members in different classes.
    programClass.constantPoolEntriesAccept(this);

    // Explicitly mark the <clinit> method.
    programClass.methodAccept(ClassConstants.INTERNAL_METHOD_NAME_CLINIT,
                              ClassConstants.INTERNAL_METHOD_TYPE_CLINIT,
                              this);

    // Explicitly mark the parameterless <init> method.
    programClass.methodAccept(ClassConstants.INTERNAL_METHOD_NAME_INIT,
                              ClassConstants.INTERNAL_METHOD_TYPE_INIT,
                              this);

    // Mark all methods that may have implementations.
    programClass.methodsAccept(filteredMethodMarker);
}
 
Example 5
public void visitProgramClass(ProgramClass programClass)
{
    stackSizesMayHaveChanged = false;

    // Fix the constant pool entries.
    for (int index = 1; index < programClass.u2constantPoolCount; index++)
    {
        Constant constant = programClass.constantPool[index];
        if (constant != null)
        {
            // Fix the entry, replacing it entirely if needed.
            this.constantIndex = index;

            constant.accept(programClass, this);
        }
    }

    // Fix the class members.
    programClass.fieldsAccept(this);
    programClass.methodsAccept(this);

    // Fix the attributes.
    programClass.attributesAccept(this);
}
 
Example 6
Source Project: java-n-IDE-for-Android   Source File: MappingPrinter.java    License: Apache License 2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Special cases: <clinit> and <init> are always kept unchanged.
    // We can ignore them here.
    String name = programMethod.getName(programClass);
    if (name.equals(ClassConstants.INTERNAL_METHOD_NAME_CLINIT) ||
        name.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT))
    {
        return;
    }

    String newName = MemberObfuscator.newMemberName(programMethod);
    if (newName != null)
    {
        ps.println("    " +
                   lineNumberRange(programClass, programMethod) +
                   ClassUtil.externalFullMethodDescription(
                       programClass.getName(),
                       0,
                       programMethod.getName(programClass),
                       programMethod.getDescriptor(programClass)) +
                   " -> " +
                   newName);
    }
}
 
Example 7
Source Project: java-n-IDE-for-Android   Source File: ClassObfuscator.java    License: Apache License 2.0 6 votes vote down vote up
public void visitProgramClass(ProgramClass programClass)
{
    // Does the class already have a new name?
    String newClassName = newClassName(programClass);
    if (newClassName != null)
    {
        // Remember not to use this name.
        classNamesToAvoid.add(mixedCaseClassName(newClassName));

        // Are we not aggressively repackaging all obfuscated classes?
        if (repackageClasses == null ||
            !allowAccessModification)
        {
            String className = programClass.getName();

            // Keep the package name for all other classes in the same
            // package. Do this recursively if we're not doing any
            // repackaging.
            mapPackageName(className,
                           newClassName,
                           repackageClasses        == null &&
                           flattenPackageHierarchy == null);
        }
    }
}
 
Example 8
Source Project: java-n-IDE-for-Android   Source File: TargetClassChanger.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Explicitly adds a new class constant for the given class in the given
 * program class.
 */
private int addNewClassConstant(ProgramClass programClass,
                                String       className,
                                Clazz        referencedClass)
{
    ConstantPoolEditor constantPoolEditor =
        new ConstantPoolEditor(programClass);

    int nameIndex =
        constantPoolEditor.addUtf8Constant(className);

    int classConstantIndex =
        constantPoolEditor.addConstant(new ClassConstant(nameIndex,
                                                         referencedClass));
    return classConstantIndex;
}
 
Example 9
Source Project: java-n-IDE-for-Android   Source File: ClassPrinter.java    License: Apache License 2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    println(visitorInfo(programMethod) + " " +
            "Method:       " +
            programMethod.getName(programClass) +
            programMethod.getDescriptor(programClass));

    indent();
    println("Access flags: 0x" + Integer.toHexString(programMethod.u2accessFlags));
    println("  = " +
            ClassUtil.externalFullMethodDescription(programClass.getName(),
                                                    programMethod.u2accessFlags,
                                                    programMethod.getName(programClass),
                                                    programMethod.getDescriptor(programClass)));

    visitMember(programClass, programMethod);
    outdent();
}
 
Example 10
public void visitAnnotation(Clazz clazz, Annotation annotation)
{
    // Compute the new type name.
    String typeName    = clazz.getString(annotation.u2typeIndex);
    String newTypeName = newDescriptor(typeName,
                                       annotation.referencedClasses);

    if (!typeName.equals(newTypeName))
    {
        // Refer to a new Utf8 entry.
        annotation.u2typeIndex =
            new ConstantPoolEditor((ProgramClass)clazz).addUtf8Constant(newTypeName);
    }

    // Fix the element values.
    annotation.elementValuesAccept(clazz, this);
}
 
Example 11
Source Project: bazel   Source File: ElementValuesEditor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new ElementValuesEditor that will edit element values in the
 * given target annotation.
 */
public ElementValuesEditor(ProgramClass targetClass,
                           Annotation   targetAnnotation,
                           boolean      replaceElementValues)
{
    this.targetClass             = targetClass;
    this.targetAnnotation        = targetAnnotation;
    this.targetArrayElementValue = null;
    this.replaceElementValues    = replaceElementValues;
}
 
Example 12
Source Project: atlas   Source File: ClassStructVisitor.java    License: Apache License 2.0 5 votes vote down vote up
private ClassStruct getOrCreateClassStruct(ProgramClass programClass) {
    ClassStruct classStruct = visitorDTO.classStructMap.get(programClass.getName());
    if (null == classStruct) {
        classStruct = new ClassStruct();
        visitorDTO.classStructMap.put(programClass.getName(), classStruct);
    }
    return classStruct;
}
 
Example 13
Source Project: java-n-IDE-for-Android   Source File: MemberAccessFilter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    if (accepted(programMethod.getAccessFlags()))
    {
        memberVisitor.visitProgramMethod(programClass, programMethod);
    }
}
 
Example 14
public void visitProgramClass(ProgramClass programClass)
{
    programClass.hierarchyAccept(visitThisClass,
                                 visitSuperClass,
                                 visitInterfaces,
                                 visitSubclasses,
                                 classVisitor);
}
 
Example 15
Source Project: java-n-IDE-for-Android   Source File: UsageMarker.java    License: Apache License 2.0 5 votes vote down vote up
protected void markProgramClassBody(ProgramClass programClass)
{
    // Mark this class's name.
    markConstant(programClass, programClass.u2thisClass);

    // Mark the superclass.
    if (programClass.u2superClass != 0)
    {
        markConstant(programClass, programClass.u2superClass);
    }

    // Give the interfaces preliminary marks.
    programClass.hierarchyAccept(false, false, true, false,
                                 interfaceUsageMarker);

    // Explicitly mark the <clinit> method.
    programClass.methodAccept(ClassConstants.INTERNAL_METHOD_NAME_CLINIT,
                              ClassConstants.INTERNAL_METHOD_TYPE_CLINIT,
                              this);

    // Explicitly mark the parameterless <init> method.
    programClass.methodAccept(ClassConstants.INTERNAL_METHOD_NAME_INIT,
                              ClassConstants.INTERNAL_METHOD_TYPE_INIT,
                              this);

    // Process all class members that have already been marked as possibly used.
    programClass.fieldsAccept(possiblyUsedMemberUsageMarker);
    programClass.methodsAccept(possiblyUsedMemberUsageMarker);

    // Mark the attributes.
    programClass.attributesAccept(this);
}
 
Example 16
Source Project: java-n-IDE-for-Android   Source File: MappingPrinter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitProgramClass(ProgramClass programClass)
{
    String name    = programClass.getName();
    String newName = ClassObfuscator.newClassName(programClass);

    ps.println(ClassUtil.externalClassName(name) +
               " -> " +
               ClassUtil.externalClassName(newName) +
               ":");

    // Print out the class members.
    programClass.fieldsAccept(this);
    programClass.methodsAccept(this);
}
 
Example 17
Source Project: java-n-IDE-for-Android   Source File: CaughtClassFilter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitProgramClass(ProgramClass programClass)
{
    if (CaughtClassMarker.isCaught(programClass))
    {
        classVisitor.visitProgramClass(programClass);
    }
}
 
Example 18
Source Project: java-n-IDE-for-Android   Source File: UsageMarker.java    License: Apache License 2.0 5 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Has the method already been referenced?
    if (isPossiblyUsed(programMethod))
    {
        markAsUsed(programMethod);

        // Mark the method body.
        markProgramMethodBody(programClass, programMethod);

        // Note that, if the method has been marked as possibly used,
        // the method hierarchy has already been marked (cfr. below).
    }
}
 
Example 19
Source Project: atlas   Source File: ClassStructVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitProgramField(ProgramClass programClass, ProgramField programField) {
    LibraryClazzInfo libraryClazzInfo = getOrCreateLibraryClazzInfo(programClass);
    if (null != libraryClazzInfo) {
        libraryClazzInfo.addField(programField.getName(programClass));
    }
}
 
Example 20
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    if (!KeepMarker.isKept(programMethod))
    {
        MethodOptimizationInfo.setMethodOptimizationInfo(programClass,
                                                         programMethod);
    }
}
 
Example 21
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Special cases: <clinit> and <init> are always kept unchanged.
    // We can ignore them here.
    String name = programMethod.getName(programClass);
    if (name.equals(ClassConstants.INTERNAL_METHOD_NAME_CLINIT) ||
        name.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT))
    {
        return;
    }

    visitMember(programClass, programMethod, false);
}
 
Example 22
Source Project: proguard   Source File: ClassElementSorter.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void visitProgramClass(ProgramClass programClass)
    {
        programClass.accept(constantPoolSorter);
        programClass.accept(interfaceSorter);
//      programClass.accept(classMemberSorter);
        programClass.accept(attributeSorter);
    }
 
Example 23
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    hasBeenFixed = !descriptor.equals(programMethod.getDescriptor(programClass));

    if (DEBUG)
    {
        if (hasBeenFixed)
        {
            System.out.println("DuplicateInitializerInvocationFixer:");
            System.out.println("  ["+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"] ("+ClassUtil.externalClassAccessFlags(programMethod.getAccessFlags())+") referenced by:");
        }
    }
}
 
Example 24
public void visitProgramClass(ProgramClass programClass)
{
    programClass.superClassConstantAccept(new ReferencedClassVisitor(
                                          new SubclassTraveler(
                                          new ClassMerger(programClass,
                                                          allowAccessModification,
                                                          mergeInterfacesAggressively,
                                                          extraClassVisitor))));
}
 
Example 25
public void visitProgramClass(ProgramClass programClass)
{
    // Print the name of this class.
    ps.println(ClassUtil.externalClassName(programClass.getName()));

    // Print the reason for keeping this class.
    printReason(programClass);
}
 
Example 26
public void visitProgramClass(ProgramClass programClass)
{
    // Fix the constant pool.
    programClass.constantPoolEntriesAccept(this);

    // Fix class members.
    programClass.fieldsAccept(this);
    programClass.methodsAccept(this);

    // Fix the attributes.
    programClass.attributesAccept(this);
}
 
Example 27
Source Project: java-n-IDE-for-Android   Source File: UsedMemberFilter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitProgramField(ProgramClass programClass, ProgramField programField)
{
    if (usageMarker.isUsed(programField))
    {
        memberVisitor.visitProgramField(programClass, programField);
    }
}
 
Example 28
Source Project: java-n-IDE-for-Android   Source File: ClassVersionSetter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitProgramClass(ProgramClass programClass)
{
    if (programClass.u4version > classVersion &&
        newerClassVersions != null)
    {
        newerClassVersions.add(new Integer(programClass.u4version));
    }

    programClass.u4version = classVersion;
}
 
Example 29
Source Project: java-n-IDE-for-Android   Source File: UsagePrinter.java    License: Apache License 2.0 5 votes vote down vote up
public void visitProgramField(ProgramClass programClass, ProgramField programField)
{
    if (usageMarker.isUsed(programField) ^ printUnusedItems)
    {
        printClassNameHeader();

        ps.println("    " +
                   lineNumberRange(programClass, programField) +
                   ClassUtil.externalFullFieldDescription(
                       programField.getAccessFlags(),
                       programField.getName(programClass),
                       programField.getDescriptor(programClass)));
    }
}
 
Example 30
Source Project: java-n-IDE-for-Android   Source File: AttributeAdder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new AttributeAdder that will copy attributes into the given
 * target class member.
 */
public AttributeAdder(ProgramClass  targetClass,
                      ProgramMember targetMember,
                      boolean       replaceAttributes)
{
    this(targetClass, targetMember, null, replaceAttributes);
}