use of org.codehaus.groovy.ast.stmt.ReturnStatement in project hale by halestudio.
the class VertexEntityTransformation method buildCreateMethod.
/**
* Create a static method that allows creating a new vertex and adding it to
* a graph.
*
* @param clazz the entity class
* @param entityName the entity name
* @param initialExpressions the initial expressions per property
* @return the static create method taking a graph as an argument
*/
private MethodNode buildCreateMethod(ClassNode clazz, Expression entityName, Map<String, Expression> initialExpressions) {
clazz = ClassHelper.make(clazz.getName());
BlockStatement code = new BlockStatement();
// graph (parameter)
VariableExpression graph = new VariableExpression("graph");
// initialize graph
code.addStatement(callInitGraph(clazz, graph));
// vertex (local variable)
VariableExpression vertex = new VariableExpression("vertex");
// id (local variable)
VariableExpression id = new VariableExpression("id");
code.addStatement(AbstractASTTransformUtil.declStatement(id, new ConstantExpression(null)));
// id = OrientGraph.CLASS_PREFIX + entityName
Statement orientBlock = AbstractASTTransformUtil.assignStatement(id, new BinaryExpression(new ConstantExpression(OrientGraph.CLASS_PREFIX), TOKEN_PLUS, entityName));
// > if (graph instanceof OrientGraph) {
// > id = OrientGraph.CLASS_PREFIX + entityName
// > }
code.addStatement(new IfStatement(AbstractASTTransformUtil.isInstanceOf(graph, ORIENT_GRAPH_CLASS), orientBlock, new EmptyStatement()));
// > vertex = graph.addVertex(id)
Statement assignVertex = AbstractASTTransformUtil.declStatement(vertex, new MethodCallExpression(graph, new ConstantExpression("addVertex"), new ArgumentListExpression(id)));
code.addStatement(assignVertex);
// set initial values on vertex
for (Entry<String, Expression> propertyInitial : initialExpressions.entrySet()) {
// > vertex.setProperty(name, initialValue)
code.addStatement(new ExpressionStatement(new MethodCallExpression(vertex, "setProperty", new ArgumentListExpression(new ConstantExpression(propertyInitial.getKey()), propertyInitial.getValue()))));
}
// > return new Entity(vertex, graph)
code.addStatement(new ReturnStatement(new ConstructorCallExpression(clazz, new ArgumentListExpression(vertex, graph))));
return new MethodNode("create", Modifier.STATIC | Modifier.PUBLIC, clazz, new Parameter[] { new Parameter(GRAPH_CLASS, "graph") }, new ClassNode[0], code);
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class OptimizingStatementWriter method writeDeclarationExtraction.
private boolean writeDeclarationExtraction(final Statement statement) {
Expression ex = null;
if (statement instanceof ReturnStatement) {
ReturnStatement rs = (ReturnStatement) statement;
ex = rs.getExpression();
} else if (statement instanceof ExpressionStatement) {
ExpressionStatement es = (ExpressionStatement) statement;
ex = es.getExpression();
} else {
throw new GroovyBugError("unknown statement type :" + statement.getClass());
}
if (!(ex instanceof DeclarationExpression))
return true;
DeclarationExpression declaration = (DeclarationExpression) ex;
ex = declaration.getLeftExpression();
if (ex instanceof TupleExpression)
return false;
// stash declared variable in case we do subsequent visits after we
// change to assignment only
StatementMeta meta = statement.getNodeMetaData(StatementMeta.class);
if (meta != null) {
meta.declaredVariableExpression = declaration.getVariableExpression();
}
// change statement to do assignment only
BinaryExpression assignment = new BinaryExpression(declaration.getLeftExpression(), declaration.getOperation(), declaration.getRightExpression());
assignment.setSourcePosition(declaration);
assignment.copyNodeMetaData(declaration);
// replace statement code
if (statement instanceof ReturnStatement) {
ReturnStatement rs = (ReturnStatement) statement;
rs.setExpression(assignment);
} else if (statement instanceof ExpressionStatement) {
ExpressionStatement es = (ExpressionStatement) statement;
es.setExpression(assignment);
} else {
throw new GroovyBugError("unknown statement type :" + statement.getClass());
}
return true;
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class StaticTypeCheckingVisitor method isTypeSource.
private static boolean isTypeSource(final Expression expr, final MethodNode mNode) {
boolean[] returned = new boolean[1];
mNode.getCode().visit(new CodeVisitorSupport() {
@Override
public void visitReturnStatement(final ReturnStatement returnStatement) {
if (isTypeSource(expr, returnStatement.getExpression())) {
returned[0] = true;
}
}
@Override
public void visitClosureExpression(final ClosureExpression expression) {
}
});
if (!returned[0]) {
new ReturnAdder(returnStatement -> {
if (isTypeSource(expr, returnStatement.getExpression())) {
returned[0] = true;
}
}).visitMethod(mNode);
}
return returned[0];
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class RecordCompletionASTTransformation method adjustCopyWith.
// when the record classnode was processed, the tuple constructor hadn't been added yet, so manually adjust here
private void adjustCopyWith(ClassNode cNode, MethodNode copyWith) {
final List<Parameter> params = new ArrayList<>();
final List<PropertyNode> pList = getInstanceProperties(cNode);
for (int i = 0; i < pList.size(); i++) {
PropertyNode pNode = pList.get(i);
params.add(param(pNode.getType(), "arg" + i));
}
ConstructorNode consNode = cNode.getDeclaredConstructor(params.toArray(Parameter.EMPTY_ARRAY));
if (consNode != null) {
Statement code = copyWith.getCode();
if (code instanceof ReturnStatement) {
ReturnStatement rs = (ReturnStatement) code;
Expression expr = rs.getExpression();
if (expr instanceof ConstructorCallExpression) {
expr.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, consNode);
}
}
}
}
use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.
the class MemberSignatureParser method createMethodNode.
static MethodNode createMethodNode(final AsmReferenceResolver resolver, final MethodStub method) {
GenericsType[] typeParameters = null;
ClassNode[] returnType = resolve(resolver, Type.getReturnType(method.desc));
ClassNode[] parameterTypes = resolve(resolver, Type.getArgumentTypes(method.desc));
ClassNode[] exceptionTypes = resolve(resolver, method.exceptions);
if (method.signature != null) {
FormalParameterParser parser = new FormalParameterParser(resolver) {
private int exceptionIndex, parameterIndex;
@Override
public SignatureVisitor visitReturnType() {
return new TypeSignatureParser(resolver) {
@Override
void finished(final ClassNode result) {
returnType[0] = applyErasure(result, returnType[0]);
}
};
}
@Override
public SignatureVisitor visitParameterType() {
return new TypeSignatureParser(resolver) {
@Override
void finished(final ClassNode result) {
parameterTypes[parameterIndex] = applyErasure(result, parameterTypes[parameterIndex]);
parameterIndex += 1;
}
};
}
@Override
public SignatureVisitor visitExceptionType() {
return new TypeSignatureParser(resolver) {
@Override
void finished(final ClassNode result) {
exceptionTypes[exceptionIndex] = applyErasure(result, exceptionTypes[exceptionIndex]);
exceptionIndex += 1;
}
};
}
};
new SignatureReader(method.signature).accept(parser);
typeParameters = parser.getTypeParameters();
} else {
returnType[0] = GenericsUtils.nonGeneric(returnType[0]);
for (int i = 0, n = parameterTypes.length; i < n; i += 1) {
parameterTypes[i] = GenericsUtils.nonGeneric(parameterTypes[i]);
}
}
int nParameters = parameterTypes.length;
Parameter[] parameters = new Parameter[nParameters];
List<String> parameterNames = method.parameterNames;
for (int i = 0; i < nParameters; i += 1) {
String parameterName = "param" + i;
if (parameterNames != null && i < parameterNames.size()) {
String decompiledName = parameterNames.get(i);
if (decompiledName != null) {
parameterName = decompiledName;
}
}
parameters[i] = new Parameter(parameterTypes[i], parameterName);
}
if (method.parameterAnnotations != null) {
for (Map.Entry<Integer, List<AnnotationStub>> entry : method.parameterAnnotations.entrySet()) {
for (AnnotationStub stub : entry.getValue()) {
AnnotationNode annotationNode = Annotations.createAnnotationNode(stub, resolver);
if (annotationNode != null) {
parameters[entry.getKey()].addAnnotation(annotationNode);
}
}
}
}
MethodNode result;
if ("<init>".equals(method.methodName)) {
result = new ConstructorNode(method.accessModifiers, parameters, exceptionTypes, null);
} else {
result = new MethodNode(method.methodName, method.accessModifiers, returnType[0], parameters, exceptionTypes, null);
Object annDefault = method.annotationDefault;
if (annDefault != null) {
if (annDefault instanceof TypeWrapper) {
annDefault = resolver.resolveType(Type.getType(((TypeWrapper) annDefault).desc));
}
result.setCode(new ReturnStatement(new ConstantExpression(annDefault)));
result.setAnnotationDefault(true);
} else {
// seems wrong but otherwise some tests fail (e.g. TestingASTTransformsTest)
result.setCode(new ReturnStatement(new ConstantExpression(null)));
}
}
if (typeParameters != null && typeParameters.length > 0) {
result.setGenericsTypes(typeParameters);
}
return result;
}
Aggregations