Java Code Examples for proguard.classfile.ClassConstants#INTERNAL_ACC_STATIC

The following examples show how to use proguard.classfile.ClassConstants#INTERNAL_ACC_STATIC . 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
Source File: FieldOptimizationInfo.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public FieldOptimizationInfo(Clazz clazz, Field field)
{
    int accessFlags = field.getAccessFlags();

    isWritten =
    isRead    = (accessFlags & ClassConstants.INTERNAL_ACC_VOLATILE) != 0;

    if ((accessFlags & ClassConstants.INTERNAL_ACC_STATIC) != 0)
    {
        // See if we can initialize the static field with a constant value.
        field.accept(clazz, new AllAttributeVisitor(this));
    }

    if ((accessFlags & ClassConstants.INTERNAL_ACC_FINAL) == 0 &&
        value == null)
    {
        // Otherwise initialize the non-final field with the default value.
        value = initialValue(field.getDescriptor(clazz));
    }
}
 
Example 2
Source File: MethodOptimizationInfo.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new MethodOptimizationInfo for the given method.
 */
public MethodOptimizationInfo(Clazz clazz, Method method)
{
    // Set up an array of the right size for storing information about the
    // passed parameters.
    int parameterCount =
        ClassUtil.internalMethodParameterCount(method.getDescriptor(clazz));

    if ((method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) == 0)
    {
        parameterCount++;
    }

    if (parameterCount > 0)
    {
        parameters = new Value[parameterCount];
    }
}
 
Example 3
Source File: MethodStaticizer.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Is the 'this' parameter being used?
    if (!ParameterUsageMarker.isParameterUsed(programMethod, 0))
    {
        // Make the method static.
        programMethod.u2accessFlags =
            (programMethod.getAccessFlags() & ~ClassConstants.INTERNAL_ACC_FINAL) |
            ClassConstants.INTERNAL_ACC_STATIC;

        // Visit the method, if required.
        if (extraStaticMemberVisitor != null)
        {
            extraStaticMemberVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example 4
Source File: ConstantParameterFilter.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // All parameters of non-static methods are shifted by one in the local
    // variable frame.
    int firstParameterIndex =
        (programMethod.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
            0 : 1;

    int parameterCount =
        ClassUtil.internalMethodParameterCount(programMethod.getDescriptor(programClass));

    for (int index = firstParameterIndex; index < parameterCount; index++)
    {
        Value value = StoringInvocationUnit.getMethodParameterValue(programMethod, index);
        if (value != null &&
            value.isParticular())
        {
            constantParameterVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example 5
Source File: MethodImplementationTraveler.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private boolean isSpecial(Clazz clazz, Method method)
{
    return (method.getAccessFlags() &
            (ClassConstants.INTERNAL_ACC_PRIVATE |
             ClassConstants.INTERNAL_ACC_STATIC)) != 0 ||
           method.getName(clazz).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT);
}
 
Example 6
Source File: UsageMarker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Marks the hierarchy of implementing or overriding methods corresponding
 * to the given method, if any.
 */
protected void markMethodHierarchy(Clazz clazz, Method method)
{
    if ((method.getAccessFlags() &
         (ClassConstants.INTERNAL_ACC_PRIVATE |
          ClassConstants.INTERNAL_ACC_STATIC)) == 0)
    {
        clazz.accept(new ConcreteClassDownTraveler(
                     new ClassHierarchyTraveler(true, true, false, true,
                     new NamedMethodVisitor(method.getName(clazz),
                                            method.getDescriptor(clazz),
                     new MemberAccessFilter(0, ClassConstants.INTERNAL_ACC_PRIVATE | ClassConstants.INTERNAL_ACC_STATIC | ClassConstants.INTERNAL_ACC_ABSTRACT,
                     this)))));
    }
}
 
Example 7
Source File: MethodDescriptorShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a shrunk descriptor or signature of the given method.
 */
private String shrinkDescriptor(Method method, String descriptor)
{
    // All parameters of non-static methods are shifted by one in the local
    // variable frame.
    int parameterIndex =
        (method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
            0 : 1;

    // Go over the parameters.
    InternalTypeEnumeration internalTypeEnumeration =
        new InternalTypeEnumeration(descriptor);

    StringBuffer newDescriptorBuffer = new StringBuffer();

    newDescriptorBuffer.append(internalTypeEnumeration.formalTypeParameters());
    newDescriptorBuffer.append(ClassConstants.INTERNAL_METHOD_ARGUMENTS_OPEN);

    while (internalTypeEnumeration.hasMoreTypes())
    {
        String type = internalTypeEnumeration.nextType();
        if (ParameterUsageMarker.isParameterUsed(method, parameterIndex))
        {
            newDescriptorBuffer.append(type);
        }
        else if (DEBUG)
        {
            System.out.println("  Deleting parameter #"+parameterIndex+" ["+type+"]");
        }

        parameterIndex += ClassUtil.isInternalCategory2Type(type) ? 2 : 1;
    }

    newDescriptorBuffer.append(ClassConstants.INTERNAL_METHOD_ARGUMENTS_CLOSE);
    newDescriptorBuffer.append(internalTypeEnumeration.returnType());

    return newDescriptorBuffer.toString();
}
 
Example 8
Source File: MethodFinalizer.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    String name = programMethod.getName(programClass);

    // If the method is not already private/static/final/abstract,
    // and it is not a constructor,
    // and its class is final,
    //     or it is not being kept and it is not overridden,
    // then make it final.
    if ((programMethod.u2accessFlags & (ClassConstants.INTERNAL_ACC_PRIVATE |
                                        ClassConstants.INTERNAL_ACC_STATIC  |
                                        ClassConstants.INTERNAL_ACC_FINAL   |
                                        ClassConstants.INTERNAL_ACC_ABSTRACT)) == 0 &&
        !name.equals(ClassConstants.INTERNAL_METHOD_NAME_INIT)                      &&
        ((programClass.u2accessFlags & ClassConstants.INTERNAL_ACC_FINAL) != 0 ||
         (!KeepMarker.isKept(programMethod) &&
          (programClass.subClasses == null ||
           !memberFinder.isOverriden(programClass, programMethod)))))
    {
        programMethod.u2accessFlags |= ClassConstants.INTERNAL_ACC_FINAL;

        // Visit the method, if required.
        if (extraMemberVisitor != null)
        {
            extraMemberVisitor.visitProgramMethod(programClass, programMethod);
        }
    }
}
 
Example 9
Source File: EvaluationShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // Get the total size of the parameters.
    int parameterSize = ParameterUsageMarker.getParameterSize(programMethod);

    // Make the method invocation static, if possible.
    if ((programMethod.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) == 0 &&
        !ParameterUsageMarker.isParameterUsed(programMethod, 0))
    {
        replaceByStaticInvocation(programClass,
                                  invocationOffset,
                                  invocationInstruction);
    }

    // Remove unused parameters.
    for (int index = 0; index < parameterSize; index++)
    {
        if (!ParameterUsageMarker.isParameterUsed(programMethod, index))
        {
            TracedStack stack =
                partialEvaluator.getStackBefore(invocationOffset);

            int stackIndex = stack.size() - parameterSize + index;

            if (DEBUG)
            {
                System.out.println("  ["+invocationOffset+"] Ignoring parameter #"+index+" of "+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"] (stack entry #"+stackIndex+" ["+stack.getBottom(stackIndex)+"])");
                System.out.println("    Full stack: "+stack);
            }

            markStackSimplificationBefore(invocationOffset, stackIndex);
        }
    }
}
 
Example 10
Source File: MemberDescriptorSpecializer.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    // All parameters of non-static methods are shifted by one in the local
    // variable frame.
    int firstParameterIndex =
        (programMethod.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
            0 : 1;

    int parameterCount =
        ClassUtil.internalMethodParameterCount(programMethod.getDescriptor(programClass));

    int classIndex = 0;

    // Go over the parameters.
    for (int parameterIndex = firstParameterIndex; parameterIndex < parameterCount; parameterIndex++)
    {
        Value parameterValue = StoringInvocationUnit.getMethodParameterValue(programMethod, parameterIndex);
         if (parameterValue.computationalType() == Value.TYPE_REFERENCE)
         {
             Clazz referencedClass = parameterValue.referenceValue().getReferencedClass();
             if (programMethod.referencedClasses[classIndex] != referencedClass)
             {
                 if (DEBUG)
                 {
                     System.out.println("MemberDescriptorSpecializer: "+programClass.getName()+"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass));
                     System.out.println("  "+programMethod.referencedClasses[classIndex].getName()+" -> "+referencedClass.getName());
                 }

                 programMethod.referencedClasses[classIndex] = referencedClass;

                 // Visit the method, if required.
                 if (extraParameterMemberVisitor != null)
                 {
                     extraParameterMemberVisitor.visitProgramMethod(programClass, programMethod);
                 }
             }

             classIndex++;
         }
    }
}
 
Example 11
Source File: MethodDescriptorShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Shrinks the array of referenced classes of the given method.
 */
private Clazz[] shrinkReferencedClasses(Method  method,
                                        String  descriptor,
                                        Clazz[] referencedClasses)
{
    if (referencedClasses != null)
    {
        // All parameters of non-static methods are shifted by one in the local
        // variable frame.
        int parameterIndex =
            (method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
                0 : 1;

        int referencedClassIndex    = 0;
        int newReferencedClassIndex = 0;

        // Go over the parameters.
        InternalTypeEnumeration internalTypeEnumeration =
            new InternalTypeEnumeration(descriptor);

        // Also look at the formal type parameters.
        String type  = internalTypeEnumeration.formalTypeParameters();
        int    count = new DescriptorClassEnumeration(type).classCount();
        for (int counter = 0; counter < count; counter++)
        {
            referencedClasses[newReferencedClassIndex++] =
                referencedClasses[referencedClassIndex++];
        }

        while (internalTypeEnumeration.hasMoreTypes())
        {
            // Consider the classes referenced by this parameter type.
            type  = internalTypeEnumeration.nextType();
            count = new DescriptorClassEnumeration(type).classCount();

            if (ParameterUsageMarker.isParameterUsed(method, parameterIndex))
            {
                // Copy the referenced classes.
                for (int counter = 0; counter < count; counter++)
                {
                    referencedClasses[newReferencedClassIndex++] =
                        referencedClasses[referencedClassIndex++];
                }
            }
            else
            {
                // Skip the referenced classes.
                referencedClassIndex += count;
            }

            parameterIndex += ClassUtil.isInternalCategory2Type(type) ? 2 : 1;
        }

        // Also look at the return value.
        type  = internalTypeEnumeration.returnType();
        count = new DescriptorClassEnumeration(type).classCount();
        for (int counter = 0; counter < count; counter++)
        {
            referencedClasses[newReferencedClassIndex++] =
                referencedClasses[referencedClassIndex++];
        }

        // Clear the unused entries.
        while (newReferencedClassIndex < referencedClassIndex)
        {
            referencedClasses[newReferencedClassIndex++] = null;
        }
    }

    return referencedClasses;
}
 
Example 12
Source File: MethodDescriptorShrinker.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitAnyParameterAnnotationsAttribute(Clazz clazz, Method method, ParameterAnnotationsAttribute parameterAnnotationsAttribute)
{
    int[]          annotationsCounts = parameterAnnotationsAttribute.u2parameterAnnotationsCount;
    Annotation[][] annotations       = parameterAnnotationsAttribute.parameterAnnotations;

    // All parameters of non-static methods are shifted by one in the local
    // variable frame.
    int parameterIndex =
        (method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
            0 : 1;

    int annotationIndex    = 0;
    int newAnnotationIndex = 0;

    // Go over the parameters.
    String descriptor = method.getDescriptor(clazz);
    InternalTypeEnumeration internalTypeEnumeration =
        new InternalTypeEnumeration(descriptor);

    while (internalTypeEnumeration.hasMoreTypes())
    {
        String type = internalTypeEnumeration.nextType();
        if (ParameterUsageMarker.isParameterUsed(method, parameterIndex))
        {
            annotationsCounts[newAnnotationIndex] = annotationsCounts[annotationIndex];
            annotations[newAnnotationIndex++]     = annotations[annotationIndex];
        }

        annotationIndex++;

        parameterIndex += ClassUtil.isInternalCategory2Type(type) ? 2 : 1;
    }

    // Update the number of parameters.
    parameterAnnotationsAttribute.u2parametersCount = newAnnotationIndex;

    // Clear the unused entries.
    while (newAnnotationIndex < annotationIndex)
    {
        annotationsCounts[newAnnotationIndex] = 0;
        annotations[newAnnotationIndex++]     = null;
    }
}
 
Example 13
Source File: MethodInliner.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
    {
        int accessFlags = programMethod.getAccessFlags();

        if (// Only inline the method if it is private, static, or final.
            (accessFlags & (ClassConstants.INTERNAL_ACC_PRIVATE |
                            ClassConstants.INTERNAL_ACC_STATIC  |
                            ClassConstants.INTERNAL_ACC_FINAL)) != 0                              &&

            // Only inline the method if it is not synchronized, etc.
            (accessFlags & (ClassConstants.INTERNAL_ACC_SYNCHRONIZED |
                            ClassConstants.INTERNAL_ACC_NATIVE       |
                            ClassConstants.INTERNAL_ACC_INTERFACE    |
                            ClassConstants.INTERNAL_ACC_ABSTRACT)) == 0                           &&

            // Don't inline an <init> method, except in an <init> method in the
            // same class.
//            (!programMethod.getName(programClass).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT) ||
//             (programClass.equals(targetClass) &&
//              targetMethod.getName(targetClass).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT))) &&
            !programMethod.getName(programClass).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT) &&

            // Don't inline a method into itself.
            (!programMethod.equals(targetMethod) ||
             !programClass.equals(targetClass))                                                   &&

            // Only inline the method if it isn't recursing.
            !inliningMethods.contains(programMethod)                                              &&

            // Only inline the method if its target class has at least the
            // same version number as the source class, in order to avoid
            // introducing incompatible constructs.
            targetClass.u4version >= programClass.u4version                                       &&

            // Only inline the method if it doesn't invoke a super method, or if
            // it is in the same class.
            (!SuperInvocationMarker.invokesSuperMethods(programMethod) ||
             programClass.equals(targetClass))                                                    &&

            // Only inline the method if it doesn't branch backward while there
            // are uninitialized objects.
            (!BackwardBranchMarker.branchesBackward(programMethod) ||
             uninitializedObjectCount == 0)                                                       &&

            // Only inline if the code access of the inlined method allows it.
            (allowAccessModification ||
             ((!AccessMethodMarker.accessesPrivateCode(programMethod) ||
               programClass.equals(targetClass)) &&

              (!AccessMethodMarker.accessesPackageCode(programMethod) ||
               ClassUtil.internalPackageName(programClass.getName()).equals(
               ClassUtil.internalPackageName(targetClass.getName())))))                           &&

//               (!AccessMethodMarker.accessesProtectedCode(programMethod) ||
//                targetClass.extends_(programClass) ||
//                targetClass.implements_(programClass)) ||
            (!AccessMethodMarker.accessesProtectedCode(programMethod) ||
             programClass.equals(targetClass))                                                    &&

            // Only inline the method if it doesn't catch exceptions, or if it
            // is invoked with an empty stack.
            (!CatchExceptionMarker.catchesExceptions(programMethod) ||
             emptyInvokingStack)                                                                  &&

            // Only inline the method if it comes from the a class with at most
            // a subset of the initialized superclasses.
            (programClass.equals(targetClass) ||
             initializedSuperClasses(targetClass).containsAll(initializedSuperClasses(programClass))))
        {                                                                                                   boolean oldInlining = inlining;
            inlining = true;
            inliningMethods.push(programMethod);

            // Inline the method body.
            programMethod.attributesAccept(programClass, this);

            // Update the optimization information of the target method.
            MethodOptimizationInfo info =
                MethodOptimizationInfo.getMethodOptimizationInfo(targetMethod);
            if (info != null)
            {
                info.merge(MethodOptimizationInfo.getMethodOptimizationInfo(programMethod));
            }

            inlining = oldInlining;
            inliningMethods.pop();
        }
        else if (programMethod.getName(programClass).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT))
        {
            uninitializedObjectCount--;
        }
    }
 
Example 14
Source File: VariableOptimizer.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
    {
//        DEBUG =
//            clazz.getName().equals("abc/Def") &&
//            method.getName(clazz).equals("abc");

        // Initialize the global arrays.
        initializeArrays(codeAttribute);

        // Analyze the liveness of the variables in the code.
        livenessAnalyzer.visitCodeAttribute(clazz, method, codeAttribute);

        // Trim the variables in the local variable tables, because even
        // clipping the tables individually may leave some inconsistencies
        // between them.
            codeAttribute.attributesAccept(clazz, method, this);

        int startIndex =
            (method.getAccessFlags() & ClassConstants.INTERNAL_ACC_STATIC) != 0 ||
            reuseThis ? 0 : 1;

        int parameterSize =
            ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz),
                                                  method.getAccessFlags());

        int variableSize = codeAttribute.u2maxLocals;
        int codeLength   = codeAttribute.u4codeLength;

        boolean remapping = false;

        // Loop over all variables.
        for (int oldIndex = 0; oldIndex < variableSize; oldIndex++)
        {
            // By default, the variable will be mapped onto itself.
            variableMap[oldIndex] = oldIndex;

            // Only try remapping the variable if it's not a parameter.
            if (oldIndex >= parameterSize &&
                oldIndex < MAX_VARIABLES_SIZE)
            {
                // Try to remap the variable to a variable with a smaller index.
                for (int newIndex = startIndex; newIndex < oldIndex; newIndex++)
                {
                    if (areNonOverlapping(oldIndex, newIndex, codeLength))
                    {
                        variableMap[oldIndex] = newIndex;

                        updateLiveness(oldIndex, newIndex, codeLength);

                        remapping = true;

                        // This variable has been remapped. Go to the next one.
                        break;
                    }
                }
            }
        }

        // Have we been able to remap any variables?
        if (remapping)
        {
            if (DEBUG)
            {
                System.out.println("VariableOptimizer: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
                for (int index= 0; index < variableSize; index++)
                {
                    System.out.println("  v"+index+" -> "+variableMap[index]);
                }
            }

            // Remap the variables.
            variableRemapper.setVariableMap(variableMap);
            variableRemapper.visitCodeAttribute(clazz, method, codeAttribute);

            // Visit the method, if required.
            if (extraVariableMemberVisitor != null)
            {
                method.accept(clazz, extraVariableMemberVisitor);
            }
        }
        else
        {
            // Just clean up any empty variables.
            variableCleaner.visitCodeAttribute(clazz, method, codeAttribute);
        }
    }
 
Example 15
Source File: TailRecursionSimplifier.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
    {
        int accessFlags = method.getAccessFlags();

        if (// Only check the method if it is private, static, or final.
            (accessFlags & (ClassConstants.INTERNAL_ACC_PRIVATE |
                            ClassConstants.INTERNAL_ACC_STATIC  |
                            ClassConstants.INTERNAL_ACC_FINAL)) != 0 &&

            // Only check the method if it is not synchronized, etc.
            (accessFlags & (ClassConstants.INTERNAL_ACC_SYNCHRONIZED |
                            ClassConstants.INTERNAL_ACC_NATIVE       |
                            ClassConstants.INTERNAL_ACC_INTERFACE    |
                            ClassConstants.INTERNAL_ACC_ABSTRACT)) == 0)
        {
//            codeAttributeComposer.DEBUG = DEBUG =
//                clazz.getName().equals("abc/Def") &&
//                method.getName(clazz).equals("abc");

            targetMethod = method;
            inlinedAny   = false;
            codeAttributeComposer.reset();

            // The code may expand, due to expanding constant and variable
            // instructions.
            codeAttributeComposer.beginCodeFragment(codeAttribute.u4codeLength);

            // Copy the instructions.
            codeAttribute.instructionsAccept(clazz, method, this);

            // Update the code attribute if any code has been inlined.
            if (inlinedAny)
            {
                // Copy the exceptions.
                codeAttribute.exceptionsAccept(clazz, method, this);

                // Append a label just after the code.
                codeAttributeComposer.appendLabel(codeAttribute.u4codeLength);

                codeAttributeComposer.endCodeFragment();

                codeAttributeComposer.visitCodeAttribute(clazz, method, codeAttribute);
            }
        }
    }
 
Example 16
Source File: ParameterUsageMarker.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
{
    int parameterSize =
        ClassUtil.internalMethodParameterSize(programMethod.getDescriptor(programClass),
                                              programMethod.getAccessFlags());

    if (parameterSize > 0)
    {
        int accessFlags = programMethod.getAccessFlags();

        // Must we mark the 'this' parameter?
        if (markThisParameter &&
            (accessFlags & ClassConstants.INTERNAL_ACC_STATIC) == 0)
        {
            // Mark the 'this' parameter.
            markParameterUsed(programMethod, 0);
        }

        // Must we mark all other parameters?
        if (markAllParameters)
        {
            // Mark all parameters, without the 'this' parameter.
            markUsedParameters(programMethod,
                               (accessFlags & ClassConstants.INTERNAL_ACC_STATIC) != 0 ?
                                   -1L : -2L);
        }

        // Is it a native method?
        if ((accessFlags & ClassConstants.INTERNAL_ACC_NATIVE) != 0)
        {
            // Mark all parameters.
            markUsedParameters(programMethod, -1L);
        }

        // Is it an abstract method?
        else if ((accessFlags & ClassConstants.INTERNAL_ACC_ABSTRACT) != 0)
        {
            // Mark the 'this' parameter.
            markParameterUsed(programMethod, 0);
        }

        // Is it a non-native, concrete method?
        else
        {
            // Is the method not static, but synchronized, or can it have
            // other implementations, or is it a class instance initializer?
            if ((accessFlags & ClassConstants.INTERNAL_ACC_STATIC) == 0 &&
                ((accessFlags & ClassConstants.INTERNAL_ACC_SYNCHRONIZED) != 0 ||
                 programClass.mayHaveImplementations(programMethod)            ||
                 programMethod.getName(programClass).equals(ClassConstants.INTERNAL_METHOD_NAME_INIT)))
            {
                // Mark the 'this' parameter.
                markParameterUsed(programMethod, 0);
            }

            // Mark the parameters that are used by the code.
            programMethod.attributesAccept(programClass, this);
        }

        if (DEBUG)
        {
            System.out.print("ParameterUsageMarker: ["+programClass.getName() +"."+programMethod.getName(programClass)+programMethod.getDescriptor(programClass)+"]: ");
            for (int index = 0; index < parameterSize; index++)
            {
                System.out.print(isParameterUsed(programMethod, index) ? '+' : '-');
            }
            System.out.println();
        }

    }

    // Set the parameter size.
    setParameterSize(programMethod, parameterSize);
}
 
Example 17
Source File: MemberAdder.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public void visitProgramField(ProgramClass programClass, ProgramField programField)
    {
        String name        = programField.getName(programClass);
        String descriptor  = programField.getDescriptor(programClass);
        int    accessFlags = programField.getAccessFlags();

        // Does the target class already have such a field?
        ProgramField targetField = (ProgramField)targetClass.findField(name, descriptor);
        if (targetField != null)
        {
            // Is the field private or static?
            int targetAccessFlags = targetField.getAccessFlags();
            if ((targetAccessFlags &
                 (ClassConstants.INTERNAL_ACC_PRIVATE |
                  ClassConstants.INTERNAL_ACC_STATIC)) != 0)
            {
                if (DEBUG)
                {
                    System.out.println("MemberAdder: renaming field ["+targetClass+"."+targetField.getName(targetClass)+" "+targetField.getDescriptor(targetClass)+"]");
                }

                // Rename the private or static field.
                targetField.u2nameIndex =
                    constantPoolEditor.addUtf8Constant(newUniqueMemberName(name, targetClass.getName()));
            }
//            else
//            {
//                // Keep the non-private and non-static field, but update its
//                // contents, in order to keep any references to it valid.
//                if (DEBUG)
//                {
//                    System.out.println("MemberAdder: updating field ["+programClass+"."+programField.getName(programClass)+" "+programField.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
//                }
//
//                // Combine the access flags.
//                targetField.u2accessFlags = accessFlags | targetAccessFlags;
//
//                // Add and replace any attributes.
//                programField.attributesAccept(programClass,
//                                              new AttributeAdder(targetClass,
//                                                                 targetField,
//                                                                 true));
//
//                // Don't add a new field.
//                return;
//            }
        }

        if (DEBUG)
        {
            System.out.println("MemberAdder: copying field ["+programClass+"."+programField.getName(programClass)+" "+programField.getDescriptor(programClass)+"] into ["+targetClass.getName()+"]");
        }

        // Create a copy of the field.
        ProgramField newProgramField =
            new ProgramField(accessFlags,
                             constantAdder.addConstant(programClass, programField.u2nameIndex),
                             constantAdder.addConstant(programClass, programField.u2descriptorIndex),
                             0,
                             programField.u2attributesCount > 0 ?
                                 new Attribute[programField.u2attributesCount] :
                                 EMPTY_ATTRIBUTES,
                             programField.referencedClass);

        // Link to its visitor info.
        newProgramField.setVisitorInfo(programField);

        // Copy its attributes.
        programField.attributesAccept(programClass,
                                      new AttributeAdder(targetClass,
                                                         newProgramField,
                                                         false));

        // Add the completed field.
        classEditor.addField(newProgramField);
    }
 
Example 18
Source File: ClassUtil.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Converts internal method access flags into an external access description.
 * @param accessFlags the method access flags.
 * @param prefix      a prefix that is added to each access modifier.
 * @return the external method access description,
 *                    e.g. "public synchronized ".
 */
public static String externalMethodAccessFlags(int accessFlags, String prefix)
{
    if (accessFlags == 0)
    {
        return EMPTY_STRING;
    }

    StringBuffer string = new StringBuffer(50);

    if ((accessFlags & ClassConstants.INTERNAL_ACC_PUBLIC) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_PUBLIC).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_PRIVATE) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_PRIVATE).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_PROTECTED) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_PROTECTED).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_STATIC) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_STATIC).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_FINAL) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_FINAL).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_SYNCHRONIZED) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_SYNCHRONIZED).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_BRIDGE) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_BRIDGE).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_VARARGS) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_VARARGS).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_NATIVE) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_NATIVE).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_ABSTRACT) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_ABSTRACT).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_STRICT) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_STRICT).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_SYNTHETIC) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_SYNTHETIC).append(' ');
    }

    return string.toString();
}
 
Example 19
Source File: ClassUtil.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Converts internal field access flags into an external access description.
 * @param accessFlags the field access flags.
 * @param prefix      a prefix that is added to each access modifier.
 * @return the external field access description,
 *         e.g. "<code>public volatile </code>".
 */
public static String externalFieldAccessFlags(int accessFlags, String prefix)
{
    if (accessFlags == 0)
    {
        return EMPTY_STRING;
    }

    StringBuffer string = new StringBuffer(50);

    if ((accessFlags & ClassConstants.INTERNAL_ACC_PUBLIC) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_PUBLIC).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_PRIVATE) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_PRIVATE).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_PROTECTED) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_PROTECTED).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_STATIC) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_STATIC).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_FINAL) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_FINAL).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_VOLATILE) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_VOLATILE).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_TRANSIENT) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_TRANSIENT).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_SYNTHETIC) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_SYNTHETIC).append(' ');
    }

    return string.toString();
}
 
Example 20
Source File: ClassUtil.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Converts internal class access flags into an external access description.
 * @param accessFlags the class access flags.
 * @param prefix      a prefix that is added to each access modifier.
 * @return the external class access description,
 *         e.g. "<code>public final </code>".
 */
public static String externalClassAccessFlags(int accessFlags, String prefix)
{
    if (accessFlags == 0)
    {
        return EMPTY_STRING;
    }

    StringBuffer string = new StringBuffer(50);

    if ((accessFlags & ClassConstants.INTERNAL_ACC_PUBLIC) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_PUBLIC).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_PRIVATE) != 0)
    {
        // Only in InnerClasses attributes.
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_PRIVATE).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_PROTECTED) != 0)
    {
        // Only in InnerClasses attributes.
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_PROTECTED).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_STATIC) != 0)
    {
        // Only in InnerClasses attributes.
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_STATIC).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_FINAL) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_FINAL).append(' ');
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_ANNOTATTION) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_ANNOTATION);
    }
    if ((accessFlags & ClassConstants.INTERNAL_ACC_INTERFACE) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_INTERFACE).append(' ');
    }
    else if ((accessFlags & ClassConstants.INTERNAL_ACC_ENUM) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_ENUM).append(' ');
    }
    else if ((accessFlags & ClassConstants.INTERNAL_ACC_ABSTRACT) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_ABSTRACT).append(' ');
    }
    else if ((accessFlags & ClassConstants.INTERNAL_ACC_SYNTHETIC) != 0)
    {
        string.append(prefix).append(ClassConstants.EXTERNAL_ACC_SYNTHETIC).append(' ');
    }

    return string.toString();
}