Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7529551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:41:25+00:00 2026-05-30T04:41:25+00:00

So, given the following code: public MyInterface getMyInterface() { return new MyInterface() { public

  • 0

So, given the following code:

public MyInterface getMyInterface() {
    return new MyInterface() {
        public SomethingElse getSomethingElse() {
           // ....
        }
    }
}

...
MyInterface obj = getMyInterface();

Is there some way to instrument a call to getSomethingElse() on that obj? To go in and do some bytecode modification or something?

I have production code in there that in a different situation (call it “design time”) I want to add some tracing/logging and such code for help in troubleshooting and analysis. Performance is critical for the production case so I want to leave it without the extra tracing/logging overhead. But in the design time situation, I want to have all the trace info.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:41:26+00:00Added an answer on May 30, 2026 at 4:41 am

    Yes, it is possible to do what you’re asking, although there are definitely better ways to accomplish it – the most obvious would be to create a default implementation of MyInterface, and then a “tracing” subclass of it that extends and logs before invoking the superclass version.

    If instrumentation is your only option, then when running at design time, you can start your project with a java agent in Java 5 or add a java agent to the classpath at runtime in Java 6. See the instrumentation documentation.

    To instrument the class, you will probably want to use a tool like ASM. The steps would be something like this:

    1. In your Agent class, implement java.lang.instrument.ClassFileTransformer .
    2. In your agentmain() or premain() method, request to transform classes.
    3. When you receive a call to the transform method, you can check if the class implements MyInterface by using Class.getInterfaces().
    4. Optionally, you can check to see if its Class.getEnclosingClass() is the class in which you wrote/found this code.
    5. If the Class passes these sanity checks, then create a ClassWriter that adds logging to the getSomethingElse() method. The ASMifier helps a lot when trying to figure out how to generate the code you want.

    Then, in production, none of that code will exist. In development, you would add your Java Agent in your environment, which would enable your debugging.

    Again, there are almost certainly better ways to do this, but there are good reasons to use instrumentation, and this is a mini-crash course in doing it.

    Hope that helps,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code, is there a way I can call class A's version
Given the following code: Class User{ Task m_Task; public function getTask(Do work) { return
Given the following code, how can I return some value (DateTime.Now.ToString()) if the exception
Given the following code: public static bool UpdateUser(string userId, string jobTitle) { return GetProvider().UpdateUser
Given the following code Public Shared Sub DoAsyncAction() Using asmxProxy As New MyAsmxServiceProxy() AddHandler
Given the following code, public T Execute<T>(Func<T> methodParam) { return methodParam (); } public
In c#, Given the following code: public class Person { public int PersonID {
Problem One friend suggested an interesting problem. Given the following code: public class OuterClass
Given the following code: var people = new List<person>(){ new person { Name =
Given the following code: using (var client = new WebClient()) { string url =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.