|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.hdfs.patch; |
| 11 | + |
| 12 | +import org.objectweb.asm.ClassVisitor; |
| 13 | +import org.objectweb.asm.ClassWriter; |
| 14 | +import org.objectweb.asm.MethodVisitor; |
| 15 | +import org.objectweb.asm.Type; |
| 16 | + |
| 17 | +import static org.objectweb.asm.Opcodes.ASM9; |
| 18 | +import static org.objectweb.asm.Opcodes.INVOKESTATIC; |
| 19 | +import static org.objectweb.asm.Opcodes.POP; |
| 20 | + |
| 21 | +class SubjectGetSubjectPatcher extends ClassVisitor { |
| 22 | + SubjectGetSubjectPatcher(ClassWriter classWriter) { |
| 23 | + super(ASM9, classWriter); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { |
| 28 | + return new ReplaceCallMethodVisitor(super.visitMethod(access, name, descriptor, signature, exceptions), name, access, descriptor); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Replaces calls to Subject.getSubject(context); with calls to Subject.current(); |
| 33 | + */ |
| 34 | + private static class ReplaceCallMethodVisitor extends MethodVisitor { |
| 35 | + private static final String SUBJECT_CLASS_INTERNAL_NAME = "javax/security/auth/Subject"; |
| 36 | + private static final String METHOD_NAME = "getSubject"; |
| 37 | + |
| 38 | + ReplaceCallMethodVisitor(MethodVisitor methodVisitor, String name, int access, String descriptor) { |
| 39 | + super(ASM9, methodVisitor); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { |
| 44 | + if (opcode == INVOKESTATIC && SUBJECT_CLASS_INTERNAL_NAME.equals(owner) && METHOD_NAME.equals(name)) { |
| 45 | + // Get rid of the extra arg on the stack |
| 46 | + mv.visitInsn(POP); |
| 47 | + // Call Subject.current() |
| 48 | + mv.visitMethodInsn( |
| 49 | + INVOKESTATIC, |
| 50 | + SUBJECT_CLASS_INTERNAL_NAME, |
| 51 | + "current", |
| 52 | + Type.getMethodDescriptor(Type.getObjectType(SUBJECT_CLASS_INTERNAL_NAME)), |
| 53 | + false |
| 54 | + ); |
| 55 | + } else { |
| 56 | + super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments