St.Antario
St.Antario

Reputation: 27375

Understanding java compilation and inheritance

I'm reading JMH samples and now I'm at the section about inheritance. Here is the remark they made:

because we only know the type hierarchy during the compilation, it is only possible during the same compilation session. That is, mixing in the subclass extending your benchmark class after the JMH compilation would have no effect.

I haven't thought of this aspect of compilation and so this doesn't seem quite clear to me. We could use Class::getSuperClass though. Example:

@Benchmark
public abstract class MyBenchmark{
    public void mb(){
        doSome();
    }

    public abstract doSome();
}

I thought that when compiling this class JHM uses annotation processor for benchmark generation. And then if we try to compile a subclass say

public class MyConcreteBenchmark extends MyBenchmark {
    @Override
    public void doSome(){
        //Do some
    }
}

it has no effect because annotation processor has nothing to process.

Upvotes: 1

Views: 201

Answers (1)

devops
devops

Reputation: 9179

JHM comes before compiling (analyze and generate). Something like preprocessor or precompiler. Therefore jmh can not see the inheritance tree and it is not possible to see inherited annotations.

Lombock for example works in the same way. Here is an image demonstrating how it works (just replace in mind Lombok by JMH):

enter image description here

Readmore: Project Lombok: Creating Custom Transformations

Upvotes: 2

Related Questions