keeskuip
|
Posted: December 05, 2010 18:51 by keeskuip
|
|
I know I can do this with 'jackpot' but can this also be done with refactorNG? I have: logger.debug("Log me"); and I want: if(logger.isDebugEnabled()) { logger.debug("Log me"); } The "logger.isDebugEnabled" should only be added if it is not already there. The "logger" can be any logger but it has to be of type "org.log4j.Logger" |
How to do this?
Replies: 3 - Last Post: December 21, 2010 15:54
by: DenisStepanov
by: DenisStepanov
« Older Topic
» Newer Topic
showing 1 - 4 of 4
DenisStepanov
|
Posted: December 06, 2010 12:56 by DenisStepanov
|
You need to have two refactoring rules, first rule will add IF:
ExpressionStatement [id: logCall] {
MethodInvocation {
List<Tree> { },
MemberSelect [identifier: "debug"]{
Identifier [id: log, instanceof: "org.apache.log4j.Logger"]
},
List<Expression> { Literal [kind: STRING_LITERAL] }
}
}
->
If {
Parenthesized {
MethodInvocation {
List<Tree> { },
MemberSelect [identifier: "isDebugEnabled"]{
Identifier [ref: log]
},
List<Expression> { }
}
},
Block {
List<Statement> {
ExpressionStatement [ref: logCall],
}
},
null
}
and second will remove double IF:
If {
Parenthesized {
MethodInvocation {
List<Tree> { },
MemberSelect [identifier: "isDebugEnabled"] {
Identifier [instanceof: "org.apache.log4j.Logger"]
},
List<Expression> { }
}
},
Block {
List<Statement> {
If [id: oneIf] {
Parenthesized {
MethodInvocation {
List<Tree> { },
MemberSelect [identifier: "isDebugEnabled"] {
Identifier [instanceof: "org.apache.log4j.Logger"]
},
List<Expression> { }
}
},
Block {
List<Statement> [maxSize: *] { }
},
null
}
}
},
null
} ->
If [ref: oneIf]
|
keeskuip
|
Posted: December 20, 2010 20:30 by keeskuip
|
|
hmm, I cannot get the first rule to work. (it finds 0 occurences) The actual code is not 'logger.debug("haha")' but 'Log.logger.debug("haha")' My code has the following structure:
ExpressionStatement {
MethodInvocation {
List<Tree> {
},
MemberSelect [identifier: "debug"] {
MemberSelect [identifier: "exception"] {
Identifier [elementKind: CLASS, name: "Log", qualifiedName: "gti.util.log.Log"]
}
},
List<Expression> {
Literal [kind: STRING_LITERAL, value: "haha"]
}
}
},
The sourcecode of above structure is: Log.exception.debug("haha"); (The "debug.exeception" is a instanceof org.apache.log4j.Logger) My questions are:
Can you help me (again) with this? |
DenisStepanov
|
Posted: December 21, 2010 15:54 by DenisStepanov
|
|
You can keep identifier attribute empty or have two rule for exception and form fields try this:
ExpressionStatement {
MethodInvocation {
List<Tree> {
},
MemberSelect [identifier: "debug"] {
MemberSelect {
Identifier [id: log]
}
},
List<Expression> {
Literal [kind: STRING_LITERAL, value: "haha"]
}
}
}
->
ExpressionStatement {
MethodInvocation {
List<Tree> {
},
MemberSelect [identifier: "debug"] {
MemberSelect [identifier: "defaultLog"] {
Identifier [ref:log]
}
},
List<Expression> {
Literal [kind: STRING_LITERAL, value: "haha"]
}
}
}
|
Replies: 3 - Last Post: December 21, 2010 15:54
by: DenisStepanov
by: DenisStepanov
« Older Topic
» Newer Topic






