or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

bean-utilities.mdcore.mdfast-reflection.mdindex.mdproxy-generation.mdtransform.mdutilities.md

transform.mddocs/

0

# Bytecode Transformation

1

2

Advanced bytecode transformation utilities for custom class loading and runtime class modification. These components provide lower-level access to CGLib's bytecode generation capabilities for specialized use cases.

3

4

## Capabilities

5

6

### ClassTransformer

7

8

Base class for implementing custom bytecode transformations during class generation.

9

10

```java { .api }

11

/**

12

* Base class for bytecode transformations

13

*/

14

public abstract class ClassTransformer {

15

/**

16

* Transform bytecode during class generation

17

* @param cv - ClassVisitor for bytecode modification

18

* @return Modified ClassVisitor

19

*/

20

public abstract ClassVisitor transform(ClassVisitor cv);

21

22

/**

23

* Set the next transformer in the chain

24

* @param next - Next ClassTransformer

25

*/

26

public void setNext(ClassTransformer next);

27

28

/**

29

* Get the next transformer in the chain

30

* @return Next ClassTransformer or null if none

31

*/

32

public ClassTransformer getNext();

33

}

34

```

35

36

**Usage Examples:**

37

38

```java

39

import net.sf.cglib.transform.ClassTransformer;

40

import org.objectweb.asm.ClassVisitor;

41

import org.objectweb.asm.MethodVisitor;

42

43

// Custom transformer to add logging to all methods

44

public class LoggingTransformer extends ClassTransformer {

45

@Override

46

public ClassVisitor transform(ClassVisitor cv) {

47

return new ClassVisitor(Opcodes.ASM7, cv) {

48

@Override

49

public MethodVisitor visitMethod(int access, String name,

50

String descriptor, String signature,

51

String[] exceptions) {

52

MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);

53

// Add logging bytecode to method

54

return new LoggingMethodVisitor(mv, name);

55

}

56

};

57

}

58

}

59

```

60

61

### TransformingClassLoader

62

63

ClassLoader that applies transformations during class loading, enabling runtime class modification.

64

65

```java { .api }

66

/**

67

* ClassLoader that applies transformations during class loading

68

*/

69

public class TransformingClassLoader extends ClassLoader {

70

/**

71

* Create TransformingClassLoader with parent ClassLoader

72

* @param parent - Parent ClassLoader

73

* @param filter - ClassFilter to determine which classes to transform

74

* @param transformer - ClassTransformer to apply

75

*/

76

public TransformingClassLoader(ClassLoader parent, ClassFilter filter,

77

ClassTransformer transformer);

78

79

/**

80

* Load and transform class

81

* @param name - Class name

82

* @return Loaded and transformed Class

83

* @throws ClassNotFoundException - If class cannot be found

84

*/

85

@Override

86

public Class<?> loadClass(String name) throws ClassNotFoundException;

87

88

/**

89

* Load class from byte array with transformation

90

* @param name - Class name

91

* @param b - Original bytecode

92

* @param off - Offset in byte array

93

* @param len - Length of bytecode

94

* @return Loaded Class after transformation

95

*/

96

protected Class<?> loadClass(String name, byte[] b, int off, int len);

97

}

98

```

99

100

**Usage Examples:**

101

102

```java

103

import net.sf.cglib.transform.*;

104

import net.sf.cglib.core.ClassFilter;

105

106

// Create class filter to select which classes to transform

107

ClassFilter filter = new ClassFilter() {

108

@Override

109

public boolean accept(String name) {

110

return name.startsWith("com.mycompany"); // Only transform our classes

111

}

112

};

113

114

// Create transformer chain

115

ClassTransformer transformer = new LoggingTransformer();

116

117

// Create transforming class loader

118

TransformingClassLoader loader = new TransformingClassLoader(

119

getClass().getClassLoader(), filter, transformer);

120

121

// Load transformed class

122

Class<?> transformedClass = loader.loadClass("com.mycompany.MyService");

123

Object instance = transformedClass.newInstance();

124

```

125

126

### ClassFilter

127

128

Interface for determining which classes should be transformed.

129

130

```java { .api }

131

/**

132

* Interface for filtering classes during transformation

133

*/

134

public interface ClassFilter {

135

/**

136

* Determine if class should be transformed

137

* @param name - Class name

138

* @return true if class should be transformed, false otherwise

139

*/

140

boolean accept(String name);

141

}

142

```

143

144

## Types

145

146

```java { .api }

147

import org.objectweb.asm.ClassVisitor;

148

import org.objectweb.asm.MethodVisitor;

149

```

150

151

## Usage Notes

152

153

- **ASM Dependency**: Transformation utilities require direct use of ASM (Abstract Syntax Tree Manipulation) framework

154

- **Advanced Usage**: These classes are for advanced users who need custom bytecode manipulation

155

- **Performance**: Direct bytecode transformation can be complex but offers maximum flexibility

156

- **Integration**: Often used with other CGLib components for specialized proxy generation scenarios