org.openhab.patch.ClassAdapter.java Source code

Java tutorial

Introduction

Here is the source code for org.openhab.patch.ClassAdapter.java

Source

/**
 * Copyright (c) 2010-2016 by the respective copyright holders.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.patch;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class ClassAdapter extends ClassVisitor implements Opcodes {

    public ClassAdapter(final ClassVisitor cv) {
        super(ASM5, cv);
    }

    /**
     * Modify the findResources method to always return an empty list.
     */
    @Override
    public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
            final String[] exceptions) {

        MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);

        if (mv != null && "findResources".equals(name)) {

            mv.visitFieldInsn(GETSTATIC, "org/ops4j/pax/web/utils/ClassPathUtil", "LOG", "Lorg/slf4j/Logger;");
            mv.visitLdcInsn("Ignoring bundle scan for {} {}.");
            mv.visitVarInsn(ALOAD, 1);
            mv.visitVarInsn(ALOAD, 2);
            mv.visitMethodInsn(INVOKEINTERFACE, "org/slf4j/Logger", "info",
                    "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V", true);
            mv.visitFieldInsn(GETSTATIC, "java/util/Collections", "EMPTY_LIST", "Ljava/util/List;");
            mv.visitInsn(ARETURN);
            mv.visitMaxs(4, 4);
            mv.visitEnd();
        }

        return mv;
    }
}