0 Replies Latest reply: Jul 21, 2012 2:50 AM by Kalle Richter RSS

mapping of parameter and generic parameter types

Kalle Richter Newbie

Hallo together,

I'm wondering whether the length of the arrays returned by java.lang.reflect.Constructor.getParameterTypes() and getGenericParameterTypes() should be always equal, i.e. whether it's a bug if they're not or whether I'm doing something wrong. You can run the program below and you'll understand my problem immediately

 

package org.h9t2.javassistbug;

import java.io.*;
import java.util.List;
import java.util.Set;
import javassist.*;

public class App {
    private final static ClassPool pool = ClassPool.getDefault();

    public static void main( String[] args ) throws NotFoundException, IOException, CannotCompileException 
{        
        CtClass ctClass1 = pool.get(Clazz.class.getName());
        CtClass clone1 = createClone(ctClass1);
        CtConstructor cloneConstructor1 = clone1.getConstructors()[0];
        cloneConstructor1.addParameter(pool.get(List.class.getName()));
        cloneConstructor1.addParameter(pool.get(Set.class.getName()));
        Class clazz1 = clone1.toClass();
        
        System.out.println(clazz1.getConstructors()[0].getParameterTypes().length);
        System.out.println(clazz1.getConstructors()[0].getGenericParameterTypes().length);
  }
        
    private static int classNameSuffix = 1;
        
    private static CtClass createClone(CtClass ctClass) throws IOException {
        ByteArrayOutputStream classFileByteStream =new ByteArrayOutputStream();
        DataOutputStream classFileStream = new DataOutputStream(classFileByteStream);
        if(ctClass.isFrozen()) {
            ctClass.defrost();
        }
        ctClass.getClassFile().write(classFileStream);
        byte[] classFileBytes = classFileByteStream.toByteArray();
        InputStream classFileCloneStream = new ByteArrayInputStream(classFileBytes);
        CtClass retValue = pool.makeClass(classFileCloneStream);
        retValue.setName(ctClass.getName()+"V"+classNameSuffix++);
        return retValue;
    }
}

class Clazz {

    public Clazz(
        List<Boolean> parameter0,
        int b
    ) {}
}

 

Or is it the fault of my createClone method?

 

Any help or hint is appreciated

 

Best regards