有效的Java代码不是有效的Groovy代码? [英] Valid Java code that is NOT valid Groovy code?
问题描述
在Groovy中,Java中的哪些构造/特性在语法上无效? 请提供具体的Java代码示例(Java 1.6),它不是有效的Groovy代码(Groovy 1.6)。
更新:
到目前为止,我们已经得到了5个无效的Groovy代码的Java代码示例:
- 数组初始化
- 内部类
-
def
是Groovy中的一个关键字,但不包含在Java中 -
$$
- 字符串 - 解析为无效Groovy中的GString
s
- 非静态初始化块
- class Foo {Integer x; {x = 1; }}
这是完整的列表吗?任何进一步的例子?
更新#1:我已经开始了一个赏金来解决这个问题。奖金将授予提供最全面实例列表的人员。到目前为止,我们已经发现了五个例子,但我确信那里有更多的例子。所以让他们来吧!
以下是有效的Java 6项目列表,但无效的Groovy 1.6。这不是一个完整的列表,但我认为它涵盖了大多数情况。
(顺便说一下,我认为您应该注意非静态初始化块DO )
任何内部类声明
包括静态的 / p> public class Outer {
static class Inner {}
}
$ c $
非静Outer {
class Inner {}
}
局部类,
public class Outer {
public static void main(String [] args){$ b $ class class {}
code $
$ b 和匿名类别
java.util.EventListener listener = new java.util.EventListener(){};
使用Groovy关键字作为变量
int def;
int in;
int threadsafe;
int as;
数组初始化
String [] stuff = new String [] {string};
int [] array = {1,2,3};
在字符串中使用美元符号,后面的内容不是有效的表达式
String s =$$;
String s =$ def;
String s =$ enum;
String s =$;;
String s =$ \\;
//等。
for循环中的多个初始化器
for(int i = 0,j = 0; i <5; i ++){}
for循环中的多个增量
int j = 0;
for(int i = 0; i <5; i ++,j ++){}
使用换行符分解一些表达式
int a = 2
/ 2
;
无案件结案开关
b
$ b switch(a){
case 1:
}
$ b 在没有正文的开关中设置默认值
其中默认是在最后
int a = 0;
switch(a){
default:
}
或(b)
开关(a){
默认值:
情况1:
打破;
带列表的注释
@SuppressWarnings({boxing,cast})
本地方法声明
public native int nativeMethod() ;
每个枚举类
pre public $ getSymbol(){return+; }
};
抽象字符串getSymbol();
}
循环
do {
System.out.println(stuff);
} while(true);
Most Java code is also syntactically valid Groovy code. However, there are a few exceptions which leads me to my question:
Which constructs/features in Java are syntactically invalid in Groovy? Please provide concrete examples of Java code (Java 1.6) that is NOT valid Groovy code (Groovy 1.6).
Update:
So far we've got five examples of syntactically valid Java code that is not valid Groovy code:
- Array initializations
- Inner classes
def
is a keyword in Groovy, but not in Java"$$"
-strings - parsed as an invalidGString
s in Groovy- Non-static initialization blocks
-- class Foo { Integer x; { x = 1; } }
Is this the complete list? Any further examples?
Update #1: I've started a bounty to bump this question. The bounty will be granted to the person who provides the most comprehensive list of examples. So far we've uncovered five examples, but I'm sure there a quite some more out there. So keep them coming!
Here is a list of items that are valid Java 6, but not valid Groovy 1.6. This isn't a complete list, but I think it covers most of the cases.
(By the way, I think you should note that non-static initialization blocks DO work in Groovy.)
Any inner class declaration
including static,
public class Outer{
static class Inner{}
}
non-static,
public class Outer{
class Inner{}
}
local classes,
public class Outer{
public static void main(String[] args) {
class Local{}
}
}
and anonymous classes
java.util.EventListener listener=new java.util.EventListener(){};
Using Groovy keywords as variables
int def;
int in;
int threadsafe;
int as;
Array initialization
String[] stuff=new String[]{"string"};
int[] array={1,2,3};
Using dollar signs in strings where what follows isn't a valid expression
String s="$$";
String s="$def";
String s="$enum";
String s="$;";
String s="$\\";
//etc.
More than one initializer in a for loop
for (int i=0, j=0; i < 5; i++) {}
More than one increment in a for loop
int j=0;
for (int i=0; i < 5; i++,j++) {}
Breaking up some expressions using newlines
int a= 2
/ 2
;
Ending switch with a case that has no body
switch(a){
case 1:
}
Having a default in a switch with no body
Applies in both cases where default is at the end
int a=0;
switch(a){
default:
}
or somewhere in the middle
switch(a){
default:
case 1:
break;
}
Annotations with lists
@SuppressWarnings({"boxing","cast"})
Native method declaration
public native int nativeMethod();
Class per enum
public enum JavaEnum{
ADD{
public String getSymbol(){ return "+"; }
};
abstract String getSymbol();
}
Do loop
do{
System.out.println("stuff");
}while(true);
这篇关于有效的Java代码不是有效的Groovy代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!