• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

raphw / byte-buddy / #801

27 Oct 2025 09:37AM UTC coverage: 84.715% (-0.4%) from 85.118%
#801

push

raphw
Fix imports.

29586 of 34924 relevant lines covered (84.72%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.49
/byte-buddy-dep/src/main/java/net/bytebuddy/build/RenamingPlugin.java
1
/*
2
 * Copyright 2014 - Present Rafael Winterhalter
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.bytebuddy.build;
17

18
import net.bytebuddy.asm.AsmVisitorWrapper;
19
import net.bytebuddy.description.field.FieldDescription;
20
import net.bytebuddy.description.field.FieldList;
21
import net.bytebuddy.description.method.MethodList;
22
import net.bytebuddy.description.type.TypeDescription;
23
import net.bytebuddy.dynamic.ClassFileLocator;
24
import net.bytebuddy.dynamic.DynamicType;
25
import net.bytebuddy.implementation.Implementation;
26
import net.bytebuddy.matcher.ElementMatcher;
27
import net.bytebuddy.pool.TypePool;
28
import net.bytebuddy.utility.OpenedClassReader;
29
import org.objectweb.asm.ClassVisitor;
30
import org.objectweb.asm.commons.ClassRemapper;
31
import org.objectweb.asm.commons.Remapper;
32

33
import java.util.ArrayList;
34
import java.util.Arrays;
35
import java.util.HashMap;
36
import java.util.List;
37
import java.util.Map;
38
import java.util.regex.Matcher;
39
import java.util.regex.Pattern;
40

41
import static net.bytebuddy.matcher.ElementMatchers.any;
42
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
43

44
/**
45
 * A plugin that replaces names that are discovered in class files.
46
 */
47
@HashCodeAndEqualsPlugin.Enhance
48
public class RenamingPlugin extends AsmVisitorWrapper.AbstractBase implements Plugin {
49

50
    /**
51
     * The renaming to apply.
52
     */
53
    private final Renaming renaming;
54

55
    /**
56
     * A matcher that determines what types to consider for renaming.
57
     */
58
    private final ElementMatcher<? super TypeDescription> matcher;
59

60
    /**
61
     * Creates a renaming plugin for a given regular expression and replacement that applies to all types.
62
     *
63
     * @param pattern     The pattern to consider.
64
     * @param replacement The replacement to apply if the supplied pattern is matched.
65
     */
66
    public RenamingPlugin(String pattern, String replacement) {
67
        this(new Renaming.ForPattern(Pattern.compile(pattern), replacement));
1✔
68
    }
1✔
69

70
    /**
71
     * Creates a renaming plugin for a given regular expression and replacement that applies to all types that start with a given prefix.
72
     *
73
     * @param pattern     The pattern to consider.
74
     * @param replacement The replacement to apply if the supplied pattern is matched.
75
     * @param prefix      The prefix for types to consider for renaming.
76
     */
77
    public RenamingPlugin(String pattern, String replacement, String prefix) {
78
        this(new Renaming.ForPattern(Pattern.compile(pattern), replacement), nameStartsWith(prefix));
×
79
    }
×
80

81
    /**
82
     * Creates a renaming plugin for the given renaming that applies to all types.
83
     *
84
     * @param renaming The renaming to apply.
85
     */
86
    public RenamingPlugin(Renaming renaming) {
87
        this(renaming, any());
1✔
88
    }
1✔
89

90
    /**
91
     * Creates a renaming plugin for the given renaming and type matcher.
92
     *
93
     * @param renaming The renaming to apply.
94
     * @param matcher  A matcher that determines what types to consider for renaming.
95
     */
96
    public RenamingPlugin(Renaming renaming, ElementMatcher<? super TypeDescription> matcher) {
1✔
97
        this.renaming = renaming;
1✔
98
        this.matcher = matcher;
1✔
99
    }
1✔
100

101
    /**
102
     * {@inheritDoc}
103
     */
104
    public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator) {
105
        return builder.visit(this);
1✔
106
    }
107

108
    /**
109
     * {@inheritDoc}
110
     */
111
    public boolean matches(TypeDescription target) {
112
        return matcher.matches(target);
×
113
    }
114

115
    /**
116
     * {@inheritDoc}
117
     */
118
    public void close() {
119
        /* do nothing */
120
    }
×
121

122
    /**
123
     * {@inheritDoc}
124
     */
125
    public ClassVisitor wrap(TypeDescription instrumentedType,
126
                             ClassVisitor classVisitor,
127
                             Implementation.Context implementationContext,
128
                             TypePool typePool,
129
                             FieldList<FieldDescription.InDefinedShape> fields,
130
                             MethodList<?> methods,
131
                             int writerFlags,
132
                             int readerFlags) {
133
        return new ClassRemapper(classVisitor, new RenamingRemapper(renaming));
1✔
134
    }
135

136
    /**
137
     * A renaming function tho transform a type's binary name.
138
     */
139
    public interface Renaming {
140

141
        /**
142
         * Applies a renaming.
143
         *
144
         * @param name The previous name.
145
         * @return The former name.
146
         */
147
        String apply(String name);
148

149
        /**
150
         * A non-operational renaming.
151
         */
152
        enum NoOp implements Renaming {
×
153

154
            /**
155
             * The singleton instance.
156
             */
157
            INSTANCE;
×
158

159
            /**
160
             * {@inheritDoc}
161
             */
162
            public String apply(String name) {
163
                return name;
×
164
            }
165
        }
166

167
        /**
168
         * A renaming that renames types by a given regular expression.
169
         */
170
        @HashCodeAndEqualsPlugin.Enhance
171
        class ForPattern implements Renaming {
172

173
            /**
174
             * The regular expression to use.
175
             */
176
            private final Pattern pattern;
177

178
            /**
179
             * The replacement to apply.
180
             */
181
            private final String replacement;
182

183
            /**
184
             * Creates a new renaming for a regular expression.
185
             *
186
             * @param pattern     The regular expression to use.
187
             * @param replacement The replacement to apply.
188
             */
189
            public ForPattern(Pattern pattern, String replacement) {
1✔
190
                this.pattern = pattern;
1✔
191
                this.replacement = replacement;
1✔
192
            }
1✔
193

194
            /**
195
             * {@inheritDoc}
196
             */
197
            public String apply(String name) {
198
                Matcher matcher = pattern.matcher(name);
1✔
199
                if (matcher.find()) {
1✔
200
                    StringBuffer buffer = new StringBuffer();
1✔
201
                    do {
202
                        matcher.appendReplacement(buffer, replacement);
1✔
203
                    } while (matcher.find());
1✔
204
                    return matcher.appendTail(buffer).toString();
1✔
205
                } else {
206
                    return name;
1✔
207
                }
208
            }
209
        }
210

211
        /**
212
         * A compound renaming.
213
         */
214
        @HashCodeAndEqualsPlugin.Enhance
215
        class Compound implements Renaming {
216

217
            /**
218
             * The renamings to apply.
219
             */
220
            private final List<Renaming> renamings;
221

222
            /**
223
             * Creates a new compound renaming.
224
             *
225
             * @param renaming The renaming to apply.
226
             */
227
            public Compound(Renaming... renaming) {
228
                this(Arrays.asList(renaming));
×
229
            }
×
230

231
            /**
232
             * Creates a new compound renaming.
233
             *
234
             * @param renamings The renamings to apply.
235
             */
236
            public Compound(List<? extends Renaming> renamings) {
×
237
                this.renamings = new ArrayList<Renaming>(renamings.size());
×
238
                for (Renaming remapping : renamings) {
×
239
                    if (remapping instanceof Compound) {
×
240
                        this.renamings.addAll(((Compound) remapping).renamings);
×
241
                    } else if (!(remapping instanceof NoOp)) {
×
242
                        this.renamings.add(remapping);
×
243
                    }
244
                }
×
245
            }
×
246

247
            /**
248
             * {@inheritDoc}
249
             */
250
            public String apply(String name) {
251
                for (Renaming remapping : renamings) {
×
252
                    name = remapping.apply(name);
×
253
                }
×
254
                return name;
×
255
            }
256
        }
257
    }
258

259
    /**
260
     * An ASM {@link Remapper} to apply renamings.
261
     */
262
    protected static class RenamingRemapper extends Remapper {
263

264
        /**
265
         * The renaming to apply.
266
         */
267
        private final Renaming renaming;
268

269
        /**
270
         * A cache of previously applied renamings.
271
         */
272
        private final Map<String, String> cache = new HashMap<String, String>();
1✔
273

274
        /**
275
         * Creates a new renaming remapper.
276
         * @param renaming The renaming to apply.
277
         */
278
        protected RenamingRemapper(Renaming renaming) {
279
            super(OpenedClassReader.ASM_API);
1✔
280
            this.renaming = renaming;
1✔
281
        }
1✔
282

283
        @Override
284
        public String map(String internalName) {
285
            String renamed = cache.get(internalName);
1✔
286
            if (renamed != null) {
1✔
287
                return renamed;
1✔
288
            }
289
            renamed = renaming.apply(internalName.replace('/', '.')).replace('.', '/');
1✔
290
            cache.put(internalName, renamed);
1✔
291
            return renamed;
1✔
292
        }
293
    }
294
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc