View Javadoc
1   /*
2    * Copyright 2013 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.util.internal;
18  
19  import io.netty.util.internal.logging.InternalLogger;
20  import io.netty.util.internal.logging.InternalLoggerFactory;
21  import javassist.ClassClassPath;
22  import javassist.ClassPath;
23  import javassist.ClassPool;
24  import javassist.CtClass;
25  import javassist.Modifier;
26  import javassist.NotFoundException;
27  
28  import java.lang.reflect.Method;
29  
30  public final class JavassistTypeParameterMatcherGenerator {
31  
32      private static final InternalLogger logger =
33              InternalLoggerFactory.getInstance(JavassistTypeParameterMatcherGenerator.class);
34  
35      private static final ClassPool classPool = new ClassPool(true);
36  
37      static {
38          classPool.appendClassPath(new ClassClassPath(NoOpTypeParameterMatcher.class));
39      }
40  
41      public static void appendClassPath(ClassPath classpath) {
42          classPool.appendClassPath(classpath);
43      }
44  
45      public static void appendClassPath(String pathname) throws NotFoundException {
46          classPool.appendClassPath(pathname);
47      }
48  
49      public static ClassPool classPool() {
50          return classPool;
51      }
52  
53      public static TypeParameterMatcher generate(Class<?> type) {
54          ClassLoader classLoader = PlatformDependent.getContextClassLoader();
55          if (classLoader == null) {
56              classLoader = PlatformDependent.getSystemClassLoader();
57          }
58          return generate(type, classLoader);
59      }
60  
61      public static TypeParameterMatcher generate(Class<?> type, ClassLoader classLoader) {
62          final String typeName = typeName(type);
63          final String className = "io.netty.util.internal.__matchers__." + typeName + "Matcher";
64          try {
65              try {
66                  return (TypeParameterMatcher) Class.forName(className, true, classLoader).newInstance();
67              } catch (Exception e) {
68                  // Not defined in the specified class loader.
69              }
70  
71              CtClass c = classPool.getAndRename(NoOpTypeParameterMatcher.class.getName(), className);
72              c.setModifiers(c.getModifiers() | Modifier.FINAL);
73              c.getDeclaredMethod("match").setBody("{ return $1 instanceof " + typeName + "; }");
74              byte[] byteCode = c.toBytecode();
75              c.detach();
76              Method method = ClassLoader.class.getDeclaredMethod(
77                      "defineClass", String.class, byte[].class, int.class, int.class);
78              method.setAccessible(true);
79  
80              Class<?> generated = (Class<?>) method.invoke(classLoader, className, byteCode, 0, byteCode.length);
81              if (type != Object.class) {
82                  logger.debug("Generated: {}", generated.getName());
83              } else {
84                  // Object.class is only used when checking if Javassist is available.
85              }
86              return (TypeParameterMatcher) generated.newInstance();
87          } catch (RuntimeException e) {
88              throw e;
89          } catch (Exception e) {
90              throw new RuntimeException(e);
91          }
92      }
93  
94      private static String typeName(Class<?> type) {
95          if (type.isArray()) {
96              return typeName(type.getComponentType()) + "[]";
97          }
98  
99          return type.getName();
100     }
101 
102     private JavassistTypeParameterMatcherGenerator() { }
103 }