本文整理汇总了Java中net.bytebuddy.matcher.ElementMatchers类的典型用法代码示例。如果您正苦于以下问题:Java ElementMatchers类的具体用法?Java ElementMatchers怎么用?Java ElementMatchers使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ElementMatchers类属于net.bytebuddy.matcher包,在下文中一共展示了ElementMatchers类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createLazyClass
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private <T> Class<? extends T> createLazyClass(Class<T> type) {
Class<? extends Element> lazyType = byteBuddy
.subclass(AbstractElement.class)
.implement(type)
.defineField("handler", InvocationHandler.class, Visibility.PUBLIC)
.implement(HandlerSetter.class)
.intercept(FieldAccessor.ofField("handler"))
.method(ElementMatchers.not(ElementMatchers.isDeclaredBy(HandlerSetter.class)))
.intercept(InvocationHandlerAdapter.toField("handler"))
.make()
.load(type.getClassLoader())
.getLoaded();
lazyTypes.put(type, lazyType);
return (Class<? extends T>) lazyType;
}
示例2: transform
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
@Override
public DynamicType.Builder<?> transform(
final DynamicType.Builder<?> builder,
final TypeDescription typeDescription,
final ClassLoader classLoader,
final JavaModule module) {
final AsmVisitorWrapper methodsVisitor =
Advice.to(EnterAdvice.class, ExitAdviceMethods.class)
.on(ElementMatchers.isAnnotatedWith(CollectMetrics.class)
.and(ElementMatchers.isMethod()));
final AsmVisitorWrapper constructorsVisitor =
Advice.to(EnterAdvice.class, ExitAdviceConstructors.class)
.on(ElementMatchers.isAnnotatedWith(CollectMetrics.class)
.and(ElementMatchers.isConstructor()));
return builder.visit(methodsVisitor).visit(constructorsVisitor);
}
示例3: getIncludes
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public ElementMatcher<MethodDescription> getIncludes() {
return not(ElementMatchers.<MethodDescription>isPrivate()
.or(ObjectMethodElementMatchers.INSTANCE)
.or(named("close"))
.or(named("getDB"))
.or(named("getClient"))
.or(named("connect"))
.or(named("setDataSource"))
.or(named("resetState"))
.or(named("clusterSlots"))
.or(named("isConnected"))
.or(named("ping"))
.or(named("quit"))
.or(named("checkIsInMultiOrPipeline")));
}
示例4: matchAnyMethodIn
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
/**
* Returns a byte buddy matcher matching any method contained in methodSignatures.
*/
public static ElementMatcher<MethodDescription> matchAnyMethodIn(Set<MethodSignature> methodSignatures) {
ElementMatcher.Junction<MethodDescription> methodMatcher = ElementMatchers.none();
for (MethodSignature methodSignature : methodSignatures) {
ElementMatcher.Junction<MethodDescription> junction = ElementMatchers
.named(methodSignature.getMethodName())
.and(not(isAbstract()))
.and(isPublic())
.and(takesArguments(methodSignature.getParameterTypes().size()));
for (int i = 0; i < methodSignature.getParameterTypes().size(); i++) {
junction = junction.and(takesArgument(i, named(methodSignature.getParameterTypes().get(i))));
}
methodMatcher = methodMatcher.or(junction);
}
return methodMatcher;
}
示例5: result
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
@Override
public final Result<StackManipulation> result() {
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 SmtInvokeMethod(
declaringType,
new ConjunctionMatcher<>(
ElementMatchers.isSynthetic(),
ElementMatchers.named("atom$equal")
)
);
}
}
示例6: ExtendingTest
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
public ExtendingTest() {
super(
new TestCase(
"matches types inherited from atom",
new AssertThatTypeMatches(
new TypeDescription.ForLoadedType(
Bar.class
),
new Extending(
ElementMatchers.anyOf(Foo.class)
)
)
),
new TestCase(
"mismatch atoms",
new AssertThatTypeDoesNotMatch(
new TypeDescription.ForLoadedType(
Foo.class
),
new Extending(
ElementMatchers.anyOf(Foo.class)
)
)
)
);
}
示例7: createAgentBuilder
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
/**
* Creates the AgentBuilder that will redefine the System class.
* @param inst instrumentation instance.
* @return an agent builder.
*/
private static AgentBuilder createAgentBuilder(Instrumentation inst) {
// Find me a class called "java.lang.System"
final ElementMatcher.Junction<NamedElement> systemType = ElementMatchers.named("java.lang.System");
// And then find a method called setSecurityManager and tell MySystemInterceptor to
// intercept it (the method binding is smart enough to take it from there)
final AgentBuilder.Transformer transformer =
(b, typeDescription) -> b.method(ElementMatchers.named("setSecurityManager"))
.intercept(MethodDelegation.to(MySystemInterceptor.class));
// Disable a bunch of stuff and turn on redefine as the only option
final ByteBuddy byteBuddy = new ByteBuddy().with(Implementation.Context.Disabled.Factory.INSTANCE);
final AgentBuilder agentBuilder = new AgentBuilder.Default()
.withByteBuddy(byteBuddy)
.withInitializationStrategy(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
.withRedefinitionStrategy(AgentBuilder.RedefinitionStrategy.REDEFINITION)
.withTypeStrategy(AgentBuilder.TypeStrategy.Default.REDEFINE)
.type(systemType)
.transform(transformer);
return agentBuilder;
}
示例8: createMethodIdProxy
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
private static <C> C createMethodIdProxy(final Class<C> interfaceToProxy, final Optional<String> scopeNameOpt)
{
final List<ConfigDescriptor> configDescList = ConfigSystem.descriptorFactory.buildDescriptors(interfaceToProxy, scopeNameOpt);
DynamicType.Builder<C> typeBuilder = new ByteBuddy().subclass(interfaceToProxy);
for (ConfigDescriptor desc : configDescList) {
typeBuilder = typeBuilder.method(ElementMatchers.is(desc.getMethod())).intercept(InvocationHandlerAdapter.of((Object proxy, Method method1, Object[] args) -> {
log.trace("BB InvocationHandler identifying method {} proxy {}, argCount {}", method1.getName(), proxy.toString(), args.length);
lastIdentifiedMethodAndScope.set(new MethodAndScope(method1, scopeNameOpt));
return defaultForType(desc.getMethod().getReturnType());
}));
}
Class<? extends C> configImpl = typeBuilder.make()
.load(interfaceToProxy.getClassLoader(), ClassLoadingStrategy.Default.INJECTION)
.getLoaded();
try {
return configImpl.newInstance();
}
catch (InstantiationException | IllegalAccessException ex) {
throw new ConfigException("Failed to instantiate identification implementation of Config {} scope {}",
interfaceToProxy.getName(), scopeNameOpt.orElse("<empty>"), ex);
}
}
示例9: install
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
public static void install() {
ByteBuddyAgent.install();
ClassLoader targetClassLoader = File.class.getClassLoader();
// interceptor class must be injected to the same classloader as the target class that is intercepted
new ByteBuddy().redefine(CountFileSystemOperations.class)
.make()
.load(targetClassLoader,
ClassReloadingStrategy.fromInstalledAgent());
new ByteBuddy().redefine(DirectoryFileTree.class)
.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)
.method(ElementMatchers.named("length"), Advice.to(CountFileSystemOperations.LengthMethod.class))
.method(ElementMatchers.named("isFile"), Advice.to(CountFileSystemOperations.IsFileMethod.class))
.method(ElementMatchers.named("isDirectory"), Advice.to(CountFileSystemOperations.IsDirectoryMethod.class))
.method(ElementMatchers.named("lastModified"), Advice.to(CountFileSystemOperations.LastModifiedMethod.class))
.method(ElementMatchers.named("exists"), Advice.to(CountFileSystemOperations.ExistsMethod.class))
)
.make()
.load(targetClassLoader,
ClassReloadingStrategy.fromInstalledAgent());
}
示例10: install
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
public static void install() {
ByteBuddyAgent.install();
ClassLoader targetClassLoader = DirectoryFileTree.class.getClassLoader();
// interceptor class must be injected to the same classloader as the target class that is intercepted
new ByteBuddy().redefine(CountDirectoryScans.class)
.make()
.load(targetClassLoader,
ClassReloadingStrategy.fromInstalledAgent());
new ByteBuddy().redefine(DirectoryFileTree.class)
.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES).method(ElementMatchers.named("visitFrom"), Advice.to(CountDirectoryScans.class)))
.make()
.load(targetClassLoader,
ClassReloadingStrategy.fromInstalledAgent());
}
示例11: getConstructorsInterceptPoints
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
@Override
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
return new ConstructorInterceptPoint[] {
new ConstructorInterceptPoint() {
@Override
public ElementMatcher<MethodDescription> getConstructorMatcher() {
return ElementMatchers.takesArguments(1);
}
@Override
public String getConstructorInterceptor() {
return DO_CONSTRUCTOR_INTERCEPTOR;
}
}
};
}
示例12: agent
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
private static synchronized void agent(final boolean shouldRedefine, final Instrumentation instrumentation)
{
BufferAlignmentAgent.instrumentation = instrumentation;
// all Int methods, and all String method other than
// XXXStringWithoutLengthXXX or getStringXXX(int, int)
final Junction<MethodDescription> intVerifierMatcher = nameContains("Int")
.or(nameMatches(".*String[^W].*").and(not(ElementMatchers.takesArguments(int.class, int.class))));
alignmentTransformer = new AgentBuilder.Default(new ByteBuddy().with(TypeValidation.DISABLED))
.with(LISTENER)
.disableClassFormatChanges()
.with(shouldRedefine ?
AgentBuilder.RedefinitionStrategy.RETRANSFORMATION :
AgentBuilder.RedefinitionStrategy.DISABLED)
.type(isSubTypeOf(DirectBuffer.class).and(not(isInterface())))
.transform((builder, typeDescription, classLoader, module) -> builder
.visit(to(LongVerifier.class).on(nameContains("Long")))
.visit(to(DoubleVerifier.class).on(nameContains("Double")))
.visit(to(IntVerifier.class).on(intVerifierMatcher))
.visit(to(FloatVerifier.class).on(nameContains("Float")))
.visit(to(ShortVerifier.class).on(nameContains("Short")))
.visit(to(CharVerifier.class).on(nameContains("Char"))))
.installOn(instrumentation);
}
示例13: createStageClass
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
public <T> Class<? extends T> createStageClass( Class<T> stageClass ) {
return new ByteBuddy()
.subclass( stageClass, ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING )
.implement( StageInterceptorInternal.class )
.defineField( INTERCEPTOR_FIELD_NAME, StepInterceptor.class )
.method( named(SETTER_NAME) )
.intercept(
MethodDelegation.withDefaultConfiguration()
.withBinders( FieldProxy.Binder.install(
StepInterceptorGetterSetter.class ))
.to(new StepInterceptorSetter() ))
.method( not( named( SETTER_NAME )
.or(ElementMatchers.isDeclaredBy(Object.class))))
.intercept(
MethodDelegation.withDefaultConfiguration()
.withBinders(FieldProxy.Binder.install(
StepInterceptorGetterSetter.class ))
.to( new ByteBuddyMethodInterceptor() ))
.make()
.load( getClassLoader( stageClass ),
getClassLoadingStrategy( stageClass ) )
.getLoaded();
}
示例14: testRetrieveFromArray
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
@Test
public void testRetrieveFromArray() throws Exception {
DynamicType.Unloaded<RetrieveFromArray> arr = new ByteBuddy().subclass(RetrieveFromArray.class)
.method(ElementMatchers.isDeclaredBy(RetrieveFromArray.class))
.intercept(new Implementation.Compound(new LoadReferenceParamImplementation(1),
new RelativeRetrieveArrayImplementation(1),
new ReturnAppenderImplementation(ReturnAppender.ReturnType.INT)))
.make();
Class<?> dynamicType = arr.load(RetrieveFromArray.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
RetrieveFromArray test = (RetrieveFromArray) dynamicType.newInstance();
int result = test.returnVal(0, 1);
assertEquals(1, result);
}
示例15: inPlaceSet
import net.bytebuddy.matcher.ElementMatchers; //导入依赖的package包/类
@Test
public void inPlaceSet() throws Exception {
DynamicType.Unloaded<SetValueInPlace> val =
new ByteBuddy().subclass(SetValueInPlace.class)
.method(ElementMatchers.isDeclaredBy(SetValueInPlace.class))
.intercept(new StackManipulationImplementation(new StackManipulation.Compound(
MethodVariableAccess.REFERENCE.loadOffset(1),
MethodVariableAccess.INTEGER.loadOffset(2),
MethodVariableAccess.INTEGER.loadOffset(3),
ArrayStackManipulation.store(), MethodReturn.VOID)))
.make();
val.saveIn(new File("target"));
SetValueInPlace dv = val.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded()
.newInstance();
int[] ret = {2, 4};
int[] assertion = {1, 4};
dv.update(ret, 0, 1);
assertArrayEquals(assertion, ret);
}