Skip to content

Commit 636ef87

Browse files
committedJan 27, 2017
GROOVY-8066: protected fields should be disallowed in traits (closes groovy#482)
1 parent 11c3711 commit 636ef87

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
 

‎src/main/org/codehaus/groovy/transform/trait/TraitASTTransformation.java

+6
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ private static boolean methodNeedsReplacement(ClassNode classNode, MethodNode m)
386386

387387

388388
private void processField(final FieldNode field, final MethodNode initializer, final MethodNode staticInitializer, final ClassNode fieldHelper, final ClassNode trait, final Set<String> knownFields) {
389+
if (field.isProtected()) {
390+
unit.addError(new SyntaxException("Cannot have protected field in a trait (" + trait.getName() + "#" + field.getName() + ")",
391+
field.getLineNumber(), field.getColumnNumber()));
392+
return;
393+
}
394+
389395
Expression initialExpression = field.getInitialExpression();
390396
MethodNode selectedMethod = field.isStatic()?staticInitializer:initializer;
391397
if (initialExpression != null && !field.isFinal()) {
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package groovy.bugs
20+
21+
import gls.CompilableTestSupport
22+
23+
class Groovy8066Bug extends CompilableTestSupport {
24+
void testTraitWithProtectedFieldShouldGiveCompileError() {
25+
def message = shouldNotCompile """
26+
trait Foo {
27+
protected String bar
28+
}
29+
class Baz implements Foo { }
30+
"""
31+
assert message.contains('Cannot have protected field in a trait')
32+
}
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.