本文整理匯總了Java中groovy.lang.GroovyClassLoader.parseClass方法的典型用法代碼示例。如果您正苦於以下問題:Java GroovyClassLoader.parseClass方法的具體用法?Java GroovyClassLoader.parseClass怎麽用?Java GroovyClassLoader.parseClass使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類groovy.lang.GroovyClassLoader
的用法示例。
在下文中一共展示了GroovyClassLoader.parseClass方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: newCredentialSelectionPredicate
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
/**
* Gets credential selection predicate.
*
* @param selectionCriteria the selection criteria
* @return the credential selection predicate
*/
public static Predicate<org.apereo.cas.authentication.Credential> newCredentialSelectionPredicate(final String selectionCriteria) {
try {
if (StringUtils.isBlank(selectionCriteria)) {
return credential -> true;
}
if (selectionCriteria.endsWith(".groovy")) {
final ResourceLoader loader = new DefaultResourceLoader();
final Resource resource = loader.getResource(selectionCriteria);
if (resource != null) {
final String script = IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8);
final GroovyClassLoader classLoader = new GroovyClassLoader(Beans.class.getClassLoader(),
new CompilerConfiguration(), true);
final Class<Predicate> clz = classLoader.parseClass(script);
return clz.newInstance();
}
}
final Class predicateClazz = ClassUtils.getClass(selectionCriteria);
return (Predicate<org.apereo.cas.authentication.Credential>) predicateClazz.newInstance();
} catch (final Exception e) {
final Predicate<String> predicate = Pattern.compile(selectionCriteria).asPredicate();
return credential -> predicate.test(credential.getId());
}
}
示例2: initGroovyTransformer
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
private void initGroovyTransformer(String code, List<String> extraPackage) {
GroovyClassLoader loader = new GroovyClassLoader(GroovyTransformer.class.getClassLoader());
String groovyRule = getGroovyRule(code, extraPackage);
Class groovyClass;
try {
groovyClass = loader.parseClass(groovyRule);
} catch (CompilationFailedException cfe) {
throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, cfe);
}
try {
Object t = groovyClass.newInstance();
if (!(t instanceof Transformer)) {
throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, "datax bug! contact askdatax");
}
this.groovyTransformer = (Transformer) t;
} catch (Throwable ex) {
throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_GROOVY_INIT_EXCEPTION, ex);
}
}
示例3: getGroovyObject
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
@SuppressWarnings("resource")
private static final GroovyObject getGroovyObject(String rule)
throws IllegalAccessException, InstantiationException {
if (!RULE_CLASS_CACHE.containsKey(rule)) {
synchronized (GroovyRuleEngine.class) {
if (!RULE_CLASS_CACHE.containsKey(rule)) {
Matcher matcher = DimensionRule.RULE_COLUMN_PATTERN.matcher(rule);
StringBuilder engineClazzImpl = new StringBuilder(200)
.append("class RuleEngineBaseImpl extends " + RuleEngineBase.class.getName() + "{")
.append("Object execute(Map context) {").append(matcher.replaceAll("context.get(\"$1\")"))
.append("}").append("}");
GroovyClassLoader loader = new GroovyClassLoader(AbstractDimensionRule.class.getClassLoader());
Class<?> engineClazz = loader.parseClass(engineClazzImpl.toString());
RULE_CLASS_CACHE.put(rule, engineClazz);
}
}
}
return (GroovyObject) RULE_CLASS_CACHE.get(rule).newInstance();
}
示例4: loadGroovy
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
public static void loadGroovy(String name, String content) throws Exception {
logger.info("begin load groovy name=" + name);
logger.debug("groovy content=" + content);
if(name.toLowerCase().contains("abstract")){
logger.info("skip load groovy name=" + name);
return;
}
ClassLoader parent = GroovyLoadUtil.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class<?> groovyClass = loader.parseClass(content,name+".groovy");
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
GroovyObject proxyObject=groovyObject;
if(pluginList!=null){
int size=pluginList.size();
for(int i=0;i<size;i++){
IGroovyLoadPlugin plugin=(IGroovyLoadPlugin) pluginList.get(i);
proxyObject=plugin.execPlugIn(name, groovyObject, proxyObject);
}
}
GroovyExecUtil.getGroovyMap().put(name, proxyObject);
//GroovyExecUtil.getGroovyMap().put(name, groovyObject);
logger.info("finish load groovy name=" + name);
}
示例5: classLoading
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
@Test
@SuppressWarnings("resource")
public void classLoading() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
GroovyClassLoader gcl = new GroovyClassLoader();
Class<?> class1 = gcl.parseClass("class TestBean { def myMethod() { \"foo\" } }");
Class<?> class2 = gcl.parseClass("class TestBean { def myMethod() { \"bar\" } }");
context.registerBeanDefinition("testBean", new RootBeanDefinition(class1));
Object testBean1 = context.getBean("testBean");
Method method1 = class1.getDeclaredMethod("myMethod", new Class<?>[0]);
Object result1 = ReflectionUtils.invokeMethod(method1, testBean1);
assertEquals("foo", result1);
context.removeBeanDefinition("testBean");
context.registerBeanDefinition("testBean", new RootBeanDefinition(class2));
Object testBean2 = context.getBean("testBean");
Method method2 = class2.getDeclaredMethod("myMethod", new Class<?>[0]);
Object result2 = ReflectionUtils.invokeMethod(method2, testBean2);
assertEquals("bar", result2);
}
示例6: parseClass
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
/**
* 將groovy源碼解析為Class
*/
public static Class parseClass(String groovySource) throws GroovyException {
GroovyClassLoader loader = new GroovyClassLoader();
ClassLoader contextClassLoader = null;
try {
contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) {
Thread.currentThread().setContextClassLoader(null);
}
return loader.parseClass(groovySource);
} catch (Throwable t) {
throw new GroovyException("parseClass error:", t);
} finally {
if (contextClassLoader != null) {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
}
}
示例7: main
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
@SuppressWarnings({ "unchecked", "resource" })
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
String classCode = ""+
"import java.util.Map; "+
"class GroovyServiceImpl2 implements IGroovyService{"+
" @Override"+
" public Object execute(Map<String, Object> param) {"+
" return 1656565;"+
" }"+
"}"+
";";
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
Class<IGroovyService> clazz = groovyClassLoader.parseClass(classCode);
IGroovyService service = clazz.newInstance();
System.out.println(service.execute(null));
}
示例8: test_groovy
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
public void test_groovy() throws Exception {
ClassLoader parent = Thread.currentThread().getContextClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
// A類
Class AClass = loader.parseClass("class A {\n" + //
" int id\n" + //
"}");
// A實例
GroovyObject a = (GroovyObject) AClass.newInstance();
a.setProperty("id", 33);
String textA = JSON.toJSONString(a);
GroovyObject aa = (GroovyObject) JSON.parseObject(textA, AClass);
Assert.assertEquals(a.getProperty("id"), aa.getProperty("id"));
System.out.println(a);
// B類,繼承於A
Class BClass = loader.parseClass("class B extends A {\n" + //
" String name\n" + //
"}");
// B實例
GroovyObject b = (GroovyObject) BClass.newInstance();
b.setProperty("name", "jobs");
String textB = JSON.toJSONString(b);
GroovyObject bb = (GroovyObject) JSON.parseObject(textB, BClass);
Assert.assertEquals(b.getProperty("id"), bb.getProperty("id"));
Assert.assertEquals(b.getProperty("name"), bb.getProperty("name"));
// 序列化失敗
System.out.println(JSON.toJSONString(b, true));
}
示例9: resolveMultifactorProviderViaPredicate
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
@Override
protected Set<Event> resolveMultifactorProviderViaPredicate(final RequestContext context, final RegisteredService service,
final Principal principal,
final Collection<MultifactorAuthenticationProvider> providers) {
try {
if (predicateResource == null || !ResourceUtils.doesResourceExist(predicateResource)) {
LOGGER.debug("No groovy script predicate is defined to decide which multifactor authentication provider should be chosen");
return null;
}
final String script = IOUtils.toString(predicateResource.getInputStream(), StandardCharsets.UTF_8);
final GroovyClassLoader classLoader = new GroovyClassLoader(getClass().getClassLoader(),
new CompilerConfiguration(), true);
final Class<Predicate> predicateClass = classLoader.parseClass(script);
final Object[] args = {service, principal, providers, LOGGER};
final Class[] types = {Object.class, Object.class, Object.class, Object.class};
final Constructor<Predicate> ctor = predicateClass.getDeclaredConstructor(types);
final Predicate<MultifactorAuthenticationProvider> predicate = ctor.newInstance(args);
return resolveEventViaPrincipalAttribute(principal, attributeNames, service, context, providers,
input -> providers.stream()
.filter(predicate)
.findFirst()
.isPresent());
} catch (final Exception e) {
throw Throwables.propagate(e);
}
}
開發者ID:mrluo735,項目名稱:cas-5.1.0,代碼行數:31,代碼來源:PredicatedPrincipalAttributeMultifactorAuthenticationPolicyEventResolver.java
示例10: buildClosure
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
public static Closure<?> buildClosure(String... strings) throws IOException {
Closure<?> closure = null;
// Create a method returning a closure
StringBuilder sb = new StringBuilder("def closure() { { script -> ");
sb.append(StringUtils.join(strings, "\n"));
sb.append(" } }");
// Create an anonymous class for the method
GroovyClassLoader loader = new GroovyClassLoader();
Class<?> groovyClass = loader.parseClass(sb.toString());
try {
// Create an instance of the class
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
// Invoke the object's method and thus obtain the closure
closure = (Closure<?>) groovyObject.invokeMethod("closure", null);
} catch (Throwable e) {
throw new RuntimeException(e);
} finally {
loader.close();
}
return closure;
}
示例11: executeMain
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
protected static void executeMain()
{
String scriptName = WRAPPER_MANAGER.getGroovyScript();
if (scriptName == null)
{
System.out.println("script not found in configuration -> aborting");
System.exit(999);
}
File scriptFile = new File(scriptName);
if (!scriptFile.exists())
{
System.out.println("script not found -> aborting: "
+ scriptFile.getAbsolutePath());
System.exit(999);
}
Object[] mainMethodArgs = WRAPPER_MANAGER.getMainMethodArgs();
try
{
ClassLoader parent = WrapperGroovyMain.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(scriptFile);
GroovyObject script = (GroovyObject) groovyClass.newInstance();
script.invokeMethod("main", mainMethodArgs);
}
catch (Throwable e)
{
e.printStackTrace();
exception = e;
}
}
示例12: loadInstruction
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
/**
* Attempts to parse the Groovy code specified as the parameter and
* instantiates a new {@code InstructionInfo} object. Throws an exception
* if the code cannot be parsed or the parsed {@code Class} object does
* not meet the requirements (methods, annotations).
*
* @param classCode the code of a valid Groovy class
*
* @return an {@code InstructionInfo} object created from the specified Groovy code
*
* @throws CompilationFailedException If the contents of the specified stream
* cannot be parsed into a {@code Class}.
* @throws InvalidInstructionClassException
* If a {@code Class} object can be created but it's not annotated
* properly or does not contain any applicable {@code apply} methods.
* @throws InvalidFormalParameterListException
* If any of the {@code apply} methods has a faulty formal
* parameter list.
*/
public static InstructionInfo loadInstruction(String classCode)
throws CompilationFailedException,
InvalidInstructionClassException,
InvalidFormalParameterListException
{
String actualCode = automaticImportStatement + classCode;
GroovyClassLoader groovyClassLoader
= new GroovyClassLoader(InstructionLoader.class.getClassLoader());
Class<?> instructionClass = groovyClassLoader.parseClass(actualCode);
logger.trace("Class parsed.");
String opcode = checkOpcode(instructionClass)
.orElseThrow(InvalidInstructionClassException::new);
logger.trace("Opcode checked.");
List<String> rules = getUsedPreprocessorRules(instructionClass);
logger.trace("Used rules retrieved.");
List<FormalCall> calls = getFormalCalls(instructionClass);
logger.trace("Calls discovered.");
if (calls.isEmpty()) {
throw new InvalidInstructionClassException(
"Method(s) with name \"apply\" not found.");
}
return new InstructionInfo(opcode, rules, instructionClass, calls);
}
示例13: QuotaHandler
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
public QuotaHandler(Quota config) {
super();
this.config = config;
GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
try {
scriptClass = groovyClassLoader.parseClass(config.getQuotaKeyFunction());
} finally {
try {
groovyClassLoader.close();
} catch (IOException e) {}
}
}
示例14: testCreateScriptWithScriptClass
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
public void testCreateScriptWithScriptClass() {
GroovyClassLoader classLoader = new GroovyClassLoader();
String controlProperty = "text";
String controlValue = "I am a script";
String code = controlProperty + " = '" + controlValue + "'";
GroovyCodeSource codeSource = new GroovyCodeSource(code, "testscript", "/groovy/shell");
Class scriptClass = classLoader.parseClass(codeSource, false);
Script script = InvokerHelper.createScript(scriptClass, new Binding(bindingVariables));
assertEquals(bindingVariables, script.getBinding().getVariables());
script.run();
assertEquals(controlValue, script.getProperty(controlProperty));
}
示例15: _groovy
import groovy.lang.GroovyClassLoader; //導入方法依賴的package包/類
private MethodHandle _groovy(String file, String method, MethodType type, GroovyClassLoader classLoader) {
String filename = "snippets/groovy/" + file + ".groovy";
Class klass = classLoader.parseClass(CodeLoader.class.getResourceAsStream("/" + filename), filename);
try {
return LOOKUP.findStatic(klass, method, type);
} catch (Exception e) {
throw new RuntimeException(e);
}
}