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 1020745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:12:18+00:00 2026-05-16T11:12:18+00:00

I want to use local variable annotations to do better AOP. One idea is

  • 0

I want to use local variable annotations to do better AOP. One idea is to implement the Future<T> concept with a proxy using an annotation.

@NonBlocking ExpensiveObject exp = new ExpensiveObject(); 
//returns immediately, but has threaded out instantiation of the ExpensiveObject.

exp.doStuff(); 
//okay, now it blocks until it's finished instantiating and then executes #doStuff

Can I sick AspectJ on this somehow and get what I want done with local variable annotations? I know other threads have indicated that Java doesn’t really support them but it would be magical. I really don’t want to pass around a Future and break encapsulation.

  • 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-16T11:12:19+00:00Added an answer on May 16, 2026 at 11:12 am

    You can not do this with a proxy, but real aspectj bytecode weaving will get you there if you annotate the type instead of the local variable. (I don’t think local variable access is supported as a pointcut). Anyway, here’s some code.

    An annotation:

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface Later {}
    

    A class marked with this annotation:

    package com.dummy.aspectj;
    @Later
    public class HeavyObject{
    
        public HeavyObject(){
            System.out.println("Boy, I am heavy");
        }
    }
    

    A main class:

    package com.dummy.aspectj;
    public class HeavyLifter{
    
        public static void main(final String[] args){
            final HeavyObject fatman = new HeavyObject();
            System.out.println("Finished with main");
        }
    
    }
    

    and an aspect:

    package com.dummy.aspectj;
    public aspect LaterAspect{
    
        pointcut laterInstantiation() :
            execution(@Later *.new(..))    ;
    
        void around() : laterInstantiation() {
            new Thread(new Runnable(){
                @Override
                public void run(){
                    System.out.println("Wait... this is too heavy");
    
                    try{
                        Thread.sleep(2000);
                    } catch(final InterruptedException e){
                        throw new IllegalStateException(e);
                    }
                    System.out.println("OK, now I am up to the task");
                    proceed();
                }
            }).start();
        }
    
    }
    

    Here is the output from HeavyLifter when you run it as an AspectJ/Java Application from eclipse:

    Finished with main
    Wait... this is too heavy
    OK, now I am up to the task
    Boy, I am heavy
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In VBA, I want to use a name for a local variable that I'd
I want to use that i declare local variable like a as clause ,
I want to use a bunch of local variables defined in a function, outside
I want to save an high-score for local use only. A single int, that
First, I don't want to use a database or a local file. Is it
I want use this 1 for using Bar code or QR code scanner. I
In a Google Chrome Extension, I want to use chrome.storage.local (as opposed to localStorage)
I only want to remove a value.. I don't need to use the variable
I want to pass a local variable that contains the origin to come on
I want use BYTE_ORDER macro in my Xcode project but i can't because i

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.