Search in sources :

Example 61 with CtField

use of javassist.CtField in project powermock by powermock.

the class ClassReplicaCreator method copyFields.

private void copyFields(CtClass originalClassAsCtClass, final CtClass newClass) throws CannotCompileException, NotFoundException {
    CtField[] declaredFields = originalClassAsCtClass.getDeclaredFields();
    CtField[] undeclaredFields = originalClassAsCtClass.getFields();
    Set<CtField> allFields = new HashSet<CtField>();
    Collections.addAll(allFields, declaredFields);
    Collections.addAll(allFields, undeclaredFields);
    for (CtField ctField : allFields) {
        CtField f = new CtField(ctField.getType(), ctField.getName(), newClass);
        newClass.addField(f);
    }
}
Also used : CtField(javassist.CtField) HashSet(java.util.HashSet)

Example 62 with CtField

use of javassist.CtField in project powermock by powermock.

the class ClassReplicaCreator method addDelegatorField.

/**
 * Add a field to the replica class that holds the instance delegator. I.e.
 * if we're creating a instance replica of {@code java.lang.Long} this
 * methods adds a new field of type {@code delegator.getClass()} to the
 * replica class.
 */
private <T> void addDelegatorField(T delegator, final CtClass replicaClass) throws CannotCompileException {
    CtField f = CtField.make(String.format("private %s %s = null;", delegator.getClass().getName(), POWERMOCK_INSTANCE_DELEGATOR_FIELD_NAME), replicaClass);
    replicaClass.addField(f);
}
Also used : CtField(javassist.CtField)

Example 63 with CtField

use of javassist.CtField in project yyl_example by Relucent.

the class JdwpTest method main.

public static void main(String[] args) throws CannotCompileException, NotFoundException, IOException, IllegalConnectorArgumentsException {
    ClassPool pool = ClassPool.getDefault();
    CtClass cc = pool.makeClass("yyl.test.TestClass");
    CtField cf = new CtField(pool.get("java.lang.String"), "name", cc);
    cc.addField(cf);
    HotSwapper hs = new HotSwapper(1234);
    hs.reload(cc.getName(), cc.toBytecode());
    cc.toClass();
}
Also used : HotSwapper(javassist.util.HotSwapper) CtClass(javassist.CtClass) CtField(javassist.CtField) ClassPool(javassist.ClassPool)

Example 64 with CtField

use of javassist.CtField in project uavstack by uavorg.

the class AbstractAdaptor method defineField.

protected void defineField(String fieldName, String fieldClass, String varClass, String initCode) throws CannotCompileException, NotFoundException {
    CtClass cc = pool.get(varClass);
    String fd = "private " + fieldClass + " " + fieldName;
    if (StringHelper.isEmpty(initCode)) {
        fd += ";";
    } else {
        fd += "=" + initCode + ";";
    }
    CtField f = CtField.make(fd, cc);
    cc.addField(f);
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField)

Example 65 with CtField

use of javassist.CtField in project hibernate-orm by hibernate.

the class EntityEnhancer method collectInheritCollectionFields.

private Collection<CtField> collectInheritCollectionFields(CtClass managedCtClass) {
    if (managedCtClass == null || Object.class.getName().equals(managedCtClass.getName())) {
        return Collections.emptyList();
    }
    try {
        CtClass managedCtSuperclass = managedCtClass.getSuperclass();
        if (!enhancementContext.isMappedSuperclassClass(managedCtSuperclass)) {
            return collectInheritCollectionFields(managedCtSuperclass);
        }
        List<CtField> collectionList = new ArrayList<CtField>();
        for (CtField ctField : managedCtSuperclass.getDeclaredFields()) {
            if (!Modifier.isStatic(ctField.getModifiers())) {
                if (enhancementContext.isPersistentField(ctField) && !enhancementContext.isMappedCollection(ctField)) {
                    if (PersistentAttributesHelper.isAssignable(ctField, Collection.class.getName()) || PersistentAttributesHelper.isAssignable(ctField, Map.class.getName())) {
                        collectionList.add(ctField);
                    }
                }
            }
        }
        collectionList.addAll(collectInheritCollectionFields(managedCtSuperclass));
        return collectionList;
    } catch (NotFoundException nfe) {
        return Collections.emptyList();
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) ArrayList(java.util.ArrayList) NotFoundException(javassist.NotFoundException)

Aggregations

CtField (javassist.CtField)76 CtClass (javassist.CtClass)47 CtMethod (javassist.CtMethod)27 CannotCompileException (javassist.CannotCompileException)24 NotFoundException (javassist.NotFoundException)22 ClassPool (javassist.ClassPool)20 CtConstructor (javassist.CtConstructor)15 Test (org.junit.Test)12 ClassFile (javassist.bytecode.ClassFile)9 IOException (java.io.IOException)7 Method (java.lang.reflect.Method)7 ArrayList (java.util.ArrayList)6 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 ConstPool (javassist.bytecode.ConstPool)4 SMethod (org.bimserver.shared.meta.SMethod)4 SParameter (org.bimserver.shared.meta.SParameter)4 InsertableMethod (com.github.stephanenicolas.afterburner.inserts.InsertableMethod)3 SimpleInsertableMethod (com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3