net.bytebuddy.dynamic.DynamicType.Unloaded Java Examples

The following examples show how to use net.bytebuddy.dynamic.DynamicType.Unloaded. 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: ByteBuddyProcessFunctionInvoker.java    From da-streamingledger with Apache License 2.0 6 votes vote down vote up
private static <InT, OutT> DynamicType.Unloaded<?> createDynamicTypeFromSpec(StreamingLedgerSpec<InT, OutT> spec)
        throws NoSuchMethodException {
    PackageLocalNamingStrategy generatedTypeName = new PackageLocalNamingStrategy(spec.processFunction.getClass());

    TypeDefinition generatedType = Generic.Builder.parameterizedType(
            ProcessFunctionInvoker.class,
            spec.inputType.getTypeClass(),
            spec.resultType.getTypeClass()
    ).build();

    TypeDefinition processFunctionType = new TypeDescription.ForLoadedType(spec.processFunction.getClass());
    ForLoadedConstructor superTypeConstructor =
            new ForLoadedConstructor(ProcessFunctionInvoker.class.getDeclaredConstructor());
    MethodDescription processMethodType = processMethodTypeFromSpec(spec);

    Builder<?> builder = configureByteBuddyBuilder(
            generatedTypeName,
            generatedType,
            processFunctionType,
            superTypeConstructor,
            processMethodType,
            spec.stateBindings.size());

    return builder.make();
}
 
Example #2
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 #3
Source File: ByteBuddyProcessFunctionInvoker.java    From da-streamingledger with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <InT, OutT> Class<? extends ProcessFunctionInvoker<InT, OutT>> loadClass(
        DynamicType.Unloaded<?> unloaded,
        ClassLoader classLoader) {
    return (Class<? extends ProcessFunctionInvoker<InT, OutT>>)
            unloaded
                    .load(classLoader, Default.INJECTION)
                    .getLoaded();
}
 
Example #4
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 #5
Source File: ByteBuddyState.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Unloaded<?> make(TypePool typePool, DynamicType.Builder<?> builder) {
	if ( System.getSecurityManager() != null ) {
		builder = builder.visit( getDeclaredMethodMemberSubstitution );
		builder = builder.visit( getMethodMemberSubstitution );
	}

	Unloaded<?> unloadedClass;
	if ( typePool != null ) {
		unloadedClass = builder.make( typePool );
	}
	else {
		unloadedClass = builder.make();
	}

	if ( DEBUG ) {
		try {
			unloadedClass.saveIn( new File( System.getProperty( "java.io.tmpdir" ) + "/bytebuddy/" ) );
		}
		catch (IOException e) {
			LOG.warn( "Unable to save generated class %1$s", unloadedClass.getTypeDescription().getName(), e );
		}
	}

	if ( System.getSecurityManager() != null ) {
		// we authorize the proxy class to access the method lookup dispatcher
		HibernateMethodLookupDispatcher.registerAuthorizedClass( unloadedClass.getTypeDescription().getName() );
	}

	return unloadedClass;
}
 
Example #6
Source File: ByteBuddyProcessFunctionInvoker.java    From da-streamingledger with Apache License 2.0 4 votes vote down vote up
private static <InT, OutT> Class<? extends ProcessFunctionInvoker<InT, OutT>> generateAndLoadClass(
        StreamingLedgerSpec<InT, OutT> spec) throws NoSuchMethodException {
    Unloaded<?> unloaded = createDynamicTypeFromSpec(spec);
    return loadClass(unloaded, classLoader(spec));
}
 
Example #7
Source File: ByteBuddyState.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private Unloaded<?> make(DynamicType.Builder<?> builder) {
	return make( null, builder );
}
 
Example #8
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 #9
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 #10
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);
  }
}