|
Groovy example source code file (AbstractASTTransformUtil.java)
The Groovy AbstractASTTransformUtil.java source code
/*
* Copyright 2008-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.transform;
import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.BinaryExpression;
import org.codehaus.groovy.ast.expr.BooleanExpression;
import org.codehaus.groovy.ast.expr.ClassExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.PropertyExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.ast.stmt.EmptyStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.IfStatement;
import org.codehaus.groovy.ast.stmt.ReturnStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.classgen.Verifier;
import org.codehaus.groovy.syntax.Token;
import org.codehaus.groovy.syntax.Types;
import org.objectweb.asm.Opcodes;
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractASTTransformUtil implements Opcodes {
private static final Token COMPARE_EQUAL = Token.newSymbol(Types.COMPARE_EQUAL, -1, -1);
private static final Token COMPARE_NOT_EQUAL = Token.newSymbol(Types.COMPARE_NOT_EQUAL, -1, -1);
private static final Token INSTANCEOF = Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1);
private static final Token ASSIGN = Token.newSymbol(Types.ASSIGN, -1, -1);
public static boolean hasDeclaredMethod(ClassNode cNode, String name, int argsCount) {
List<MethodNode> ms = cNode.getDeclaredMethods(name);
for (MethodNode m : ms) {
Parameter[] paras = m.getParameters();
if (paras != null && paras.length == argsCount) {
return true;
}
}
return false;
}
public static Statement returnFalseIfWrongType(ClassNode cNode, Expression other) {
return new IfStatement(
notEqualClasses(cNode, other),
new ReturnStatement(ConstantExpression.FALSE),
new EmptyStatement()
);
}
public static Statement returnFalseIfNotInstanceof(ClassNode cNode, Expression other) {
return new IfStatement(
isInstanceof(cNode, other),
new EmptyStatement(),
new ReturnStatement(ConstantExpression.FALSE)
);
}
public static IfStatement returnFalseIfNull(Expression other) {
return new IfStatement(
equalsNullExpr(other),
new ReturnStatement(ConstantExpression.FALSE),
new EmptyStatement()
);
}
public static IfStatement returnTrueIfIdentical(Expression self, Expression other) {
return new IfStatement(
identicalExpr(self, other),
new ReturnStatement(ConstantExpression.TRUE),
new EmptyStatement()
);
}
@Deprecated
public static Statement returnFalseIfPropertyNotEqual(FieldNode fNode, Expression other) {
return returnFalseIfFieldNotEqual(fNode, other);
}
public static Statement returnFalseIfPropertyNotEqual(PropertyNode pNode, Expression other) {
return new IfStatement(
notEqualsPropertyExpr(pNode, other),
new ReturnStatement(ConstantExpression.FALSE),
new EmptyStatement()
);
}
public static Statement returnFalseIfFieldNotEqual(FieldNode fNode, Expression other) {
return new IfStatement(
notEqualsFieldExpr(fNode, other),
new ReturnStatement(ConstantExpression.FALSE),
new EmptyStatement()
);
}
public static List<PropertyNode> getInstanceProperties(ClassNode cNode) {
final List<PropertyNode> result = new ArrayList
Other Groovy examples (source code examples)Here is a short list of links related to this Groovy AbstractASTTransformUtil.java source code file: |
Other websites by Alvin Alexander:
Life/living in Alaska (OneMansAlaska.com)
How I Sold My Business (HowISoldMyBusiness.com)
Copyright 1998-2011 Alvin Alexander, devdaily.com
All Rights Reserved.