Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * JBoss, Home of Professional Open Source
  * Copyright 2008, Red Hat, Inc., and individual contributors
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * 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.
 */
package org.jboss.weld.util.bytecode;
Utility class for working with constructors in the low level javassist API

Author(s):
Stuart Douglas
public class ConstructorUtils {
    private ConstructorUtils() {
    }

    
adds a constructor that calls super()
    public static void addDefaultConstructor(ClassFile fileList<DeferredBytecodeinitialValueBytecodefinal boolean useUnsafeInstantiators) {
        addConstructor(.new String[0], new String[0], fileinitialValueBytecodeuseUnsafeInstantiators);
    }

    
Adds a constructor that delegates to a super constructor with the same descriptor. The bytecode in initialValueBytecode will be executed at the start of the constructor and can be used to initialize fields to a default value. As the object is not properly constructed at this point this bytecode may not reference this (i.e. the variable at location 0)

Parameters:
returnType the constructor descriptor
exceptions any exceptions that are thrown
file the classfile to add the constructor to
initialValueBytecode bytecode that can be used to set initial values
    public static void addConstructor(String returnTypeString[] paramsString[] exceptionsClassFile fileList<DeferredBytecodeinitialValueBytecodefinal boolean useUnsafeInstantiators) {
        try {
            final String initMethodName = "<init>";
            final ClassMethod ctor = file.addMethod(.initMethodNamereturnTypeparams);
            ctor.addCheckedExceptions(exceptions);
            final CodeAttribute b = ctor.getCodeAttribute();
            for(final DeferredBytecode iv : initialValueBytecode) {
                iv.apply(b);
            }
            // we need to generate a constructor with a single invokespecial call
            // to the super constructor
            // to do this we need to push all the arguments on the stack first
            // local variables is the number of parameters +1 for this
            // if some of the parameters are wide this may go up.
            b.aload(0);
            b.loadMethodParameters();
            // now we have the parameters on the stack
            b.invokespecial(file.getSuperclass(), initMethodName, DescriptorUtils.getMethodDescriptor(paramsreturnType));
            if(!useUnsafeInstantiators) {
                // now set constructed to true
                b.aload(0);
                b.iconst(1);
            }
            b.returnInstruction();
        }  catch (DuplicateMemberException e) {
            throw new RuntimeException(e);
        }
    }
New to GrepCode? Check out our FAQ X