View Javadoc
1   /*-
2    * #%L
3    * io.earcam.instrumental.agent.defy
4    * %%
5    * Copyright (C) 2018 earcam
6    * %%
7    * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8    * 
9    * You <b>must</b> choose to accept, in full - any individual or combination of 
10   * the following licenses:
11   * <ul>
12   * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13   * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14   * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15   * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16   * </ul>
17   * #L%
18   */
19  package io.earcam.instrumental.agent.defy;
20  
21  import static org.objectweb.asm.Opcodes.ACC_FINAL;
22  import static org.objectweb.asm.Opcodes.ACC_INTERFACE;
23  import static org.objectweb.asm.Opcodes.ASM6;
24  
25  import org.objectweb.asm.ClassVisitor;
26  import org.objectweb.asm.FieldVisitor;
27  import org.objectweb.asm.MethodVisitor;
28  
29  class RemoveFinalVisitor extends ClassVisitor {
30  
31  	private boolean isInterface;
32  
33  
34  	RemoveFinalVisitor(ClassVisitor cv)
35  	{
36  		super(ASM6, cv);
37  	}
38  
39  
40  	@Override
41  	public void visit(int version, int access, String name, String signature, String superName, String[] interfaces)
42  	{
43  		isInterface = isInterface(access);
44  		cv.visit(version, removeFinalModifier(access), name, signature, superName, interfaces);
45  	}
46  
47  
48  	private boolean isInterface(int access)
49  	{
50  		return (access & ACC_INTERFACE) == ACC_INTERFACE;
51  	}
52  
53  
54  	private int removeFinalModifier(int access)
55  	{
56  		return isInterface ? access : access & ~ACC_FINAL;
57  	}
58  
59  
60  	@Override
61  	public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions)
62  	{
63  		return super.visitMethod(removeFinalModifier(access), name, desc, signature, exceptions);
64  	}
65  
66  
67  	@Override
68  	public FieldVisitor visitField(int access, String name, String desc, String signature, Object value)
69  	{
70  		return super.visitField(removeFinalModifier(access), name, desc, signature, value);
71  	}
72  }