[jira] [Commented] (GROOVY-9068) GroovyScriptEngine causes Metaspace OOM

2019-04-11 Thread Jingfei Hu (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-9068?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16815914#comment-16815914
 ] 

Jingfei Hu commented on GROOVY-9068:


[~daniel_sun], Thank you very much for providing detailed workarounds. So based 
on your response, I can see
 # You're using GroovyShell instead of GroovyScriptEngine. That's the 
difference and it matters.
 # The method you declare it's not thread safe comes from 
GroovyScriptEngineImpl actually, part of your library with version 2.5.6. So 
according to your statements, GroovyScriptEngineImpl is not thread safe, I 
doubt that. 
 # The following 2 solutions seem to work. However after I did a quick learning 
about this topic here and there, the key point is a compromise between caching 
Class objects parsed from scrip text and consumption of *Metaspace*

Below is the logic behind this, correct me if there is any misunderstanding.
 * If we cache Class objects and never release them, then we would continuously 
consume *Metaspace* and finally meet OOM as the number of different scripts 
increases. In our application, that could occur in the near future. 
 * If we don't cache at all, which means we can meet below prerequisites after 
we complete the evaluation, however it depends on when GC kicks in to reclaim 
Class objects and the corresponding *Metaspace*, it's indeterministic and 
Metaspace could be used up if there is a burst load and before GC actually does 
its work. When we test it in out testbed environment, it confirmed the 
suspicious, it quickly hit OOM merely after it's running one hour or so ** 

h3.  

Prerequisites to unload the Class objects to release Metaspace
 # There are no reachable instances of this Class
 # The corresponding class loader object is GC-ed. So apparently sharing a 
class loader object is against this rule
 # There are no any references to this Class object, for instance there is no 
way to access methods of this Class by reflection

 

So it seems to me that a cache which has a rotation/retiring mechanism function 
is the "ultimate" solution, isn't it? Better ideas and suggestions are welcome!

> GroovyScriptEngine causes Metaspace OOM
> ---
>
> Key: GROOVY-9068
> URL: https://issues.apache.org/jira/browse/GROOVY-9068
> Project: Groovy
>  Issue Type: Bug
>  Components: GroovyScriptEngine
>Affects Versions: 2.4.9
> Environment: macOS Mojave, MacBook Pro (Retina, 15-inch, Mid 2015)
>Reporter: Jingfei Hu
>Priority: Major
>  Labels: GroovyScriptEngineImpl, Metaspace, OOM
>
> Hello team,
> We've encountered this troublesome Metaspace OOM in our application recently 
> as the number of groovy scripts increases. The groovy usage pattern in our 
> application is evaluating scripts adhoc and directly. There are no any kinds 
> of caches. And we use below code to do the evaluation. 
>  
> {code:java}
> engine.eval(scriptText, bindings);
> {code}
> We thought the cache of GroovyScriptEngineImpl which is below field would 
> take effect, but actually not in the multi-threading context
> {code:java}
> // script-string-to-generated Class map
> private ManagedConcurrentValueMap classMap = new 
> ManagedConcurrentValueMap(ReferenceBundle.getSoftBundle());
> {code}
> So without proper cache, our application continuously leverages the groovy 
> class loader of GroovyScriptEngineImpl to parse scripts to generate Class 
> objects and fill up Metaspace eventually. 
>  
> And below code snippets can easily reproduce this.  
>  
> {code:java}
> package com.jingfei;
> import java.util.concurrent.ExecutorService;
> import java.util.concurrent.Executors;
> public class Main {
> static final GroovyScriptExecutor groovyScriptExecutor = new 
> GroovyScriptExecutor();
> public static void main(String[] args) throws InterruptedException {
> ExecutorService executor = Executors.newFixedThreadPool(1);
> while (true)  {
> executor.execute(new Runnable() {
> @Override
> public void run() {
> try {
> groovyScriptExecutor.executeScript("(1..10).sum()");
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> });
> }
> }
> }
> package com.jingfei;
> import java.util.HashMap;
> import java.util.Map;
> import javax.script.CompiledScript;
> import javax.script.ScriptEngine;
> import javax.script.ScriptEngineManager;
> import javax.script.ScriptException;
> import groovy.lang.GroovyClassLoader;
> import org.codehaus.groovy.jsr223.GroovyCompiledScript;
> import org.codehaus.groovy.jsr223.GroovyScriptEngineImpl;
> /**
>  * @author jingfei
>  * @version $Id: GroovyScriptExecutor.java, v 0.1 2019-04-02 20:07 jingfei 
> Exp $$

[jira] [Resolved] (GROOVY-9078) Inefficiency in MethodNode and ConstructorNode

2019-04-11 Thread Paul King (JIRA)


 [ 
https://issues.apache.org/jira/browse/GROOVY-9078?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Paul King resolved GROOVY-9078.
---
   Resolution: Fixed
 Assignee: Paul King
Fix Version/s: 2.4.17
   2.5.7
   3.0.0-beta-1

Code adjusted as suggested. Thanks!

> Inefficiency in MethodNode and ConstructorNode
> --
>
> Key: GROOVY-9078
> URL: https://issues.apache.org/jira/browse/GROOVY-9078
> Project: Groovy
>  Issue Type: Bug
>Reporter: Eric Milles
>Assignee: Paul King
>Priority: Minor
> Fix For: 3.0.0-beta-1, 2.5.7, 2.4.17
>
>
> The constructor of {{MethodNode}} performs {{VariableScope scope = new 
> VariableScope(); setVariableScope(scope);}} followed by 
> {{setParameters(parameters);}}, which in turn creates a variable scope, 
> populates it and sets it.  The first variable scope is superflous and those 2 
> lines could be deleted.
> Also, the constructor of {{ConstructorNode}} calls the super constructor and 
> then creates a variable scope, fills it with param info and sets it.  Since 
> the super constructor (as described above) already calls {{setParameters}}, 
> the extra variable scope created in {{ConstructorNode}} could be removed.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[GitHub] [groovy] asfgit closed pull request #908: Found and removed extra "a"

2019-04-11 Thread GitBox
asfgit closed pull request #908: Found and removed extra "a"
URL: https://github.com/apache/groovy/pull/908
 
 
   


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [groovy] paulk-asert commented on issue #908: Found and removed extra "a"

2019-04-11 Thread GitBox
paulk-asert commented on issue #908: Found and removed extra "a"
URL: https://github.com/apache/groovy/pull/908#issuecomment-482389439
 
 
   Merged. Thanks!


This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[jira] [Commented] (GROOVY-9079) STC: NPE in StaticTypeCheckingVisitor.inferSAMTypeGenericsInAssignment

2019-04-11 Thread Eric Milles (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-9079?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16815787#comment-16815787
 ] 

Eric Milles commented on GROOVY-9079:
-

This can be fixed with a guard condition:
{code:java}
private ClassNode inferSAMTypeGenericsInAssignment(ClassNode samUsage, 
MethodNode sam, ClassNode closureType, ClosureExpression closureExpression) {
// if the sam type or closure type do not provide generics information,
// we cannot infer anything, thus we simply return the provided samUsage
GenericsType[] samGt = samUsage.getGenericsTypes();
GenericsType[] closureGt = closureType.getGenericsTypes();
if (samGt == null || closureGt == null) return samUsage;

// extract the generics from the return type
Map connections = new 
HashMap();
extractGenericsConnections(connections, 
getInferredReturnType(closureExpression), sam.getReturnType());

// GRECLIPSE add
if (closureExpression.isParameterSpecified()) {
// GRECLIPSE end
// next we get the block parameter types and set the generics
// information just like before
// TODO: add vargs handling
Parameter[] closureParams = closureExpression.getParameters();
Parameter[] methodParams = sam.getParameters();
for (int i = 0; i < closureParams.length; i++) {
ClassNode fromClosure = closureParams[i].getType();
ClassNode fromMethod = methodParams[i].getType();
extractGenericsConnections(connections, fromClosure, fromMethod);
}
// GRECLIPSE add
}
// GRECLIPSE end
ClassNode result = applyGenericsContext(connections, 
samUsage.redirect());
return result;
}
{code}

There may also be a latent NPE in the AutoFinal xform, which uses 
ClosureExpression.getParameters() unguarded as well.

> STC: NPE in StaticTypeCheckingVisitor.inferSAMTypeGenericsInAssignment
> --
>
> Key: GROOVY-9079
> URL: https://issues.apache.org/jira/browse/GROOVY-9079
> Project: Groovy
>  Issue Type: Bug
>Reporter: Eric Milles
>Priority: Major
>
> Consider the following:
> {code:groovy}
> @groovy.transform.CompileStatic
> void meth() {
>   java.util.concurrent.Callable call = { -> }
> }
> {code}
> This code produces:
> {code}
> BUG! exception in phase 'instruction selection' in source unit 'Blah.groovy' 
> unexpected NullpointerException
>   at 
> org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1148)
>   at 
> org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:654)
>   at 
> org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:632)
>   at 
> org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:609)
>   ...
> Caused by: java.lang.NullPointerException
>   at 
> org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.inferSAMTypeGenericsInAssignment(StaticTypeCheckingVisitor.java:4282)
>   at 
> org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.getResultType(StaticTypeCheckingVisitor.java:4133)
>   at 
> org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitBinaryExpression(StaticTypeCheckingVisitor.java:831)
>   ...
> {code}
> The method in StaticTypeCheckingVisitor calls 
> ClosureExpression.getParameters(), which returns null for a closure 
> expression like "{ -> }".



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Created] (GROOVY-9079) STC: NPE in StaticTypeCheckingVisitor.inferSAMTypeGenericsInAssignment

2019-04-11 Thread Eric Milles (JIRA)
Eric Milles created GROOVY-9079:
---

 Summary: STC: NPE in 
StaticTypeCheckingVisitor.inferSAMTypeGenericsInAssignment
 Key: GROOVY-9079
 URL: https://issues.apache.org/jira/browse/GROOVY-9079
 Project: Groovy
  Issue Type: Bug
Reporter: Eric Milles


Consider the following:
{code:groovy}
@groovy.transform.CompileStatic
void meth() {
  java.util.concurrent.Callable call = { -> }
}
{code}

This code produces:
{code}
BUG! exception in phase 'instruction selection' in source unit 'Blah.groovy' 
unexpected NullpointerException
at 
org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1148)
at 
org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:654)
at 
org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:632)
at 
org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:609)
...
Caused by: java.lang.NullPointerException
at 
org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.inferSAMTypeGenericsInAssignment(StaticTypeCheckingVisitor.java:4282)
at 
org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.getResultType(StaticTypeCheckingVisitor.java:4133)
at 
org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitBinaryExpression(StaticTypeCheckingVisitor.java:831)
...
{code}


The method in StaticTypeCheckingVisitor calls 
ClosureExpression.getParameters(), which returns null for a closure expression 
like "{ -> }".



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Created] (GROOVY-9078) Inefficiency in MethodNode and ConstructorNode

2019-04-11 Thread Eric Milles (JIRA)
Eric Milles created GROOVY-9078:
---

 Summary: Inefficiency in MethodNode and ConstructorNode
 Key: GROOVY-9078
 URL: https://issues.apache.org/jira/browse/GROOVY-9078
 Project: Groovy
  Issue Type: Bug
Reporter: Eric Milles


The constructor of {{MethodNode}} performs {{VariableScope scope = new 
VariableScope(); setVariableScope(scope);}} followed by 
{{setParameters(parameters);}}, which in turn creates a variable scope, 
populates it and sets it.  The first variable scope is superflous and those 2 
lines could be deleted.

Also, the constructor of {{ConstructorNode}} calls the super constructor and 
then creates a variable scope, fills it with param info and sets it.  Since the 
super constructor (as described above) already calls {{setParameters}}, the 
extra variable scope created in {{ConstructorNode}} could be removed.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Created] (GROOVY-9077) STC: inferred type of property that resolves to method not always stored

2019-04-11 Thread Eric Milles (JIRA)
Eric Milles created GROOVY-9077:
---

 Summary: STC: inferred type of property that resolves to method 
not always stored
 Key: GROOVY-9077
 URL: https://issues.apache.org/jira/browse/GROOVY-9077
 Project: Groovy
  Issue Type: Bug
Affects Versions: 2.5.6
Reporter: Eric Milles


Consider the following:
{code:groovy}
@groovy.transform.CompileStatic
void meth(List list) {
  if (list instanceof LinkedList && list.first) {
  }
  if (list instanceof LinkedList && list.peek()) {
  }
}
{code}

{{storeTargetMethod}} is called for the method call expression {{list.peek()}}. 
 However, when 
{{org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.existsProperty}} 
resolves {{list.first}} to a method, {{storeTargetMethod}} is not called.  This 
means the AST node does not record the direct method call target.  Also, a 
type-checking extension does not have a chance to handle the method selection 
event.


This addition addresses the issue for class getters:
{code:java}
if (readMode && checkGetterOrSetter) {
if (getter != null) {
ClassNode cn = inferReturnTypeGenerics(current, getter, 
ArgumentListExpression.EMPTY_ARGUMENTS);
storeInferredTypeForPropertyExpression(pexp, cn);
// GRECLIPSE add
storeTargetMethod(pexp, getter);
// GRECLIPSE end

pexp.removeNodeMetaData(StaticTypesMarker.READONLY_PROPERTY);
String delegationData = receiver.getData();
if (delegationData != null)

pexp.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, delegationData);
return true;
}
{code}

and this change addresses the issue for extension method getters:

{code:java}
// GROOVY-5568, the property may be defined by DGM
List dgmReceivers = new ArrayList(2);
dgmReceivers.add(testClass);
if (isPrimitiveType(testClass)) 
dgmReceivers.add(getWrapper(testClass));
for (ClassNode dgmReceiver : dgmReceivers) {
List methods = 
findDGMMethodsByNameAndArguments(getTransformLoader(), dgmReceiver, "get" + 
capName, ClassNode.EMPTY_ARRAY);
for (MethodNode m : 
findDGMMethodsByNameAndArguments(getTransformLoader(), dgmReceiver, "is" + 
capName, ClassNode.EMPTY_ARRAY)) {
if (Boolean_TYPE.equals(getWrapper(m.getReturnType( 
methods.add(m);
}
if (!methods.isEmpty()) {
List methodNodes = 
chooseBestMethod(dgmReceiver, methods, ClassNode.EMPTY_ARRAY);
if (methodNodes.size() == 1) {
MethodNode getter = methodNodes.get(0);
if (visitor != null) {
visitor.visitMethod(getter);
}
ClassNode cn = inferReturnTypeGenerics(dgmReceiver, 
getter, ArgumentListExpression.EMPTY_ARGUMENTS);
storeInferredTypeForPropertyExpression(pexp, cn);
// GRECLIPSE add
storeTargetMethod(pexp, getter);
// GRECLIPSE end
return true;
}
}
}
{code}

The selection of a setter method is not as simple, since there may be many 
candidates.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Created] (GROOVY-9076) Debugger Step Into doesn't work in Groovy-compiled classes

2019-04-11 Thread Daniil Ovchinnikov (JIRA)
Daniil Ovchinnikov created GROOVY-9076:
--

 Summary: Debugger Step Into doesn't work in Groovy-compiled classes
 Key: GROOVY-9076
 URL: https://issues.apache.org/jira/browse/GROOVY-9076
 Project: Groovy
  Issue Type: Bug
Affects Versions: 2.5.6
Reporter: Daniil Ovchinnikov


Please see https://github.com/dovchinnikov/debug-groovy-bug




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Commented] (GROOVY-5961) Variable scope not checked properly for AIC in static method

2019-04-11 Thread Eric Milles (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-5961?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16815740#comment-16815740
 ] 

Eric Milles commented on GROOVY-5961:
-

This change in {{org.codehaus.groovy.ast.InnerClassNode}} sets an anon. inner 
class scope to non-static and allows 
{{VariableScopeVisitor.visitConstructorCallExpression}} to find inner class 
members:

{code:java}
public void setVariableScope(VariableScope scope) {
this.scope = scope;
// GRECLIPSE add -- GROOVY-5961
scope.setClassScope(this);
scope.setInStaticContext(false);
// GRECLIPSE end
}
{code}

Or these setters could be called from 
{{VariableScopeVisitor.visitConstructorCallExpression}} which appears to be the 
only call to {{InnerClassNode.setVariableScope}}.

> Variable scope not checked properly for AIC in static method
> 
>
> Key: GROOVY-5961
> URL: https://issues.apache.org/jira/browse/GROOVY-5961
> Project: Groovy
>  Issue Type: Sub-task
>  Components: Compiler
>Affects Versions: 2.1.0
>Reporter: Cédric Champeau
>Priority: Major
>
> The following code will fail:
> {code}
> static void foo() {
> new LinkedList() {
> int index
> Object get(int i) { super.get(index++) }
> }
> }
> {code}
> The error is:
> {noformat}
> Apparent variable 'index' was found in a static scope but doesn't refer to a 
> local variable, static field or class. Possible causes:
> You attempted to reference a variable in the binding or an instance variable 
> from a static context.
> You misspelled a classname or statically imported field. Please check the 
> spelling.
> You attempted to use a method 'index' but left out brackets in a place not 
> allowed by the grammar.
>  at line: 5, column: 39
> {noformat}
> But obviously, {{index}} is not accessed from a static context here.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Commented] (GROOVY-5961) Variable scope not checked properly for AIC in static method

2019-04-11 Thread Eric Milles (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-5961?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16815669#comment-16815669
 ] 

Eric Milles commented on GROOVY-5961:
-

When {{StaticVerifier}} examines the arguments of an AIC constructor call, it 
does this:
{code:java}
@Override
public void visitConstructorCallExpression(ConstructorCallExpression cce) {
boolean oldIsSpecialConstructorCall = inSpecialConstructorCall;
inSpecialConstructorCall = cce.isSpecialCall();
super.visitConstructorCallExpression(cce);
inSpecialConstructorCall = oldIsSpecialConstructorCall;
}
{code}

However, when it visits the AIC class body, it has popped out of the enclosing 
class visit and starts over with the anon. inner:
{code:java}
public void visitClass(ClassNode node, SourceUnit source) {
this.source = source;
super.visitClass(node);
}
{code}

It is possible to set {{inSpecialConstructorCall}} here for anon. inners.  But 
another factor to consider is that the variable expression that is within the 
anon. inner method claims to be from a static scope.  That could be the issue.  
*Should an anon. inner body always be indicated as a non-static scope?*

> Variable scope not checked properly for AIC in static method
> 
>
> Key: GROOVY-5961
> URL: https://issues.apache.org/jira/browse/GROOVY-5961
> Project: Groovy
>  Issue Type: Sub-task
>  Components: Compiler
>Affects Versions: 2.1.0
>Reporter: Cédric Champeau
>Priority: Major
>
> The following code will fail:
> {code}
> static void foo() {
> new LinkedList() {
> int index
> Object get(int i) { super.get(index++) }
> }
> }
> {code}
> The error is:
> {noformat}
> Apparent variable 'index' was found in a static scope but doesn't refer to a 
> local variable, static field or class. Possible causes:
> You attempted to reference a variable in the binding or an instance variable 
> from a static context.
> You misspelled a classname or statically imported field. Please check the 
> spelling.
> You attempted to use a method 'index' but left out brackets in a place not 
> allowed by the grammar.
>  at line: 5, column: 39
> {noformat}
> But obviously, {{index}} is not accessed from a static context here.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Commented] (GROOVY-5961) Variable scope not checked properly for AIC in static method

2019-04-11 Thread Eric Milles (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-5961?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16815515#comment-16815515
 ] 

Eric Milles commented on GROOVY-5961:
-

Same issue exists for anon. inner that access field of super:
{code:groovy}
abstract class A {
  abstract def method(key)
  protected Map map = [:]
}
@CompileStatic
class C {
  static void test() {
def anon = new A() {
  public def method(key) {
map.get(key) // Apparent variable 'map' was found in a static scope but 
doesn't refer to a local variable, static field or class.
  }
}
  }
}
{code}

> Variable scope not checked properly for AIC in static method
> 
>
> Key: GROOVY-5961
> URL: https://issues.apache.org/jira/browse/GROOVY-5961
> Project: Groovy
>  Issue Type: Sub-task
>  Components: Compiler
>Affects Versions: 2.1.0
>Reporter: Cédric Champeau
>Priority: Major
>
> The following code will fail:
> {code}
> static void foo() {
> new LinkedList() {
> int index
> Object get(int i) { super.get(index++) }
> }
> }
> {code}
> The error is:
> {noformat}
> Apparent variable 'index' was found in a static scope but doesn't refer to a 
> local variable, static field or class. Possible causes:
> You attempted to reference a variable in the binding or an instance variable 
> from a static context.
> You misspelled a classname or statically imported field. Please check the 
> spelling.
> You attempted to use a method 'index' but left out brackets in a place not 
> allowed by the grammar.
>  at line: 5, column: 39
> {noformat}
> But obviously, {{index}} is not accessed from a static context here.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Comment Edited] (GROOVY-9068) GroovyScriptEngine causes Metaspace OOM

2019-04-11 Thread Daniel Sun (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-9068?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16815091#comment-16815091
 ] 

Daniel Sun edited comment on GROOVY-9068 at 4/11/19 12:33 PM:
--

{{GroovyCodeSource}} object is not cacheable by default, please try with Groovy 
2.5.6+ :


{code:java}
import groovy.transform.CompileStatic
import org.codehaus.groovy.runtime.InvokerHelper
import org.codehaus.groovy.runtime.memoize.ConcurrentCommonCache
import org.codehaus.groovy.runtime.memoize.EvictableCache

@CompileStatic
class GroovyUtils {
private static EvictableCache SCRIPT_CONTENT_CACHE = new 
ConcurrentCommonCache<>()

static eval(String scriptPath, Map bindingMap = [:]) {
def scriptClass = SCRIPT_CONTENT_CACHE.getAndPut(scriptPath, { key ->
return new 
GroovyClassLoader().parseClass(GroovyUtils.getResourceAsStream(key).getText('UTF-8'))
})

return InvokerHelper.createScript(scriptClass, new 
Binding(bindingMap)).run()
}
}
{code}

P.S. You can use MD5 of script text as cache key.




was (Author: daniel_sun):
{{GroovyCodeSource}} object is not cacheable by default, please try with Groovy 
2.5.6+ :


{code:java}
import groovy.transform.CompileStatic
import org.codehaus.groovy.runtime.InvokerHelper
import org.codehaus.groovy.runtime.memoize.ConcurrentCommonCache
import org.codehaus.groovy.runtime.memoize.EvictableCache

@CompileStatic
class GroovyUtils {
private static EvictableCache SCRIPT_CONTENT_CACHE = new 
ConcurrentCommonCache<>()

static eval(String scriptPath, Map bindingMap = [:]) {
def scriptClass = SCRIPT_CONTENT_CACHE.getAndPut(scriptPath, { key ->
return new 
GroovyClassLoader().parseClass(GroovyUtils.getResourceAsStream(key).getText('UTF-8'))
})

return InvokerHelper.createScript(scriptClass, new 
Binding(bindingMap)).run()
}
}
{code}


> GroovyScriptEngine causes Metaspace OOM
> ---
>
> Key: GROOVY-9068
> URL: https://issues.apache.org/jira/browse/GROOVY-9068
> Project: Groovy
>  Issue Type: Bug
>  Components: GroovyScriptEngine
>Affects Versions: 2.4.9
> Environment: macOS Mojave, MacBook Pro (Retina, 15-inch, Mid 2015)
>Reporter: Jingfei Hu
>Priority: Major
>  Labels: GroovyScriptEngineImpl, Metaspace, OOM
>
> Hello team,
> We've encountered this troublesome Metaspace OOM in our application recently 
> as the number of groovy scripts increases. The groovy usage pattern in our 
> application is evaluating scripts adhoc and directly. There are no any kinds 
> of caches. And we use below code to do the evaluation. 
>  
> {code:java}
> engine.eval(scriptText, bindings);
> {code}
> We thought the cache of GroovyScriptEngineImpl which is below field would 
> take effect, but actually not in the multi-threading context
> {code:java}
> // script-string-to-generated Class map
> private ManagedConcurrentValueMap classMap = new 
> ManagedConcurrentValueMap(ReferenceBundle.getSoftBundle());
> {code}
> So without proper cache, our application continuously leverages the groovy 
> class loader of GroovyScriptEngineImpl to parse scripts to generate Class 
> objects and fill up Metaspace eventually. 
>  
> And below code snippets can easily reproduce this.  
>  
> {code:java}
> package com.jingfei;
> import java.util.concurrent.ExecutorService;
> import java.util.concurrent.Executors;
> public class Main {
> static final GroovyScriptExecutor groovyScriptExecutor = new 
> GroovyScriptExecutor();
> public static void main(String[] args) throws InterruptedException {
> ExecutorService executor = Executors.newFixedThreadPool(1);
> while (true)  {
> executor.execute(new Runnable() {
> @Override
> public void run() {
> try {
> groovyScriptExecutor.executeScript("(1..10).sum()");
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> });
> }
> }
> }
> package com.jingfei;
> import java.util.HashMap;
> import java.util.Map;
> import javax.script.CompiledScript;
> import javax.script.ScriptEngine;
> import javax.script.ScriptEngineManager;
> import javax.script.ScriptException;
> import groovy.lang.GroovyClassLoader;
> import org.codehaus.groovy.jsr223.GroovyCompiledScript;
> import org.codehaus.groovy.jsr223.GroovyScriptEngineImpl;
> /**
>  * @author jingfei
>  * @version $Id: GroovyScriptExecutor.java, v 0.1 2019-04-02 20:07 jingfei 
> Exp $$
>  */
> public class GroovyScriptExecutor {
> /**  Script Engine Manager */
> private static final ScriptEngineManager factory = new 
> ScriptEngineManager();
> /**  Script engine */
> private static final 

[jira] [Updated] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong

2019-04-11 Thread Xiaoguang Wang (JIRA)


 [ 
https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xiaoguang Wang updated GROOVY-9075:
---
Affects Version/s: 2.4.16

> The exception message should be more clear when a GroovyObject's metaClass is 
> wrong
> ---
>
> Key: GROOVY-9075
> URL: https://issues.apache.org/jira/browse/GROOVY-9075
> Project: Groovy
>  Issue Type: Bug
>  Components: groovy-runtime
>Affects Versions: 2.4.16, 3.0.0-alpha-4, 2.5.6
>Reporter: Xiaoguang Wang
>Priority: Major
>
> If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
> instance may cause an exception with unclear message.
>  
> We have met the problem too many times and have spent a lot of time on 
> debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)
>  
> If there is a try-catch (or some other check) in the runtime code and report 
> this problem more clearly, it will save everyone's time to debug. Or, is 
> there any better solution to this problem, eg: check the class type in 
> `MetaClassImpl.setProperty`, `DefaultGroovyMethods.setMetaClass` ?
>  
> {code:java}
> import groovy.transform.CompileStatic
> //@CompileStatic
> class C1 {
> int x
> }
> //@CompileStatic
> class C2 {
> int x
> }
> //@CompileStatic
> class TestGroovy {
> static void main(String[] args) {
> def c1 = new C1()
> def c2 = new C2()
> c1.metaClass = c2.metaClass  //eg: 
> org.springframework.beans.BeanUtils.copyProperties
> // or c1.setMetaClass(c2.getMetaClass())
> c1.x += 1  // crash here with unclear exception, but c1.setX() works
> }
> }
> {code}
>  
> {code:java}
> /*
> without @CompileStatic
> Exception in thread "main" java.lang.IllegalArgumentException: object is not 
> an instance of declaring class
>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>at java.lang.reflect.Method.invoke(Method.java:498)
>at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
>at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
>at 
> org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
>at 
> org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
>at 
> org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)
> with @CompileStatic
> Exception in thread "main" java.lang.IllegalArgumentException: object is not 
> an instance of declaring class
>at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>at java.lang.reflect.Method.invoke(Method.java:498)
>at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
>at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
>at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
>at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
>at C1.setProperty(TestGroovy.groovy)
>at 
> org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
>at 
> org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
>  */
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Updated] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong

2019-04-11 Thread Xiaoguang Wang (JIRA)


 [ 
https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xiaoguang Wang updated GROOVY-9075:
---
Description: 
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug. Or, is there 
any better solution to this problem, eg: check the class type in 
`MetaClassImpl.setProperty`, `DefaultGroovyMethods.setMetaClass` ?

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass  //eg: 
org.springframework.beans.BeanUtils.copyProperties
// or c1.setMetaClass(c2.getMetaClass())

c1.x += 1  // crash here with unclear exception, but c1.setX() works
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
   at C1.setProperty(TestGroovy.groovy)
   at 
org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
   at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
 */
{code}

  was:
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug. Or, is there 
any better solution to this problem, eg: check the class type in 
`MetaClassImpl.setProperty`, `DefaultGroovyMethods.setMetaClass` ?

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass  //eg: 
org.springframework.beans.BeanUtils.copyProperties
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 

[jira] [Updated] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong

2019-04-11 Thread Xiaoguang Wang (JIRA)


 [ 
https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xiaoguang Wang updated GROOVY-9075:
---
Description: 
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug. Or, is there 
any better solution to this problem, eg: check the class type in 
`MetaClassImpl.setProperty`, `DefaultGroovyMethods.setMetaClass` ?

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass  //eg: 
org.springframework.beans.BeanUtils.copyProperties
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
   at C1.setProperty(TestGroovy.groovy)
   at 
org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
   at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
 */
{code}

  was:
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug. Or, is there 
any better solution to this problem, eg: check the class type in 
`MetaClassImpl.setProperty` ?

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass  //eg: 
org.springframework.beans.BeanUtils.copyProperties
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 

[jira] [Updated] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong

2019-04-11 Thread Xiaoguang Wang (JIRA)


 [ 
https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xiaoguang Wang updated GROOVY-9075:
---
Description: 
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug. Or, is there 
any better solution to this problem, eg: check the class type in 
`MetaClassImpl.setProperty` ?

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass  //eg: 
org.springframework.beans.BeanUtils.copyProperties
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
   at C1.setProperty(TestGroovy.groovy)
   at 
org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
   at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
 */
{code}

  was:
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug. Or, is there 
any better solution to this problem?

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass  //eg: 
org.springframework.beans.BeanUtils.copyProperties
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in 

[jira] [Updated] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong

2019-04-11 Thread Xiaoguang Wang (JIRA)


 [ 
https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xiaoguang Wang updated GROOVY-9075:
---
Description: 
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug. Or, is there 
any better solution to this problem?

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass  //eg: 
org.springframework.beans.BeanUtils.copyProperties
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
   at C1.setProperty(TestGroovy.groovy)
   at 
org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
   at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
 */
{code}

  was:
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug.

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass  //eg: 
org.springframework.beans.BeanUtils.copyProperties
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at 

[jira] [Updated] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong

2019-04-11 Thread Xiaoguang Wang (JIRA)


 [ 
https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xiaoguang Wang updated GROOVY-9075:
---
Description: 
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug.

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass  //eg: 
org.springframework.beans.BeanUtils.copyProperties
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
   at C1.setProperty(TestGroovy.groovy)
   at 
org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
   at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
 */
{code}

  was:
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug.

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 

[jira] [Updated] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong

2019-04-11 Thread Xiaoguang Wang (JIRA)


 [ 
https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xiaoguang Wang updated GROOVY-9075:
---
Description: 
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch (or some other check) in the runtime code and report 
this problem more clearly, it will save everyone's time to debug.

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
   at C1.setProperty(TestGroovy.groovy)
   at 
org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
   at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
 */
{code}

  was:
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch in the runtime code and report this problem more 
clearly, it will save everyone's time to debug.

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 

[jira] [Created] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong

2019-04-11 Thread Xiaoguang Wang (JIRA)
Xiaoguang Wang created GROOVY-9075:
--

 Summary: The exception message should be more clear when a 
GroovyObject's metaClass is wrong
 Key: GROOVY-9075
 URL: https://issues.apache.org/jira/browse/GROOVY-9075
 Project: Groovy
  Issue Type: Bug
  Components: groovy-runtime
Affects Versions: 2.5.6, 3.0.0-alpha-4
Reporter: Xiaoguang Wang


If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch in the runtime code and report this problem more 
clearly, it will save everyone's time to debug.

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass
c1.x += 1  // crash here with unclear exception
}
}

/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
   at C1.setProperty(TestGroovy.groovy)
   at 
org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
   at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
 */

{code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)


[jira] [Updated] (GROOVY-9075) The exception message should be more clear when a GroovyObject's metaClass is wrong

2019-04-11 Thread Xiaoguang Wang (JIRA)


 [ 
https://issues.apache.org/jira/browse/GROOVY-9075?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Xiaoguang Wang updated GROOVY-9075:
---
Description: 
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch in the runtime code and report this problem more 
clearly, it will save everyone's time to debug.

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass
c1.x += 1  // crash here with unclear exception
}
}

{code}
 
{code:java}
/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:2726)
   at groovy.lang.MetaClassImpl.setProperty(MetaClassImpl.java:3785)
   at C1.setProperty(TestGroovy.groovy)
   at 
org.codehaus.groovy.runtime.InvokerHelper.setProperty(InvokerHelper.java:213)
   at 
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.setProperty(ScriptBytecodeAdapter.java:497)
 */
{code}

  was:
If a GroovyObject's metaClass is overwritten by mistake, the accesses to the 
instance may cause an exception with unclear message.

 

We have met the problem too many times and have spent a lot of time on 
debugging (and teaching everyone never to use `BeanUtils.copyProperties`, etc)

 

If there is a try-catch in the runtime code and report this problem more 
clearly, it will save everyone's time to debug.

 
{code:java}
import groovy.transform.CompileStatic

//@CompileStatic
class C1 {
int x
}
//@CompileStatic
class C2 {
int x
}

//@CompileStatic
class TestGroovy {
static void main(String[] args) {
def c1 = new C1()
def c2 = new C2()
c1.metaClass = c2.metaClass
c1.x += 1  // crash here with unclear exception
}
}

/*
without @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
   at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
   at 
org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
   at 
org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:85)
   at 
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:299)

with @CompileStatic
Exception in thread "main" java.lang.IllegalArgumentException: object is not an 
instance of declaring class
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at 

[jira] [Commented] (GROOVY-9068) GroovyScriptEngine causes Metaspace OOM

2019-04-11 Thread Daniel Sun (JIRA)


[ 
https://issues.apache.org/jira/browse/GROOVY-9068?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16815103#comment-16815103
 ] 

Daniel Sun commented on GROOVY-9068:


The following code should work with Groovy 2.5.6+ too:
{code:java}
private static final GroovyClassLoader GCL = new GroovyClassLoader();
public static Object evaluate(String scriptText) {
GroovyCodeSource gcs = AccessController.doPrivileged(new 
PrivilegedAction() {
public GroovyCodeSource run() {
return new GroovyCodeSource(scriptText, "script" + 
System.currentTimeMillis() +
Math.abs(scriptText.hashCode()) + ".groovy", 
"/groovy/script");
}
});
gcs.setCachable(true); // the key part
return GCL.parseClass(gcs);
}
{code}


> GroovyScriptEngine causes Metaspace OOM
> ---
>
> Key: GROOVY-9068
> URL: https://issues.apache.org/jira/browse/GROOVY-9068
> Project: Groovy
>  Issue Type: Bug
>  Components: GroovyScriptEngine
>Affects Versions: 2.4.9
> Environment: macOS Mojave, MacBook Pro (Retina, 15-inch, Mid 2015)
>Reporter: Jingfei Hu
>Priority: Major
>  Labels: GroovyScriptEngineImpl, Metaspace, OOM
>
> Hello team,
> We've encountered this troublesome Metaspace OOM in our application recently 
> as the number of groovy scripts increases. The groovy usage pattern in our 
> application is evaluating scripts adhoc and directly. There are no any kinds 
> of caches. And we use below code to do the evaluation. 
>  
> {code:java}
> engine.eval(scriptText, bindings);
> {code}
> We thought the cache of GroovyScriptEngineImpl which is below field would 
> take effect, but actually not in the multi-threading context
> {code:java}
> // script-string-to-generated Class map
> private ManagedConcurrentValueMap classMap = new 
> ManagedConcurrentValueMap(ReferenceBundle.getSoftBundle());
> {code}
> So without proper cache, our application continuously leverages the groovy 
> class loader of GroovyScriptEngineImpl to parse scripts to generate Class 
> objects and fill up Metaspace eventually. 
>  
> And below code snippets can easily reproduce this.  
>  
> {code:java}
> package com.jingfei;
> import java.util.concurrent.ExecutorService;
> import java.util.concurrent.Executors;
> public class Main {
> static final GroovyScriptExecutor groovyScriptExecutor = new 
> GroovyScriptExecutor();
> public static void main(String[] args) throws InterruptedException {
> ExecutorService executor = Executors.newFixedThreadPool(1);
> while (true)  {
> executor.execute(new Runnable() {
> @Override
> public void run() {
> try {
> groovyScriptExecutor.executeScript("(1..10).sum()");
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
> });
> }
> }
> }
> package com.jingfei;
> import java.util.HashMap;
> import java.util.Map;
> import javax.script.CompiledScript;
> import javax.script.ScriptEngine;
> import javax.script.ScriptEngineManager;
> import javax.script.ScriptException;
> import groovy.lang.GroovyClassLoader;
> import org.codehaus.groovy.jsr223.GroovyCompiledScript;
> import org.codehaus.groovy.jsr223.GroovyScriptEngineImpl;
> /**
>  * @author jingfei
>  * @version $Id: GroovyScriptExecutor.java, v 0.1 2019-04-02 20:07 jingfei 
> Exp $$
>  */
> public class GroovyScriptExecutor {
> /**  Script Engine Manager */
> private static final ScriptEngineManager factory = new 
> ScriptEngineManager();
> /**  Script engine */
> private static final ScriptEngine engine  = 
> factory.getEngineByName("groovy");
> public void executeScript(final String scriptText) throws ScriptException 
> {
> System.out.println(engine.eval(scriptText));
> }
> }{code}
> Looking into the Metaspace dump, we find out within the Metaspace there are 
> hundreds of class loader objects named 
> *groovy/lang/GroovyClassLoader$InnerLoader* and all of Class meta info loaded 
> by them with the naming pattern *Script.*
>  
> Since the script is all the same, i.e. 
> {code:java}
> (1..10).sum(){code}
> , this behavior seems totally odd to me, because I thought due to the 
> existence of the cache, there would be only one class loader created 
> necessary to parse the script. 
>  
> Any help is appreciated to work out this problem! Thanks!



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)