Java Code Examples for net.bytebuddy.asm.Advice#Return

The following examples show how to use net.bytebuddy.asm.Advice#Return . 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: SpecialAgentAgent.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodExit
public static void exit(final @Advice.Argument(0) ClassLoader classLoader, final @Advice.Argument(1) String arg, @Advice.Return(readOnly=false, typing=Typing.DYNAMIC) Enumeration<URL> returned) {
  try {
    String classpath = System.getProperty("java.class.path");
    final int index = classpath.indexOf("/opentracing-api-");
    final int start = classpath.lastIndexOf(File.pathSeparatorChar, index);
    final int end = classpath.indexOf(File.pathSeparatorChar, index);
    classpath = classpath.substring(start + 1, end != -1 ? end : classpath.length());
    if (!classpath.endsWith(".jar") && !classpath.endsWith("/"))
      classpath += "/";

    try (final RuleClassLoader ruleClassLoader = new RuleClassLoader(null, null, null, new File(classpath))) {
      returned = ruleClassLoader.getResources(arg); // Why is findResources(arg) not returning expected results?
      returned.hasMoreElements(); // For some reason, if I don't call this, the returned value does not have any elements!!!!
    }

    if (logger.isLoggable(Level.FINEST))
      logger.finest("<<<<<<< Agent#findResources(" + (classLoader == null ? "null" : classLoader.getClass().getName() + "@" + Integer.toString(System.identityHashCode(classLoader), 16)) + "," + arg + "): " + returned);
  }
  catch (final Throwable t) {
    logger.log(Level.SEVERE, "<><><><> AgentAgent.FindResources#exit", t);
  }
}
 
Example 2
Source File: ApacheMonitorFilterAdvice.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void onExitFilterInvoke(@Advice.Argument(1) Invocation invocation,
                                      @Advice.Return Result result,
                                      @Nullable @Advice.Local("span") final Span span,
                                      @Advice.Thrown Throwable t,
                                      @Nullable @Advice.Local("transaction") Transaction transaction) {

    RpcContext context = RpcContext.getContext();
    AbstractSpan<?> actualSpan = context.isConsumerSide() ? span : transaction;
    if (actualSpan == null) {
        return;
    }

    actualSpan.deactivate();
    if (result instanceof AsyncRpcResult) {
        AsyncCallbackCreator callbackCreator = asyncCallbackCreatorClassManager.getForClassLoaderOfClass(Result.class);
        if (callbackCreator == null) {
            actualSpan.end();
            return;
        }
        context.set(DubboTraceHelper.SPAN_KEY, actualSpan);
        result.whenCompleteWithContext(callbackCreator.create(actualSpan));
    } else {
        actualSpan.end();
    }
}
 
Example 3
Source File: BootLoaderAgent.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodExit
public static void exit(final @Advice.Argument(0) String name, @Advice.Return(readOnly=false, typing=Typing.DYNAMIC) Enumeration<URL> returned) {
  if (jarFiles == null)
    return;

  final Set<String> visited = mutex.get();
  if (!visited.add(name))
    return;

  try {
    final URL[] resources = findResources(name, jarFiles, 0, 0);
    if (resources != null) {
      final Enumeration<URL> enumeration = SpecialAgentUtil.enumeration(resources);
      returned = returned == null ? enumeration : new CompoundEnumeration<>(returned, enumeration);
    }
  }
  catch (final Throwable t) {
    log("<><><><> BootLoaderAgent.FindBootstrapResources#exit", t, DefaultLevel.SEVERE);
  }
  finally {
    visited.remove(name);
  }
}
 
Example 4
Source File: StatementInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@VisibleForAdvice
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void onAfterExecute(@Advice.Enter @Nullable Span span,
                                  @Advice.Thrown @Nullable Throwable t,
                                  @Advice.Return long returnValue /* bytebuddy converts int to long for us here ! */) {
    if (span == null) {
        return;
    }

    if (t == null) {
        span.getContext()
            .getDb()
            .withAffectedRowsCount(returnValue);
    }

    span.captureException(t)
        .deactivate()
        .end();
}
 
Example 5
Source File: CodeTemplates.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Advice.OnMethodExit
static void $$_hibernate_getDirtyAttributes(
		@Advice.This ExtendedSelfDirtinessTracker self,
		@Advice.Return(readOnly = false) String[] returned,
		@Advice.FieldValue(value = EnhancerConstants.TRACKER_FIELD_NAME, readOnly = false) DirtyTracker $$_hibernate_tracker,
		@Advice.FieldValue(value = EnhancerConstants.TRACKER_COLLECTION_NAME, readOnly = false) CollectionTracker $$_hibernate_collectionTracker) {
	if ( $$_hibernate_collectionTracker == null ) {
		returned = ( $$_hibernate_tracker == null ) ? new String[0] : $$_hibernate_tracker.get();
	}
	else {
		if ( $$_hibernate_tracker == null ) {
			$$_hibernate_tracker = new SimpleFieldTracker();
		}
		self.$$_hibernate_getCollectionFieldDirtyNames( $$_hibernate_tracker );
		returned = $$_hibernate_tracker.get();
	}
}
 
Example 6
Source File: JdbcAgentRule.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unused")
@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void exit(@Advice.Return(readOnly = false, typing = Typing.DYNAMIC) Boolean returned, @Advice.Thrown(readOnly = false, typing = Typing.DYNAMIC) Throwable thrown) {
  if (thrown instanceof EarlyReturnException) {
    thrown = null;
    returned = Boolean.TRUE;
  }
}
 
Example 7
Source File: ElasticsearchClientSyncInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class)
public static void onAfterExecute(@Advice.Return @Nullable Response response,
                                  @Advice.Local("span") @Nullable Span span,
                                  @Advice.Local("helper") @Nullable ElasticsearchRestClientInstrumentationHelper helper,
                                  @Advice.Thrown @Nullable Throwable t) {
    if (helper != null && span != null) {
        try {
            helper.finishClientSpan(response, span, t);
        } finally {
            span.deactivate();
        }
    }
}
 
Example 8
Source File: JdbcAgentRule.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void exit(@Advice.Return(readOnly = false, typing = Typing.DYNAMIC) Object returned, @Advice.Thrown(readOnly = false, typing = Typing.DYNAMIC) Throwable thrown) throws Exception {
  if (thrown instanceof EarlyReturnException) {
    returned = WrapperProxy.wrap(returned, ((EarlyReturnException)thrown).getReturnValue());
    thrown = null;
  }
}
 
Example 9
Source File: ScopeManagerInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@VisibleForAdvice
@Advice.OnMethodExit(suppress = Throwable.class)
public static void getCurrentSpan(@Advice.Return(readOnly = false) Object span) {
    if (tracer != null) {
        span = tracer.getActive();
    }
}
 
Example 10
Source File: ScopeManagerInstrumentation.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@VisibleForAdvice
@Advice.OnMethodExit(suppress = Throwable.class)
public static void getCurrentTraceContext(@Advice.Return(readOnly = false) Object traceContext) {
    if (tracer != null) {
        traceContext = tracer.getActive();
    }
}
 
Example 11
Source File: HazelcastAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit
public static void exit(final @ClassName String className, final @Advice.Origin String origin, @Advice.Return(readOnly = false, typing = Typing.DYNAMIC) Object returned) {
  if (isAllowed(className, origin))
    returned = HazelcastAgentIntercept.getAllInstances(returned);
}
 
Example 12
Source File: AbstractSpanInstrumentation.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@VisibleForAdvice
@Advice.OnMethodExit(suppress = Throwable.class)
public static void doCreateSpan(@Advice.FieldValue(value = "span", typing = Assigner.Typing.DYNAMIC) AbstractSpan<?> context,
                                @Advice.Return(readOnly = false) Object result) {
    result = context.createSpan();
}
 
Example 13
Source File: KafkaAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit
public static void exit(final @ClassName String className, final @Advice.Origin String origin, final @Advice.Return(typing = Typing.DYNAMIC) Object returned) {
  if (isAllowed(className, origin))
    KafkaAgentIntercept.onConsumerExit(returned);
}
 
Example 14
Source File: CodeTemplates.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Advice.OnMethodExit
static void $$_hibernate_getCollectionTracker( @Advice.Return(readOnly = false) CollectionTracker returned) {
	returned = NoopCollectionTracker.INSTANCE;
}
 
Example 15
Source File: AbstractSpanInstrumentation.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@VisibleForAdvice
@Advice.OnMethodExit(suppress = Throwable.class)
public static void getTraceId(@Advice.FieldValue(value = "span", typing = Assigner.Typing.DYNAMIC) AbstractSpan<?> context,
                              @Advice.Return(readOnly = false) String traceId) {
    traceId = context.getTraceContext().getTraceId().toString();
}
 
Example 16
Source File: ZuulAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit
public static void exit(final @ClassName String className, final @Advice.Origin String origin, final @Advice.Return Object returned, final @Advice.Argument(value = 0) Object arg) {
  if (isAllowed(className, origin))
    ZuulAgentIntercept.exit(returned, arg);
}
 
Example 17
Source File: RabbitMQAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit(onThrowable = Throwable.class)
public static void exit(final @ClassName String className, final @Advice.Origin String origin, final @Advice.Thrown Throwable thrown, final @Advice.Argument(value = 0) Object queue, final @Advice.Return Object returned) {
  if (isAllowed(className, origin))
    RabbitMQAgentIntercept.exitGet(returned, queue, thrown);
}
 
Example 18
Source File: ThriftAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit
public static void exit(final @ClassName String className, final @Advice.Origin String origin, @Advice.Return(readOnly = false, typing = Typing.DYNAMIC) Object returned) {
  if (isAllowed(className, origin))
    returned = ThriftAgentIntercept.getProcessor(returned);
}
 
Example 19
Source File: MongoDriverAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit
public static void exit(final @ClassName String className, final @Advice.Origin String origin, final @Advice.Return Object returned) {
  if (isAllowed(className, origin))
    MongoDriverAgentIntercept.exit(returned);
}
 
Example 20
Source File: DubboRpcAgentRule.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Advice.OnMethodExit
public static void exit(final @ClassName String className, final @Advice.Origin String origin, final @Advice.Argument(value = 1) Object key, @Advice.Return(readOnly = false, typing = Typing.DYNAMIC) Object returned) {
  if (key instanceof String && ("service.filter".equals(key) || "reference.filter".equals(key)) && isAllowed(className, origin))
    returned = DubboRpcAgentIntercept.exit(returned);
}