17fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe/*
27fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe * Copyright (C) 2014 The Android Open Source Project
37fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe *
47fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
57fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe * you may not use this file except in compliance with the License.
67fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe * You may obtain a copy of the License at
77fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe *
87fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
97fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe *
107fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe * Unless required by applicable law or agreed to in writing, software
117fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
127fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe * See the License for the specific language governing permissions and
147fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe * limitations under the License.
157fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe */
167fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
177fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampeimport org.objectweb.asm.*;
187fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampeimport org.objectweb.asm.tree.*;
197fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampeimport java.io.*;
207fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampeimport java.util.*;
217fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
227fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampepublic class Asm {
237fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  /*
247fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
257fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  Overall class access flags:
267fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
277fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0001 |  // public
287fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0010 |  // final
297fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0020 |  // super
307fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0200 |  // interface
317fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0400 |  // abstract
327fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x1000 |  // synthetic
337fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x2000 |  // annotation
347fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x4000 ;  // enum
357fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
367fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  */
377fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
387fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  public final static int INTERFACE_DEFINED_BITS =
397fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0001 |  // public, may be set.
407fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0010 |  // final, must not be set.
417fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0020 |  // super, must not be set.
427fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0200 |  // interface, must be set.
437fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0400 |  // abstract, must be set.
447fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x1000 |  // synthetic, may be set.
457fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x2000 |  // annotation, may be set (annotation implies interface)
467fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x4000 ;  // enum, must not be set.
477fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
487fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  public final static int CLASS_DEFINED_BITS =
497fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0001 |  // public, may be set.
507fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0010 |  // final, may be set.
517fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0020 |  // super, may be set.
527fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0200 |  // interface, must not be set.
537fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x0400 |  // abstract, may be set.
547fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x1000 |  // synthetic, may be set.
557fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x2000 |  // annotation, must not be set.
567fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      0x4000 ;  // enum, may be set.
577fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
587fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  public final static int FIELD_DEFINED_BITS =
597fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0001 |  // public
607fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0002 |  // private
617fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0004 |  // protected
627fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0008 |  // static
637fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0010 |  // final
647fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0040 |  // volatile
657fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0080 |  // transient
667fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x1000 |  // synthetic
677fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x4000 ;  // enum
687fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
697fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  public final static int METHOD_DEFINED_BITS =
707fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0001 |  // public
717fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0002 |  // private
727fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0004 |  // protected
737fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0008 |  // static
747fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0010 |  // final
757fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0020 |  // synchronized
767fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0040 |  // bridge
777fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0080 |  // varargs
787fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0100 |  // native
797fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0400 |  // abstract
807fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x0800 |  // strictfp
817fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe       0x1000 ;  // synthetic
827fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
837fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  public static void main(String args[]) throws Exception {
847fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    modify("Inf");
857fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    modify("NonInf");
867fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  }
877fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
887fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  private static void modify(String clazz) throws Exception {
897fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    ClassNode classNode = new ClassNode();
907fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    ClassReader cr = new ClassReader(clazz);
917fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    cr.accept(classNode, 0);
927fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
937fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    modify(classNode);
947fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
957fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    ClassWriter cw = new ClassWriter(0);
967fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    classNode.accept(cw);
977fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    byte[] b = cw.toByteArray();
987fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    OutputStream out = new FileOutputStream(clazz + ".out");
997fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    out.write(b, 0, b.length);
1007fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    out.close();
1017fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  }
1027fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
1037fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  private static void modify(ClassNode classNode) throws Exception {
1047fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    int classFlagsOr = 0xFFFF;
1057fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    // Check whether classNode is an interface or class.
1067fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    if ((classNode.access & Opcodes.ACC_INTERFACE) == 0) {
1077fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      classFlagsOr ^= CLASS_DEFINED_BITS;
1087fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    } else {
1097fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      classFlagsOr ^= INTERFACE_DEFINED_BITS;
1107fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    }
1117fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    classNode.access |= classFlagsOr;
1127fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
1137fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    // Fields.
1147fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    int fieldFlagsOr = 0xFFFF ^ FIELD_DEFINED_BITS;
1157fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    for (FieldNode fieldNode : (List<FieldNode>)classNode.fields) {
1167fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      fieldNode.access |= fieldFlagsOr;
1177fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    }
1187fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe
1197fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    // Methods.
1207fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    int methodFlagsOr = 0xFFFF ^ METHOD_DEFINED_BITS;
1217fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    for (MethodNode methodNode :(List<MethodNode>) classNode.methods) {
1227fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe      methodNode.access |= methodFlagsOr;
1237fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe    }
1247fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe  }
1257fc8f90b7160e879143be5cfd6ea3df866398884Andreas Gampe}
126