Skip to content

Commit

Permalink
GH-197 - Avoid processing static overloads of JPA lifecycle methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Nov 8, 2023
1 parent d519fb8 commit ae267f7
Showing 1 changed file with 25 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2021-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,16 @@
*/
package org.jmolecules.bytebuddy;

import static net.bytebuddy.matcher.ElementMatchers.*;

import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.method.MethodDescription.InDefinedShape;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType.Builder;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.matcher.ElementMatcher;

import java.lang.annotation.Annotation;
import java.util.Arrays;
Expand Down Expand Up @@ -91,7 +94,7 @@ public Builder<?> apply(Function<String, Advice> forExisting, Supplier<Implement
continue;
}

result = result.visit(forExisting.apply(name).on(ElementMatchers.hasMethodName(name)));
result = result.visit(forExisting.apply(name).on(nonStaticNoParametersNamed(name)));
handledMethods.add(name);

} else {
Expand Down Expand Up @@ -120,14 +123,17 @@ private static Map<Class<? extends Annotation>, Optional<String>> detectCallback

Map<Class<? extends Annotation>, Optional<String>> result = new HashMap<>();

type.getDeclaredMethods().forEach(method -> {
type.getDeclaredMethods().stream()
.filter(it -> !it.isStatic())
.filter(it -> it.getParameters().isEmpty())
.forEach(method -> {

Arrays.stream(annotations).forEach(annotation -> {
result.compute(annotation, (annotationType, methodName) -> methodName != null && methodName.isPresent()
? methodName
: getMethodNameIfAnnotated(method, annotationType));
});
});
Arrays.stream(annotations).forEach(annotation -> {
result.compute(annotation, (annotationType, methodName) -> methodName != null && methodName.isPresent()
? methodName
: getMethodNameIfAnnotated(method, annotationType));
});
});

return result;

Expand All @@ -139,4 +145,13 @@ private static Optional<String> getMethodNameIfAnnotated(InDefinedShape method,
? Optional.of(method.getName())
: Optional.empty();
}

private static ElementMatcher<? super MethodDescription> nonStaticNoParametersNamed(String name) {

ElementMatcher<MethodDescription> doesNotHaveParameters = method -> method.getParameters().isEmpty();

return hasMethodName(name)
.and(not(isStatic()))
.and(doesNotHaveParameters);
}
}

0 comments on commit ae267f7

Please sign in to comment.