Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you 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.
  */
 package org.apache.felix.scrplugin.helper;
 
 import java.io.File;
 
Helper class for injecting/generating accessor methods for unary references.
 
 public abstract class ClassModifier {

    
Add bind/unbind methods

Parameters:
className The class name in which the methods are injected
referenceName Name of the reference
fieldName Name of the field
typeName Name of the type
createBind Name of the bind method or null
createUnbind Name of the unbind method or null
outputDirectory Output directory where the class file is stored
Throws:
org.apache.felix.scrplugin.SCRDescriptorException
 
     public static void addMethods(final String className,
                            final String referenceName,
                            final String fieldName,
                            final String typeName,
                            final boolean createBind,
                            final boolean createUnbind,
                            final ClassLoader classLoader,
                            final String outputDirectory,
                            final Log logger)
     throws SCRDescriptorException {
         // now do byte code manipulation
         final String fileName = outputDirectory + . +  className.replace('.'.) + ".class";
         final ClassNode cn = new ClassNode();
         try {
             final ClassReader reader = new ClassReader(new FileInputStream(fileName));
             reader.accept(cn, 0);
 
             // For target Java7 and above use: ClassWriter.COMPUTE_MAXS  | ClassWriter.COMPUTE_FRAMES
             final int mask = (cn.version > 50 ? .  | . : 0);
             final ClassWriter writer = new ClassWriter(mask) {
 
                 @Override
                 protected String getCommonSuperClass(final String type1final String type2) {
                     Class<?> cd;
                     try {
                         c = classLoader.loadClass(type1.replace('/''.'));
                         d = classLoader.loadClass(type2.replace('/''.'));
                     } catch (final Exception e) {
                         throw new RuntimeException(e.toString(), e);
                     }
                     if (c.isAssignableFrom(d)) {
                         return type1;
                     }
                     if (d.isAssignableFrom(c)) {
                         return type2;
                     }
                     if (c.isInterface() || d.isInterface()) {
                         return "java/lang/Object";
                     }
                     do {
                         c = c.getSuperclass();
                     } while (!c.isAssignableFrom(d));
                     return c.getName().replace('.''/');
                 }
 
             };
 
             cn.accept(writer);
             if ( createBind ) {
                logger.debug("Adding bind " + className + " " + fieldName);
                createMethod(writerclassNamereferenceNamefieldNametypeNametrue);
            }
            if ( createUnbind ) {
                logger.debug("Adding unbind " + className + " " + fieldName);
                createMethod(writerclassNamereferenceNamefieldNametypeNamefalse);
            }
            final FileOutputStream fos = new FileOutputStream(fileName);
            fos.write(writer.toByteArray());
            fos.close();
        } catch (final Exception e) {
            throw new SCRDescriptorException("Unable to add methods to " + classNametypeNamee);
        }
    }
    private static void createMethod(final ClassWriter cwfinal String classNamefinal String referenceNamefinal String fieldNamefinal String typeNamefinal boolean bind) {
        final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.''/') + ";");
        final String methodName = (bind ? "" : "un") + "bind" + referenceName.substring(0, 1).toUpperCase() + referenceName.substring(1);
        final MethodVisitor mv = cw.visitMethod(.methodName"(" + type.toString() + ")V"nullnull);
        mv.visitVarInsn(., 0);
        if ( bind ) {
            mv.visitVarInsn(type.getOpcode(.), 1);
            mv.visitFieldInsn(.className.replace('.''/'), fieldNametype.toString());
        } else {
            mv.visitFieldInsn(.className.replace('.''/'), fieldNametype.toString());
            mv.visitVarInsn(., 1);
            final Label jmpLabel = new Label();
            mv.visitJumpInsn(.jmpLabel);
            mv.visitVarInsn(., 0);
            mv.visitInsn(.);
            mv.visitFieldInsn(.className.replace('.''/'), fieldNametype.toString());
            mv.visitLabel(jmpLabel);
        }
        mv.visitInsn(.);
        mv.visitMaxs(2, 2);
    }
New to GrepCode? Check out our FAQ X