Skip to content

Commit

Permalink
[LOG4J2-2604/2649] Change MethodHandle to Constructor to adapt graalvm (
Browse files Browse the repository at this point in the history
apache#291)

* change MethodHandle to Constructor

* format space.

* add comment about why change to Constructor
  • Loading branch information
Mhhli authored and rgoers committed Feb 16, 2020
1 parent bb17938 commit 474d0b0
Showing 1 changed file with 18 additions and 14 deletions.
Expand Up @@ -16,9 +16,7 @@
*/
package org.apache.logging.log4j.core.impl;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.Map.Entry;

Expand Down Expand Up @@ -48,11 +46,17 @@
* @since 2.7
*/
public class ContextDataFactory {
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final String CLASS_NAME = PropertiesUtil.getProperties().getStringProperty("log4j2.ContextData");
private static final Class<? extends StringMap> CACHED_CLASS = createCachedClass(CLASS_NAME);
private static final MethodHandle DEFAULT_CONSTRUCTOR = createDefaultConstructor(CACHED_CLASS);
private static final MethodHandle INITIAL_CAPACITY_CONSTRUCTOR = createInitialCapacityConstructor(CACHED_CLASS);

/**
* In LOG4J2-2649 (https://issues.apache.org/jira/browse/LOG4J2-2649),
* the reporter said some reason about using graalvm to static compile.
* In graalvm doc (https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md),
* graalvm is not support MethodHandle now, so the Constructor need not to return MethodHandle.
*/
private static final Constructor<?> DEFAULT_CONSTRUCTOR = createDefaultConstructor(CACHED_CLASS);
private static final Constructor<?> INITIAL_CAPACITY_CONSTRUCTOR = createInitialCapacityConstructor(CACHED_CLASS);

private static final StringMap EMPTY_STRING_MAP = createContextData(0);

Expand All @@ -71,24 +75,24 @@ private static Class<? extends StringMap> createCachedClass(final String classNa
}
}

private static MethodHandle createDefaultConstructor(final Class<? extends StringMap> cachedClass) {
private static Constructor<?> createDefaultConstructor(final Class<? extends StringMap> cachedClass){
if (cachedClass == null) {
return null;
}
try {
return LOOKUP.findConstructor(cachedClass, MethodType.methodType(void.class));
} catch (final NoSuchMethodException | IllegalAccessException ignored) {
return cachedClass.getConstructor();
} catch (final NoSuchMethodException | IllegalAccessError ignored) {
return null;
}
}

private static MethodHandle createInitialCapacityConstructor(final Class<? extends StringMap> cachedClass) {
private static Constructor<?> createInitialCapacityConstructor(final Class<? extends StringMap> cachedClass){
if (cachedClass == null) {
return null;
}
try {
return LOOKUP.findConstructor(cachedClass, MethodType.methodType(void.class, int.class));
} catch (final NoSuchMethodException | IllegalAccessException ignored) {
return cachedClass.getConstructor(int.class);
} catch (final NoSuchMethodException | IllegalAccessError ignored) {
return null;
}
}
Expand All @@ -98,7 +102,7 @@ public static StringMap createContextData() {
return new SortedArrayStringMap();
}
try {
return (IndexedStringMap) DEFAULT_CONSTRUCTOR.invoke();
return (IndexedStringMap) DEFAULT_CONSTRUCTOR.newInstance();
} catch (final Throwable ignored) {
return new SortedArrayStringMap();
}
Expand All @@ -109,7 +113,7 @@ public static StringMap createContextData(final int initialCapacity) {
return new SortedArrayStringMap(initialCapacity);
}
try {
return (IndexedStringMap) INITIAL_CAPACITY_CONSTRUCTOR.invoke(initialCapacity);
return (IndexedStringMap) INITIAL_CAPACITY_CONSTRUCTOR.newInstance(initialCapacity);
} catch (final Throwable ignored) {
return new SortedArrayStringMap(initialCapacity);
}
Expand Down

0 comments on commit 474d0b0

Please sign in to comment.