Skip to content

Commit

Permalink
Fix java module error in JDK 17
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed May 17, 2024
1 parent 1933afb commit 31cf6e6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void export(ClassLoader loader, JavaModule module, PluginDefinition defi
int index = type.lastIndexOf('.');
if (index > 0) {
String packageName = type.substring(0, index);
if (!isExportedOrOpen(exportModule, packageName, definitionModule) && exported.add(packageName)) {
if (!isExportedAndOpen(exportModule, packageName, definitionModule) && exported.add(packageName)) {
addExportOrOpen(exportModule, packageName, definitionModule);
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ private JavaModule getModule(ClassLoader loader, JavaModule module, String type)
}

/**
* Checks if a package is already exported or opened from one module to another. This is used
* Checks if a package is already exported and opened from one module to another. This is used
* to avoid unnecessary module modifications if the access is already available.
*
* @param source The module from which the package is to be exported or opened.
Expand All @@ -218,8 +218,9 @@ private JavaModule getModule(ClassLoader loader, JavaModule module, String type)
* @return {@code true} if the package is already exported or opened to the target module,
* {@code false} otherwise.
*/
private boolean isExportedOrOpen(JavaModule source, String packageName, JavaModule target) {
return source.isExported(new PackageDescription.Simple(packageName), target);
private boolean isExportedAndOpen(JavaModule source, String packageName, JavaModule target) {
PackageDescription.Simple packageDescription = new PackageDescription.Simple(packageName);
return source.isExported(packageDescription, target) && source.isOpened(packageDescription, target);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.inject.annotation.InjectLoader;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.core.plugin.definition.InterceptorDefinition;
import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter;
import com.jd.live.agent.core.plugin.definition.PluginDefinition;
import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter;
import com.jd.live.agent.core.plugin.definition.*;
import com.jd.live.agent.core.thread.Camera;
import com.jd.live.agent.governance.config.GovernanceConfig;
import com.jd.live.agent.plugin.transmission.thread.config.ThreadConfig;
Expand All @@ -48,7 +45,7 @@
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_LANE_ENABLED, matchIfMissing = true)
}, relation = ConditionalRelation.OR)
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_TRANSMISSION_THREADPOOL_ENABLED)
public class ExecutorDefinition extends PluginDefinitionAdapter {
public class ExecutorDefinition extends PluginDefinitionAdapter implements PluginImporter {
private static final String TYPE_EXECUTOR = "java.util.concurrent.Executor";

private static final String METHOD_EXECUTE = "execute";
Expand All @@ -70,4 +67,9 @@ public ExecutorDefinition() {
new InterceptorDefinitionAdapter(MatcherBuilder.in(METHODS).and(MatcherBuilder.isPublic()),
() -> new ExecutorInterceptor(handlers, threadConfig))};
}

@Override
public String[] getImports() {
return new String[]{"java.util.concurrent.FutureTask"};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.inject.annotation.InjectLoader;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.core.plugin.definition.InterceptorDefinition;
import com.jd.live.agent.core.plugin.definition.InterceptorDefinitionAdapter;
import com.jd.live.agent.core.plugin.definition.PluginDefinition;
import com.jd.live.agent.core.plugin.definition.PluginDefinitionAdapter;
import com.jd.live.agent.core.plugin.definition.*;
import com.jd.live.agent.core.thread.Camera;
import com.jd.live.agent.governance.config.GovernanceConfig;
import com.jd.live.agent.plugin.transmission.thread.config.ThreadConfig;
Expand All @@ -48,7 +45,7 @@
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_LANE_ENABLED, matchIfMissing = true)
}, relation = ConditionalRelation.OR)
@ConditionalOnProperty(value = GovernanceConfig.CONFIG_TRANSMISSION_THREADPOOL_ENABLED)
public class ScheduledExecutorServiceDefinition extends PluginDefinitionAdapter {
public class ScheduledExecutorServiceDefinition extends PluginDefinitionAdapter implements PluginImporter {
private static final String TYPE_SCHEDULED_EXECUTOR_SERVICE = "java.util.concurrent.ScheduledExecutorService";

private static final String METHOD_SCHEDULE = "schedule";
Expand All @@ -74,4 +71,9 @@ public ScheduledExecutorServiceDefinition() {
() -> new ExecutorInterceptor(handlers, threadConfig))};
}

@Override
public String[] getImports() {
return new String[]{"java.util.concurrent.FutureTask"};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,26 @@ public class ExecutorInterceptor extends InterceptorAdaptor {

private static final String FIELD_CALLABLE = "callable";

private static Field CALLABLE_FIELD;
private final Field callableField;

private final Camera[] cameras;

private final ThreadConfig threadConfig;

static {
try {
CALLABLE_FIELD = FutureTask.class.getDeclaredField(FIELD_CALLABLE);
CALLABLE_FIELD.setAccessible(true);
} catch (NoSuchFieldException ignore) {
}
}

public ExecutorInterceptor(List<Camera> cameras, ThreadConfig threadConfig) {
this.cameras = cameras == null ? new Camera[0] : cameras.toArray(new Camera[0]);
this.threadConfig = threadConfig;
this.callableField = getCallableField();
}

private Field getCallableField() {
Field result = null;
try {
result = FutureTask.class.getDeclaredField(FIELD_CALLABLE);
result.setAccessible(true);
} catch (NoSuchFieldException ignore) {
}
return result;
}

@Override
Expand Down Expand Up @@ -103,9 +106,9 @@ private Object unwrap(Object argument) {
if (argument instanceof AbstractThreadAdapter) {
return argument;
}
if (argument instanceof FutureTask && CALLABLE_FIELD != null) {
if (argument instanceof FutureTask && callableField != null) {
try {
return CALLABLE_FIELD.get(argument);
return callableField.get(argument);
} catch (Exception ignore) {
}
}
Expand Down

0 comments on commit 31cf6e6

Please sign in to comment.