Skip to content

Commit 70a1998

Browse files
committed
Allow disabling filter
1 parent ae4669c commit 70a1998

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

samples/semantickernel-concepts/semantickernel-syntax-examples/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example69_MutableKernelPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.microsoft.semantickernel.Kernel;
55
import com.microsoft.semantickernel.plugin.KernelPlugin;
6+
import com.microsoft.semantickernel.plugin.KernelPluginFactory;
67
import com.microsoft.semantickernel.semanticfunctions.KernelFunction;
78
import com.microsoft.semantickernel.semanticfunctions.annotations.DefineKernelFunction;
89

@@ -16,6 +17,7 @@ public class Example69_MutableKernelPlugin {
1617
*/
1718
public static void main(String[] args) throws NoSuchMethodException {
1819
System.out.println("======== Example69_MutableKernelPlugin ========");
20+
KernelPluginFactory.setTypeFilterEnable(false);
1921

2022
KernelPlugin plugin = new KernelPlugin("Plugin", "Mutable plugin", null);
2123
plugin.addFunction(KernelFunction.createFromMethod(

semantickernel-api/src/main/java/com/microsoft/semantickernel/plugin/KernelPluginFactory.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.charset.Charset;
2626
import java.nio.file.Files;
2727
import java.nio.file.Path;
28+
import java.time.temporal.Temporal;
2829
import java.util.ArrayList;
2930
import java.util.Arrays;
3031
import java.util.Collections;
@@ -54,7 +55,13 @@ public class KernelPluginFactory {
5455
private static final CaseInsensitiveMap<Class<?>> COMMON_CLASS_NAMES = new CaseInsensitiveMap<>();
5556
private static final Map<Class<?>, Class<?>> BOXED_FROM_PRIMITIVE = new HashMap<>();
5657

58+
public static final String CLASS_FILTER_ENABLE_PROPERTY = "semantic-kernel.class-filter-enable";
59+
private static Boolean CLASS_FILTER_ENABLE;
60+
5761
static {
62+
CLASS_FILTER_ENABLE = Boolean.parseBoolean(
63+
System.getProperty(CLASS_FILTER_ENABLE_PROPERTY, "true"));
64+
5865
PRIMITIVE_CLASS_NAMES.put("void", void.class);
5966
PRIMITIVE_CLASS_NAMES.put("int", int.class);
6067
PRIMITIVE_CLASS_NAMES.put("double", double.class);
@@ -76,6 +83,9 @@ public class KernelPluginFactory {
7683
COMMON_CLASS_NAMES.put(List.class.getName(), ArrayList.class);
7784
COMMON_CLASS_NAMES.put(Map.class.getName(), HashMap.class);
7885
COMMON_CLASS_NAMES.put(Set.class.getName(), HashSet.class);
86+
COMMON_CLASS_NAMES.put(Temporal.class.getName(), Temporal.class);
87+
COMMON_CLASS_NAMES.put(java.time.OffsetDateTime.class.getName(), java.time.OffsetDateTime.class);
88+
COMMON_CLASS_NAMES.put(java.time.ZonedDateTime.class.getName(), java.time.ZonedDateTime.class);
7989

8090
BOXED_FROM_PRIMITIVE.put(void.class, Void.class);
8191
BOXED_FROM_PRIMITIVE.put(int.class, Integer.class);
@@ -89,6 +99,10 @@ public class KernelPluginFactory {
8999

90100
}
91101

102+
public static void setTypeFilterEnable(boolean enable) {
103+
KernelPluginFactory.CLASS_FILTER_ENABLE = enable;
104+
}
105+
92106
/**
93107
* Creates a plugin that wraps the specified target object. Methods decorated with
94108
* {@code {@literal @}DefineSKFunction} will be included in the plugin.
@@ -268,6 +282,9 @@ public static Class<?> getTypeForName(String className) {
268282
}
269283

270284
public static boolean checkClassName(String className) {
285+
if (CLASS_FILTER_ENABLE == false) {
286+
return true;
287+
}
271288
return ClassFilter.CLASS_CHECKER.test(className);
272289
}
273290

@@ -638,8 +655,8 @@ private static boolean evaluateBlock(String className) {
638655
for (String ban : CLASS_BLOCK_LIST) {
639656
if (className.matches(ban)) {
640657
LOGGER.warn(
641-
"Skipping class not allowed by class block list {}, if you wish to unblock this class update the property: {}",
642-
className, CLASS_BLOCK_LIST_PROPERTY_NAME);
658+
"Skipping class not allowed by class block list {}, if you wish to unblock this class update the property: {}. Filtering can also be controlled with {} and KernelPluginFactory.setTypeFilterEnable",
659+
className, CLASS_BLOCK_LIST_PROPERTY_NAME, CLASS_FILTER_ENABLE_PROPERTY);
643660
return false;
644661
}
645662
}
@@ -660,8 +677,8 @@ private static boolean evaluateAllow(String className) {
660677
}
661678

662679
LOGGER.warn(
663-
"Skipping class not allowed by class allow list {}, if you wish to allow this class update the property: {}",
664-
className, CLASS_ALLOW_LIST_DEFAULT);
680+
"Skipping class not allowed by class allow list {}, if you wish to allow this class update the property: {}. Filtering can also be controlled with {} and KernelPluginFactory.setTypeFilterEnable",
681+
className, CLASS_ALLOW_LIST_DEFAULT, CLASS_FILTER_ENABLE_PROPERTY);
665682
return false;
666683
}
667684
}

0 commit comments

Comments
 (0)