Home | History | Annotate | Download | only in editor
      1 /*
      2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
      3  *             of Java bytecode.
      4  *
      5  * Copyright (c) 2002-2014 Eric Lafortune (eric (at) graphics.cornell.edu)
      6  *
      7  * This program is free software; you can redistribute it and/or modify it
      8  * under the terms of the GNU General Public License as published by the Free
      9  * Software Foundation; either version 2 of the License, or (at your option)
     10  * any later version.
     11  *
     12  * This program is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
     15  * more details.
     16  *
     17  * You should have received a copy of the GNU General Public License along
     18  * with this program; if not, write to the Free Software Foundation, Inc.,
     19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
     20  */
     21 package proguard.classfile.editor;
     22 
     23 import proguard.classfile.*;
     24 import proguard.classfile.attribute.*;
     25 import proguard.classfile.attribute.visitor.AttributeVisitor;
     26 import proguard.classfile.util.SimplifiedVisitor;
     27 import proguard.classfile.visitor.*;
     28 
     29 import java.util.*;
     30 
     31 /**
     32  * This ClassVisitor sorts the attributes of the classes that it visits.
     33  * The sorting order is based on the types of the attributes.
     34  *
     35  * @author Eric Lafortune
     36  */
     37 public class AttributeSorter
     38 extends      SimplifiedVisitor
     39 implements   ClassVisitor, MemberVisitor, AttributeVisitor, Comparator
     40 {
     41     // Implementations for ClassVisitor.
     42 
     43     public void visitProgramClass(ProgramClass programClass)
     44     {
     45         // Sort the attributes.
     46         Arrays.sort(programClass.attributes, 0, programClass.u2attributesCount, this);
     47 
     48         // Sort the attributes of the class members.
     49         programClass.fieldsAccept(this);
     50         programClass.methodsAccept(this);
     51     }
     52 
     53 
     54     // Implementations for MemberVisitor.
     55 
     56     public void visitProgramMember(ProgramClass programClass, ProgramMember programMember)
     57     {
     58         // Sort the attributes.
     59         Arrays.sort(programMember.attributes, 0, programMember.u2attributesCount, this);
     60 
     61         // Sort the attributes of the attributes.
     62         programMember.attributesAccept(programClass, this);
     63     }
     64 
     65 
     66     // Implementations for AttributeVisitor.
     67 
     68     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
     69 
     70 
     71     public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
     72     {
     73         // Sort the attributes.
     74         Arrays.sort(codeAttribute.attributes, 0, codeAttribute.u2attributesCount, this);
     75     }
     76 
     77 
     78     // Implementations for Comparator.
     79 
     80     public int compare(Object object1, Object object2)
     81     {
     82         Attribute attribute1 = (Attribute)object1;
     83         Attribute attribute2 = (Attribute)object2;
     84 
     85         return attribute1.u2attributeNameIndex < attribute2.u2attributeNameIndex ? -1 :
     86                attribute1.u2attributeNameIndex > attribute2.u2attributeNameIndex ?  1 :
     87                                                                                     0;
     88     }
     89 }
     90