com.github.veithen.cosmos.osgi.runtime.MemberInjector.java Source code

Java tutorial

Introduction

Here is the source code for com.github.veithen.cosmos.osgi.runtime.MemberInjector.java

Source

/*-
 * #%L
 * Cosmos
 * %%
 * Copyright (C) 2012 - 2018 Andreas Veithen
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.github.veithen.cosmos.osgi.runtime;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;

final class MemberInjector extends ClassVisitor {
    private final ClassNode classNode;
    private String name;
    private boolean fieldsInjected;

    MemberInjector(ClassVisitor cv, ClassNode classNode) {
        super(Opcodes.ASM6, cv);
        this.classNode = classNode;
    }

    @Override
    public void visit(int version, int access, String name, String signature, String superName,
            String[] interfaces) {
        super.visit(version, access, name, signature, superName, interfaces);
        this.name = name;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        if (!fieldsInjected) {
            for (FieldNode field : classNode.fields) {
                field.accept(cv);
            }
            fieldsInjected = true;
        }
        for (MethodNode method : classNode.methods) {
            if (name.equals(method.name) && desc.equals(method.desc)) {
                return null;
            }
        }
        return super.visitMethod(access, name, desc, signature, exceptions);
    }

    @Override
    public void visitEnd() {
        for (MethodNode method : classNode.methods) {
            method.accept(new RelocatingClassVisitor(cv, classNode.name, name));
        }
        super.visitEnd();
    }
}