Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7968925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:09:16+00:00 2026-06-04T07:09:16+00:00

I have sample code as below import org.codehaus.groovy.control.CompilerConfiguration abstract class MyClass extends Script {

  • 0

I have sample code as below

import org.codehaus.groovy.control.CompilerConfiguration

abstract class MyClass extends Script {

    void testMethod(Integer x) {
        println "x = $x"
    }
}

public static void main(String[] args) {
    compilerConfiguration = new CompilerConfiguration();
    compilerConfiguration.setScriptBaseClass("MyClass");
    GroovyShell shell = new GroovyShell(new Binding(), compilerConfiguration);
    shell.evaluate("testMethod 1")
}

When I run this class it prints x = 1
now if I change the "testMethod 1" to "testMethod -1" it fails with

Caught: groovy.lang.MissingPropertyException: No such property: testMethod for class: Script1
groovy.lang.MissingPropertyException: No such property: testMethod for class: Script1
    at Script1.run(Script1.groovy:1)
    at Test.run(Test.groovy:15)

Now I change "testMethod -1" to "testMethod (-1)". It again works and printed x = -1

What I need to understand is why Groovy is asking for the parentheses for negative numbers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:09:17+00:00Added an answer on June 4, 2026 at 7:09 am

    Because without parentheses, it is assuming you are trying to subtract 1 from a property called testMethod (ie: testMethod - 1)

    You need the parentheses to inform the parser that this is a method call rather than a subtraction operation


    Edit

    I came up with a horrible way to get this to work:

    import java.lang.reflect.Method
    import org.codehaus.groovy.control.CompilerConfiguration
    
    abstract class MyClass extends Script {
      private methods = [:]
      
      class MinusableMethod {
        Script declarer
        Method method
        MinusableMethod( Script d, Method m ) {
          this.declarer = d
          this.method = m
        }
        def minus( amount ) {
          method.invoke( declarer, -amount )
        }
      }
    
      public MyClass() {
        super()
        methods = MyClass.getDeclaredMethods().grep {
          it.name != 'propertyMissing' && !it.synthetic
        }.collectEntries {
          [ (it.name): new MinusableMethod( this, it ) ]
        } 
      }
    
      def propertyMissing( String name ) {
        methods[ name ]
      }
    
      void testMethod(Integer x) {
          println "x = $x"
      }
    }
    
    static main( args ) {
      def compilerConfiguration = new CompilerConfiguration();
      compilerConfiguration.setScriptBaseClass( 'MyClass' );
      GroovyShell shell = new GroovyShell(new Binding(), compilerConfiguration);
      shell.evaluate("testMethod - 1")
    }
    

    But this will probably break under other conditions

    In the long run, getting people to write valid scripts is probably the better route to take…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
I have a plot (sample code pasted below) that I am trying to add
I have written grammar for a language (sample code below) //this is a procedure
I have this sample code for async operations (copied from the interwebs) public class
I have the code below ; import cv import time cv.NamedWindow(camera, 1) capture =
I have followed the sample code below to upload a zip file in the
Below is sample code from a Module I have in my Flex app. I
I have the below script to import data from a csv file on my
I have written a very simple WCF service, that worked fine (code below), then
The below code is very simple. I have a jQuery autocomplete bound to an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.