Bug 550311 - Synthetic $SWITCH_TABLE$ method being processed by compiler
Summary: Synthetic $SWITCH_TABLE$ method being processed by compiler
Status: RESOLVED FIXED
Alias: None
Product: AspectJ
Classification: Tools
Component: Compiler (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 1.9.5   Edit
Assignee: aspectj inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-08-21 18:40 EDT by Semyon Danilov CLA
Modified: 2019-11-27 17:16 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Semyon Danilov CLA 2019-08-21 18:40:08 EDT
Eclipse Compiler generates synthetic $SWITCH_TABLE$ method whenever switch on enum value is used. The method is being generated in class where switch occurs. So, if there is an aspect with a pointcut at all of the class' methods, generated method is being processed too. 

I suppose it's not correct behaviour, as $SWITCH_TABLE$ method is some performance tweak, something user don't know about (because didn't write himself), thus it shouldn't be processed.

Here is the way to reproduce it:

public class HelloWorldEnumSwitch {

	public static void main(String[] args) {
		switch(TestEnum.A) {
			case A:
				System.out.println("A");
				break;
			case B:
				System.out.println("B");
		}

	}

	public static enum TestEnum {
		A,
		B;

		private TestEnum() {
		}
	}
}

@Aspect
public class TestAspect {

    @Around("execution(* *..*.*(..))")
    public Object beanAnnotatedWithMonitor(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println(pjp.getSignature().getName());
        return pjp.proceed();
    }

}


During execution $SWITCH_TABLE$HelloWorldEnumSwitch$TestEnum will be printed.


I made a small pull request with a fix: https://github.com/eclipse/org.aspectj/pull/60
It also contains a test for such behaviour.
Comment 1 Andrew Clement CLA 2019-09-10 12:29:00 EDT
With binary weaving also being an option, ought to consider what javac does here too.

I see that javac (at least 1.8 that I'm looking at) creates an inner class with a static initializer that does the job.

The inner class is marked synthetic, called HelloWorldEnumSwitch$1

Has a single int[] field: $SwitchMap$HelloWorldEnumSwitch$TestEnum;

Such a shame the class name didn't indicate the use as a switch table initializer. So the PR will only fix the pure ajc compilation route, not the binary weaving. But I might still integrate it as a step forward, still chewing on it :)
Comment 2 Andrew Clement CLA 2019-09-10 19:47:43 EDT
Integrated the PR, thanks for that. But left with failing tests because where the test is has introduced the need to compute stackmaps before the class is loaded which I don't think the old tests needed. working through it.
Comment 3 Andrew Clement CLA 2019-09-11 11:36:10 EDT
I moved the test out of weaver and created a formal 195 test for it. all working now. But I haven't addressed the javac scenario, yet.
Comment 4 Semyon Danilov CLA 2019-09-13 12:10:40 EDT
Wow! Haven't really thought about javac scenario, sorry =) Well, never used it with AspectJ, actually.

This problem weaving looks really interesting, I'll try to look through it too :)
Comment 5 Andrew Clement CLA 2019-11-27 17:16:11 EST
Closing this for the work done, we can open another for javac if someone complains about it.