Exception in StringConcatFactory - Java 9

While studying the java9 class, StringConcatFactoryI can’t understand why the following code with the expression MethodHandles.publicLookup()throws StringConcatException, and if used MethodHandles.lookup(), everything works fine.

According to java search docs:

"lookup - represents the search context with the access privilege of the caller"

StringConcatFactory.makeConcat(MethodHandles.publicLookup(),
"abc",MethodType.methodType(String.class));//Exception Here


StringConcatFactory.makeConcat(MethodHandles.lookup(), 
"abc", MethodType.methodType(String.class)); //Working fine

I'm not sure where I am going wrong? Please help me understand this behavior.

+4
source share
3 answers

The javadoc for makeConcattalks about the first parameter:

search Represents a search context with caller availability privileges

, publicLookup, .

, :

MethodType concatType = MethodType.methodType(String.class); // No arguments, returns String
StringConcatFactory.makeConcat(MethodHandles.publicLookup(), "abc", concatType); // Exception

StringConcatFactory#doStringConcat:

if ((lookup.lookupModes() & MethodHandles.Lookup.PRIVATE) == 0) {
    throw new StringConcatException("Invalid caller: " +
            lookup.lookupClass().getName());
}

, publicLookup :

System.out.println((MethodHandles.publicLookup().lookupModes()
    & MethodHandles.Lookup.PRIVATE) != 0); // false
System.out.println((MethodHandles.lookup().lookupModes()
    & MethodHandles.Lookup.PRIVATE) != 0); // true
+4

, , publicLookup:

StringConcatFactory.makeConcat(MethodHandles.publicLookup(), "abc", MethodType.methodType(String.class));

StringConcatException, , lookup

StringConcatFactory.makeConcat(MethodHandles.lookup(), "abc", MethodType.methodType(String.class));

, .

Javadoc publicLookup, @GhostCat

publicLookup => PUBLIC_LOOKUP => (PUBLIC|UNCONDITIONAL) modes

, . PUBLIC UNCONDITIONAL modes. , .

lookup

lookup => (lookupClass => Reflection.getCallerClass, FULL_POWER_MODES  => (ALL_MODES & ~UNCONDITIONAL))

. . Factory , -, . , .

, . , , .

C , , JVM invokedynamic , C.

+4

javadoc publicLookup() :

, .

lookup():

- . . Factory , -, . , .

javadoc makeConcat() :

throws StringConcatException - - , , .

Given the fact that there is no more detailed information in the question, the most likely answer is: you have some kind of "resolution" problem. Perhaps you are trying to "concat" something that is simply unavailable when using the "minimal trust".

+3
source

Source: https://habr.com/ru/post/1685066/


All Articles