Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Fix two traits issues, #46 and call private methods from trait methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chiquitinxx committed Jun 5, 2015
1 parent 13fb4ba commit 416eaa0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/groovy/org/grooscript/convert/Context.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Context {
boolean addClosureSwitchInitialization = false

boolean insideWith = false
String actualTraitMethodName
MethodNode actualTraitMethod

//Prefix and postfix for variables without clear scope
String prefixOperator = '', postfixOperator = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class InnerClassNodeHandler extends TraitBaseHandler {
if (methodNode.name == '$static$init$') {
initStaticTraitFields(methodNode, innerClassNode)
} else if (methodNode.code instanceof BlockStatement) {
context.actualTraitMethodName = methodNode.parameters[0].name
context.actualTraitMethod = methodNode
if (!methodNode.code.isEmpty() || methodNode.name == '$init$') {
functions.processBasicFunction("${className}.${methodNode.name}", methodNode, false)
} else {
if (functions.haveAnnotationNative(methodNode.annotations)) {
functions.putGsNativeMethod("${className}.${methodNode.name}", innerClassNode, methodNode)
}
}
context.actualTraitMethodName = null
context.actualTraitMethod = null
} else {
if (methodNode.name.startsWith('get')) {
out.addScript("${className}.${methodNode.name} = function(\$self) {" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,16 @@ class MethodCallExpressionHandler extends BaseHandler {
}

private addParametersWithoutParenthesis(expression) {
conversionFactory.visitNode(expression.arguments, false)
def arguments = expression.arguments
if (context.actualTraitMethod && arguments?.getExpressions()) {
//Inside a trait method, we remove $self as first argument
def exp = arguments.getExpression(0)
if (exp instanceof VariableExpression && exp.variable == '$self' &&
expression.methodAsString in context.actualTraitMethod.declaringClass.methods.collect { it.name }) {
arguments = new ArgumentListExpression(arguments.getExpressions().tail())
}
}
conversionFactory.visitNode(arguments, false)
}

private addParametersAsList(expression) {
Expand Down Expand Up @@ -240,8 +249,8 @@ class MethodCallExpressionHandler extends BaseHandler {
if (conversionFactory.isThis(expression.objectExpression) && context.staticProcessNode) {
out.addScript(context.staticProcessNode.nameWithoutPackage)
} else {
if (conversionFactory.isThis(expression.objectExpression) && context.actualTraitMethodName) {
out.addScript(context.actualTraitMethodName)
if (conversionFactory.isThis(expression.objectExpression) && context.actualTraitMethod) {
out.addScript(context.actualTraitMethod.parameters[0].name)
} else {
conversionFactory.visitNode(expression.objectExpression)
}
Expand All @@ -259,7 +268,7 @@ class MethodCallExpressionHandler extends BaseHandler {
if (conversionFactory.isThis(expression.objectExpression) && !context.mainContext &&
context.insideClass && !context.currentVariableScopingHasMethod(methodName) &&
!context.staticProcessNode) {
out.addScript(", ${GS_OBJECT}")
out.addScript(", ${context.actualTraitMethod ? '$self' : GS_OBJECT}")
}
out.addScript(')')
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/groovy/org/grooscript/convert/TestTraits.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,9 @@ class TestTraits extends Specification {
expect:
convertAndEvaluate('traits/ObjectProperties')
}

def 'using this in trait functions'() {
expect:
convertAndEvaluate('traits/UsingThis', true)
}
}
64 changes: 64 additions & 0 deletions src/test/resources/traits/UsingThis.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package traits

/**
* Created by jorgefrancoleza on 5/6/15.
*/
trait Equal {

int number = 5

boolean equalProperties() {
def props = this.propertyList()
if (props.size() == 2) {
equalsTwo(*props)
} else if (props.size() == 3) {
this.equalsThree(*props)
} else {
throw new Exception()
}
}

boolean allNumberFive() {
propertyList().every {
it == this.number && it == this.getNumber() && it == number
}
}

private boolean equalsTwo(a, b) {
a == b && b == a
}

private boolean equalsThree(a, b, c) {
this.equalsTwo(a, b) && this.equalsTwo(b, c) && equalsTwo(c, a)
}
}

class MyTwoNumbers implements Equal {
def a = 5
def b = 5
def propertyList() {
[a, b]
}
}

class MyThreeNumbers implements Equal {
def a = 6
def b = 6
def c = 6

def propertyList() {
[a, b, c]
}
}

def myTwoNumbers = new MyTwoNumbers()
assert myTwoNumbers.equalProperties()
assert myTwoNumbers.allNumberFive()
myTwoNumbers.a = 7
assert !myTwoNumbers.equalProperties()
assert !myTwoNumbers.allNumberFive()

def myThreeNumbers = new MyThreeNumbers()
assert myThreeNumbers.equalProperties()
myThreeNumbers.c = 8
assert !myThreeNumbers.equalProperties()

0 comments on commit 416eaa0

Please sign in to comment.