net.bytebuddy.dynamic.DynamicType.Loaded Java Examples

The following examples show how to use net.bytebuddy.dynamic.DynamicType.Loaded. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QualifierAndScopeImplementationGenerator.java    From Diorite with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example #2
Source File: ReferenceCodec.java    From morphia with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #3
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #4
Source File: AndroidCompatibility.java    From Cubes with MIT License 4 votes vote down vote up
@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);
}
 
Example #5
Source File: Compatibility.java    From Cubes with MIT License 4 votes vote down vote up
public Loaded load(Unloaded unloaded) {
  return unloaded.load(getClass().getClassLoader(), Default.INJECTION);
}
 
Example #6
Source File: LuaGeneration.java    From Cubes with MIT License 4 votes vote down vote up
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);
  }
}