/** * Returns a new immutable type description for a loaded type. * * @param type The type to be represented by this type description. * @return The type description representing the given type. */ public static TypeDescription of(Class<?> type) { TypeDescription typeDescription = TYPE_CACHE.get(type); return typeDescription == null ? new ForLoadedType(type) : typeDescription; }
@Override public boolean isOverridden(Object instance, Method origin) { SoftReference<MethodGraph> reference = graphs.get(instance.getClass()); MethodGraph methodGraph = reference == null ? null : reference.get(); if (methodGraph == null) { methodGraph = compiler.compile(new TypeDescription.ForLoadedType(instance.getClass())); graphs.put(instance.getClass(), new SoftReference<MethodGraph>(methodGraph)); } MethodGraph.Node node = methodGraph.locate(new MethodDescription.ForLoadedMethod(origin).asSignatureToken()); return !node.getSort().isResolved() || !node.getRepresentative().asDefined().getDeclaringType().represents(origin.getDeclaringClass()); }
); if ( getter.getReturnType().isPrimitive() ) { PrimitiveBoxingDelegate.forPrimitive( new TypeDescription.ForLoadedType( getter.getReturnType() ) ) .assignBoxedTo( TypeDescription.Generic.OBJECT,
/** * Makes sure that this transformer is only applied on Freemarker versions >= 2.3.23 where the * {@link Environment#getCurrentTemplate()} method was made public. This prevents nasty * {@link IllegalAccessError}s and {@link NoSuchMethodError}s. */ @Override public boolean isActive() { try { return hasMethodThat(named("getCurrentTemplate") .and(ElementMatchers.<MethodDescription.InDefinedShape>isPublic()) .and(takesArguments(0))) .matches(new TypeDescription.ForLoadedType(Class.forName("freemarker.core.Environment"))); } catch (ClassNotFoundException e) { return false; } }
/** * Ctor. */ public SmtInvokeStringBuilderAppend() { super( new TypeDescription.ForLoadedType(StringBuilder.class), new ConjunctionMatcher<>( ElementMatchers.named("append"), ElementMatchers.takesArguments(1), ElementMatchers.takesArgument(0, Object.class), ElementMatchers.returns(StringBuilder.class) ) ); } }
@Override public final void check() throws Exception { final TypeDescription typeDescription = new TypeDescription.ForLoadedType(clazz); final DynamicType.Builder<?> subclass = new ByteBuddy().redefine(clazz); final DynamicType.Unloaded<?> make = bt .transitionResult(subclass, typeDescription) .make(); final Class<?> newClazz = make.load(new AnonymousClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST).getLoaded(); assertThat( List.of(newClazz.getDeclaredMethods()).map(Method::getName) ).containsOnlyElementsOf( methodNames ); }
@Override public final StackManipulationToken stackManipulationToken() { final TypeDescription declaringType = field.getDeclaringType().asErasure(); final TypeDescription fieldType = field.getType().asErasure(); if(new NaturalJavaAtom().matches(fieldType)) { return new SmtInvokeMethod( new TypeDescription.ForLoadedType(Object.class), ElementMatchers.named("equals") ); } else { return new SmtInvokeAtomEqual(declaringType); } } }
public static TypeDescription.ForLoadedType getTypeDescription(Class<?> c) { return new TypeDescription.ForLoadedType(c); }
private Implementation throwingEid(String eid) { Class<EidIllegalStateException> cls = EidIllegalStateException.class; TypeDescription typeDescription = new TypeDescription.ForLoadedType( cls ); ConstructionDelegate constr = new EidConstructionDelegate(typeDescription, eid); return new ExceptionMethod(typeDescription, constr); }
@Override public final void check() throws Exception { final TypeDescription typeDescription = new TypeDescription.ForLoadedType(type); final DynamicType.Builder<?> subclass = new ByteBuddy().redefine(type); final DynamicType.Unloaded<?> make = builderTransition .transitionResult(subclass, typeDescription) .make(); final Class<?> clazz = make.load(new AnonymousClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST).getLoaded(); assertThat(clazz.getAnnotation(annotation)) .withFailMessage("Expected annotation %s is missing on class %s", annotation.getName(), clazz.getName()) .isNotNull(); }
@Override public final void check() throws Exception { final DynamicType.Builder<?> subclass = new ByteBuddy().redefine(type); final TypeDescription typeDescription = new TypeDescription.ForLoadedType(type); assertThatCode(() -> { final DynamicType.Unloaded<?> make = bt .transitionResult(subclass, typeDescription) .make(); final Class<?> clazz = make.load(new AnonymousClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST).getLoaded(); clazz.getMethods(); // Initiate validation. }).doesNotThrowAnyException(); }
public static byte[] getBytes(Class<?> c) throws IOException { // String name = '/' + c.getName().replace('.', '/')+ ".class"; // InputStream is = c.getClassLoader().getResourceAsStream(name); // byte[] bytes = MFile.readBinary(is); // return bytes; ClassFileLocator locator = ClassFileLocator.AgentBased.of(instrumentation, c); TypeDescription.ForLoadedType desc = new TypeDescription.ForLoadedType(c); ClassFileLocator.Resolution resolution = locator.locate(desc.getName()); return resolution.resolve(); }
PrimitiveBoxing(Class<?> wrapperType, StackSize sizeDifference, String boxingMethodName, String boxingMethodDescriptor) { this.wrapperType = new TypeDescription.ForLoadedType(wrapperType); this.size = sizeDifference.toDecreasingSize(); this.boxingMethodName = boxingMethodName; this.boxingMethodDescriptor = boxingMethodDescriptor; }
static TypeDefinition createTypeDefinitionFromJavaType(JavaType javaType) { if (!javaType.hasGenericTypes()) { //simply use the raw class to construct the corresponding TypeDefinition return new TypeDescription.ForLoadedType(javaType.getRawClass()); } //create the appropriate Generic TypeDescription using containedType values final List<Type> genericParameters = new ArrayList<Type>(); for(int i=0; i<javaType.containedTypeCount(); i++) { genericParameters.add(javaType.containedType(i).getRawClass()); } return Generic.Builder.parameterizedType(javaType.getRawClass(), genericParameters).build(); } }
AbstractTask<?> createConcreteImplementation(Class<?> type) return new ByteBuddy() .subclass(AbstractTask.class) .method(named("getDataObjectClass")) .intercept(FixedValue.value(new TypeDescription.ForLoadedType(type))) // (*) .make() .load(type.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); }
/** * Makes sure that this transformer is only applied on Freemarker versions {@code >= 2.3.23} where the * {@link Environment#getCurrentTemplate()} method was made public. This prevents nasty * {@link IllegalAccessError}s and {@link NoSuchMethodError}s. */ @Override public boolean isActive() { try { return hasMethodThat(named("getCurrentTemplate") .and(ElementMatchers.<MethodDescription.InDefinedShape>isPublic()) .and(takesArguments(0))) .matches(new TypeDescription.ForLoadedType(Class.forName("freemarker.core.Environment"))); } catch (ClassNotFoundException e) { return false; } }
@Override public Target resolve(TypeDescription instrumentedType, MethodDescription instrumentedMethod, Assigner assigner, Advice.ArgumentHandler argumentHandler, Sort sort) { final ExceptionMetered exceptionMetered = instrumentedMethod.getDeclaredAnnotations().ofType(ExceptionMetered.class).loadSilent(); final TypeDescription.ForLoadedType exceptionType = new TypeDescription.ForLoadedType(exceptionMetered.cause()); return Target.ForStackManipulation.of(exceptionType); } };
public static TypeDescription.ForLoadedType getTypeDescription(Class<?> c) { return new TypeDescription.ForLoadedType(c); }
public static TypeDescription.ForLoadedType getTypeDescription(Class<?> c) { return new TypeDescription.ForLoadedType(c); }
@Override public ClassVisitor wrap(TypeDescription instrumentedType, ClassVisitor classVisitor, Implementation.Context implementationContext, TypePool typePool, FieldList<FieldDescription.InDefinedShape> fields, MethodList<?> methods, int writerFlags, int readerFlags) { return implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V8) ? new ParameterAddingClassVisitor(classVisitor, new TypeDescription.ForLoadedType(type)) : classVisitor; }