groovy

Groovy - Ignore extra attributes in a map during object instantiation

岁酱吖の 提交于 2020-03-18 03:18:38
问题 Is there a way to make groovy ignore extra attributes in a map during object instantiation? Example: class Banana{ String name } def params = [name:'someGuy', age:13] new Banana(params) In this example, groovy throws a No such property: age exception (obviously because age isn't defined in the Banana class. Without resorting to manually mapping only the desired attributes from the map to the constructor of the Banana class, is there a way to tell Banana to ignore the extra attributes? I

How do I use String interpolation in a Groovy multiline string?

笑着哭i 提交于 2020-03-18 03:09:12
问题 In Groovy, I have a multiline String, defined with ''' , in which I need to use interpolation in order to substitute some other variables. For all my efforts, I can't get it to work -- I assume I need to escape something, which I'm missing. Here's some sample code: def cretanFood = "Dakos" def mexicanFood = "Tacos" def bestRestaurant = ''' ${mexicanFood} & ${cretanFood} ''' print bestRestaurant At the moment, this outputs: ${mexicanFood} & ${cretanFood} while I would clearly expect: Tacos &

二、Groovy语法(二):闭包

久未见 提交于 2020-03-12 08:56:50
1、Groovy中闭包基础 1.1 闭包的概念 闭包是被包装成 对象 的代码块,可以通过一个变量引用到它,页可以传递给别人进行处理(像处理一个对象一样处理闭包,比如作为参数传递、作为一个方法的返回值等) 1.2 闭包的定义和调用 //定义一个闭包(闭包是一些代码组成的代码块对象,用{}括起来的一段代码) def closure = { println 'Hello groovy!'} //调用(类似定义了一个方法,然后可以去调用这个方法,可与方法对比着来理解闭包) closure.call() //Hello groovy! closure() //Hello groovy! 1.3闭包参数(普通参数和隐式参数) //定义一个有参数的闭包 利用->区分参数和具体执行代码 def closure = { String name -> println "Hello $name!"} //调用 closure('groovy') //Hello groovy! //多个参数由逗号隔开 def closure2 = { String name,int age -> println "Hello $name! My age is $age"} //调用 closure2('groovy',6) //Hello groovy! My age is 6 //当没有声明参数时

Windows JDK配置

核能气质少年 提交于 2020-03-11 17:57:59
JDK 11+ 无需CLASSPATH JAVA_HOME: 安装路径 PATH中添加:%JAVA_HOME%\bin 一般过程: 新建系统变量:JAVA_HOME 变量值:JDK安装路径 在Path中添加变量: %JAVA_HOME%\bin 非必须操作(JDK6+): 新建系统变量:CLASSPATH 变量值:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; 说明: classpath 不是必须配置的。 jdk6以后的版本都不用再配CLASSPATH,而且也不建议去配。 理论上java安装完一个变量都不需要配置,只不过为了命令行敲起来方便,所以通常会把jdk/bin目录下加入到path变量中。JAVA_HOME这个变量的作用是一些基于java开发的工具会用到,比如tomcat,groovy,vertx.....,如果不用这个工具这个变量也可以免了。 不过通常为了方便以后用java开发的小工具,一般都会设置JAVA_HOME,然后把$JAVA_HOME/bin追加到PATH中。 来源: oschina 链接: https://my.oschina.net/u/4002376/blog/3191924

从Java到Groovy的八级进化论

假如想象 提交于 2020-03-11 09:42:32
Groovy和Java确实是亲戚关系,它们的语法非常相似,因此对于Java开发人员来说,Groovy非常容易学习。相似之处在于,大多数Java程序甚至都是有效的Groovy程序(把文件后缀 .java 改成 .groovy 即可)。 我将从一个基础 Hello World 程序开始,分享一下Java如何演化成Groovy的Demo。 原始的 Hello World public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println(helloWorld.greet()); } } 这个 Hello World 类具有一个私有字段,该私有字段表示名称及其相关的 getter() 和 setter() 。还有一个

更改Java中的导入名称,或导入两个具有相同名称的类

那年仲夏 提交于 2020-03-07 16:59:56
在Python中,您可以执行以下操作: from a import b as c 您将如何在Java中执行此操作,因为我有两个冲突的导入。 #1楼 Java中没有导入别名机制。 您不能导入两个具有相同名称的类,并且不能同时使用它们。 导入一个类,并为另一个类使用完全限定名称,即 import com.text.Formatter; private Formatter textFormatter; private com.json.Formatter jsonFormatter; #2楼 Java不允许您这样做。 您需要通过其全限定名来引用其中一个类,而仅导入另一个。 #3楼 值得注意的是 Groovy具有此功能 : import java.util.Calendar import com.example.Calendar as MyCalendar MyCalendar myCalendar = new MyCalendar() #4楼 正如其他答案所述,Java不提供此功能。 已多次要求实现此功能,例如,作为 JDK-4194542:类名别名 或 JDK-4214789:扩展导入以允许重命名导入的类型 。 从评论: 这不是一个不合理的请求,尽管不是很必要。 偶尔使用全限定名称并不是一个不适当的负担(除非库确实左右重复使用相同的简单名称,这是不好的样式)。 无论如何

How to execute shell command in Groovy and get the return code $?

戏子无情 提交于 2020-03-05 02:51:07
问题 I can't get the return code (not the output or error) from executing a shell script within Groovy. For all what I tried, it either ask me to escape or just print the $? instead of give me 1 or 0. groovy: 75: illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 75, column 24. Below are solutions I tried, all don't work. println "../src/check_job_log.s ${it}.log".execute().text println "Check log ${it}

How to build index for already present property?

删除回忆录丶 提交于 2020-03-05 00:32:07
问题 When I am updating my schema, I created a new property(say A) but I forgot to build index. Without knowing that, I have tried to add the value for that property resulted in :> g.V().hasLabel('AA').has('active',true).property('A','ok').count() ** Could not find a suitable index to answer graph query and graph scans are disabled: [(~label = user)]:VERTEX** :> g.V().hasLabel('AA').has('active',true).has('A','ok').valueMap() But when I do valueMap by traversing it with other indexed property then

How to build index for already present property?

前提是你 提交于 2020-03-05 00:30:51
问题 When I am updating my schema, I created a new property(say A) but I forgot to build index. Without knowing that, I have tried to add the value for that property resulted in :> g.V().hasLabel('AA').has('active',true).property('A','ok').count() ** Could not find a suitable index to answer graph query and graph scans are disabled: [(~label = user)]:VERTEX** :> g.V().hasLabel('AA').has('active',true).has('A','ok').valueMap() But when I do valueMap by traversing it with other indexed property then

How to get a value of a dynamic key in Groovy JSONSlurper?

只愿长相守 提交于 2020-03-04 18:03:13
问题 The variable resp contains below JSON response - {"name":"sample","address":{"country":"IN","state":"TN","city":"Chennai"}} I have planned using param1 variable to get the required key from JSON response, but I'm unable to get my expected results. I'm passing the param1 field like - address.state def actValToGet(param1){ JsonSlurper slurper = new JsonSlurper(); def values = slurper.parseText(resp) return values.param1 //values.address.state } I'm getting NULL value here -> values.param1 Can