Skip to content

Use Lookup class loading strategy if available - #895

Merged
leonard84 merged 5 commits into
spockframework:masterfrom
marcphilipp:issues/843-java-11-compat
Sep 9, 2018
Merged

leonard84 merged 5 commits into
spockframework:masterfrom
marcphilipp:issues/843-java-11-compat

Conversation

@marcphilipp

Copy link
Copy Markdown
Member

In order to create mock objects on Java 11, ProxyBasedMockFactory now uses ByteBuddy's Lookup-based ClassLoadingStrategy, if available.

Resolves #843.

In order to create mock objects on Java 11, `ProxyBasedMockFactory`
now uses ByteBuddy's `Lookup`-based `ClassLoadingStrategy`, if
available.

Resolves spockframework#843.
@marcphilipp marcphilipp self-assigned this Sep 7, 2018
* Serves as a reference point for method handle based class loading and is makes the containing package
* non-empty so generated classes can be legally defined inside of it.
*/
public class CodegenDummy {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd call it CodegenTarget

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me. 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just Target?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also ok


private static final TypeCache<TypeCache.SimpleKey> CACHE =
new TypeCache.WithInlineExpunction<>(TypeCache.Sort.SOFT);
private static final Random RANDOM = new Random();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm Random is not thread-safe IIRC that about ThreadLocalRandom?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instances of java.util.Random are threadsafe. However, the concurrent use of the same java.util.Random instance across threads may encounter contention and consequent poor performance. Consider instead using java.util.concurrent.ThreadLocalRandom in multithreaded designs.

So it's thread-safe but may have poor performance... I'll change it to ThreadLocalRandom.

if (ClassInjector.UsingReflection.isAvailable()) {
return ClassLoadingStrategy.Default.INJECTION;
}
throw new IllegalStateException("No class loading strategy available");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we fall back to WRAPPER

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which cases would that get used?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a pre Java-9 JVM does not support Unsafe but I would not know any.

@leonard84

Copy link
Copy Markdown
Member

The code should include a notice of origin pointing to Mockito

What about the classloader check https://github.com/mockito/mockito/blob/4f72147c464c1a8a642d01fc3334e98e92b464cd/src/main/java/org/mockito/internal/creation/bytebuddy/SubclassInjectionLoader.java#L75 don't we need this also?

@leonard84

Copy link
Copy Markdown
Member

@raphw maybe you could also take a look

@codecov

codecov Bot commented Sep 7, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #895 into master will decrease coverage by 0.03%.
The diff coverage is 72.41%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #895      +/-   ##
============================================
- Coverage     75.05%   75.02%   -0.04%     
+ Complexity     3427     3426       -1     
============================================
  Files           368      368              
  Lines         10552    10579      +27     
  Branches       1333     1342       +9     
============================================
+ Hits           7920     7937      +17     
- Misses         2165     2168       +3     
- Partials        467      474       +7
Impacted Files Coverage Δ Complexity Δ
...kframework/mock/runtime/ProxyBasedMockFactory.java 84.46% <72.41%> (-5.01%) 6 <0> (ø)
...ckframework/spring/mock/DelegatingInterceptor.java 52.5% <0%> (-5%) 6% <0%> (-1%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8478419...9c92669. Read the comment docs.

@raphw

raphw commented Sep 7, 2018

Copy link
Copy Markdown
Contributor

Mockito sometimes creates class loaders if a mock is loaded by a class loader that cannot see Mockito. This is a problem especially in OSGi environments, therefore the indirection and the check.

@raphw

raphw commented Sep 7, 2018

Copy link
Copy Markdown
Contributor

Looks good otherwise!

@marcphilipp

Copy link
Copy Markdown
Member Author

@leonard84 PTAL! 🙂

@leonard84
leonard84 merged commit a463f1b into spockframework:master Sep 9, 2018
MartyIX pushed a commit to MartyIX/spock that referenced this pull request Oct 14, 2018
In order to create mock objects on Java 11+, `ProxyBasedMockFactory`
now uses ByteBuddy's `Lookup`-based `ClassLoadingStrategy`, if
`Reflection` strategy is not available.

Resolves spockframework#843
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants