Java源码示例:net.bytebuddy.dynamic.DynamicType.Loaded

示例1
@SuppressWarnings("unchecked")
@Nullable
public static <T extends Annotation> Class<? extends T> transform(Class<T> clazz)
{
    if (! clazz.isAnnotation() || ! (clazz.isAnnotationPresent(Qualifier.class) || clazz.isAnnotationPresent(Scope.class)))
    {
        return null;
    }
    try
    {
        String name = GENERATED_PREFIX + "." + clazz.getName();
        Unloaded<Object> make = new ByteBuddy(ClassFileVersion.JAVA_V9).subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS)
                                                                       .implement(Serializable.class, clazz).name(name)
                                                                       .visit(new AnnotationImplementationVisitor(new ForLoadedType(clazz))).make();
        Loaded<Object> load = make.load(ClassLoader.getSystemClassLoader(), Default.INJECTION);
        return (Class<? extends T>) load.getLoaded();
    }
    catch (Throwable e)
    {
        throw new RuntimeException(e);
    }
}
 
示例2
private <T> T createProxy(final MorphiaReference reference) {
    ReferenceProxy referenceProxy = new ReferenceProxy(reference);
    try {
        Class<?> type = getField().getType();
        String name = (type.getPackageName().startsWith("java") ? type.getSimpleName() : type.getName()) + "$$Proxy";
        return ((Loaded<T>) new ByteBuddy()
                                .subclass(type)
                                .implement(MorphiaProxy.class)
                                .name(name)

                                .invokable(ElementMatchers.isDeclaredBy(type))
                                .intercept(InvocationHandlerAdapter.of(referenceProxy))

                                .method(ElementMatchers.isDeclaredBy(MorphiaProxy.class))
                                .intercept(InvocationHandlerAdapter.of(referenceProxy))

                                .make()
                                .load(Thread.currentThread().getContextClassLoader(), Default.WRAPPER))
                   .getLoaded()
                   .getDeclaredConstructor()
                   .newInstance();
    } catch (ReflectiveOperationException | IllegalArgumentException e) {
        throw new MappingException(e.getMessage(), e);
    }
}
 
示例3
static <T> Loaded<T> load(Unloaded<T> unloaded) {
    ClassLoadingStrategy<ClassLoader> strategy;

    try {
        strategy = ClassLoadingStrategy.UsingLookup.of(MethodHandles.lookup());
    } catch (IllegalStateException ex) {
        strategy = new ClassLoadingStrategy.ForUnsafeInjection();
    }

    return unloaded.load(JackpotTrees.class.getClassLoader(), strategy);
}
 
示例4
@Override
public Loaded load(Unloaded unloaded) {
  File dir = androidLauncher.getDir("cubes-class", Context.MODE_PRIVATE);
  AndroidClassLoadingStrategy androidClassLoadingStrategy = new AndroidClassLoadingStrategy.Wrapping(dir);
  return unloaded.load(getClass().getClassLoader(), androidClassLoadingStrategy);
}
 
示例5
public Loaded load(Unloaded unloaded) {
  return unloaded.load(getClass().getClassLoader(), Default.INJECTION);
}
 
示例6
public static Class extendClass(Class<?> extend, final LuaTable delegations, Class<?>... inherit) {
  long startTime = System.nanoTime();

  ArrayDeque<Class> toCheck = new ArrayDeque<Class>();
  toCheck.add(extend);
  toCheck.addAll(Arrays.asList(inherit));
  while (!toCheck.isEmpty()) {
    Class check = toCheck.pop();
    for (Method method : check.getDeclaredMethods()) {
      if (Modifier.isAbstract(method.getModifiers())) {
        if (delegations.get(method.getName()).isnil())
          throw new DynamicDelegationError("No delegation for abstract method " + method);
      }
    }
    check = check.getSuperclass();
    if (check != null && check != Object.class) toCheck.add(check);
  }
  
  try {
    ReceiverTypeDefinition<?> build = b.subclass(extend).implement(inherit)
            .method(not(isConstructor()).and(isAbstract())).intercept(MethodDelegation.to(new AbstractInterceptor(delegations)));
    if (!delegations.get("__new__").isnil()) {
      build = build.constructor(isConstructor()).intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.to(new ConstructorInterceptor(delegations))));
    }
    Junction<MethodDescription> publicMethods = not(isConstructor().or(isAbstract())).and(isPublic()).and(new ElementMatcher<MethodDescription>() {
      @Override
      public boolean matches(MethodDescription target) {
        return !delegations.get(target.getName()).isnil();
      }
    });
    build = build.method(publicMethods).intercept(MethodDelegation.to(new PublicInterceptor(delegations)));
    
    Unloaded unloaded = build.make();
    Loaded loaded = Compatibility.get().load(unloaded);
    Class c = loaded.getLoaded();
    Log.debug("Created dynamic class " + c.getName() + " in " + ((System.nanoTime() - startTime) / 1000000) + "ms");
    return c;
  } catch (Exception e) {
    Log.error("Failed to create dynamic class " + extend.getName() + " " + Arrays.toString(inherit));
    throw new CubesException("Failed to make dynamic class", e);
  }
}