use of org.codehaus.groovy.GroovyBugError in project gcontracts by andresteingress.
the class TryCatchBlockGenerator method generateTryCatchBlockForInlineMode.
public static BlockStatement generateTryCatchBlockForInlineMode(final ClassNode assertionErrorClass, final String message, final Statement assertStatement) {
final Class powerAssertionErrorClass = loadPowerAssertionErrorClass();
if (powerAssertionErrorClass == null)
throw new GroovyBugError("GContracts >= 1.1.2 needs Groovy 1.7 or above!");
VariableExpression newErrorVariableExpression = new VariableExpression("newError", assertionErrorClass);
newErrorVariableExpression.setAccessedVariable(newErrorVariableExpression);
ExpressionStatement expr = new ExpressionStatement(new DeclarationExpression(newErrorVariableExpression, Token.newSymbol(Types.ASSIGN, -1, -1), new ConstructorCallExpression(assertionErrorClass, new ArgumentListExpression(new BinaryExpression(new ConstantExpression(message), Token.newSymbol(Types.PLUS, -1, -1), new MethodCallExpression(new VariableExpression(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error")), "getMessage", ArgumentListExpression.EMPTY_ARGUMENTS))))));
ExpressionStatement exp2 = new ExpressionStatement(new MethodCallExpression(newErrorVariableExpression, "setStackTrace", new ArgumentListExpression(new MethodCallExpression(new VariableExpression(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error")), "getStackTrace", ArgumentListExpression.EMPTY_ARGUMENTS))));
final BlockStatement catchBlock = new BlockStatement();
catchBlock.addStatement(expr);
catchBlock.addStatement(exp2);
catchBlock.addStatement(new ThrowStatement(newErrorVariableExpression));
final TryCatchStatement tryCatchStatement = new TryCatchStatement(assertStatement, new EmptyStatement());
tryCatchStatement.addCatch(new CatchStatement(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error"), catchBlock));
final BlockStatement assertBlockStatement = new BlockStatement();
assertBlockStatement.addStatement(tryCatchStatement);
return assertBlockStatement;
}
use of org.codehaus.groovy.GroovyBugError in project gcontracts by andresteingress.
the class TryCatchBlockGenerator method generateTryCatchBlock.
public static BlockStatement generateTryCatchBlock(final ClassNode assertionErrorClass, final String message, final Statement assertStatement) {
final String $_gc_closure_result = "$_gc_closure_result";
final VariableExpression variableExpression = new VariableExpression($_gc_closure_result, ClassHelper.Boolean_TYPE);
variableExpression.setAccessedVariable(variableExpression);
// if the assert statement is successful the return variable will be true else false
final BlockStatement overallBlock = new BlockStatement();
overallBlock.addStatement(new ExpressionStatement(new DeclarationExpression(variableExpression, Token.newSymbol(Types.ASSIGN, -1, -1), ConstantExpression.FALSE)));
final BlockStatement assertBlockStatement = new BlockStatement();
assertBlockStatement.addStatement(assertStatement);
assertBlockStatement.addStatement(new ExpressionStatement(new BinaryExpression(variableExpression, Token.newSymbol(Types.ASSIGN, -1, -1), ConstantExpression.TRUE)));
final Class powerAssertionErrorClass = loadPowerAssertionErrorClass();
if (powerAssertionErrorClass == null)
throw new GroovyBugError("GContracts >= 1.1.2 needs Groovy 1.7 or above!");
VariableExpression newErrorVariableExpression = new VariableExpression("newError", assertionErrorClass);
newErrorVariableExpression.setAccessedVariable(newErrorVariableExpression);
ExpressionStatement expr = new ExpressionStatement(new DeclarationExpression(newErrorVariableExpression, Token.newSymbol(Types.ASSIGN, -1, -1), new ConstructorCallExpression(assertionErrorClass, new ArgumentListExpression(new BinaryExpression(new ConstantExpression(message), Token.newSymbol(Types.PLUS, -1, -1), new MethodCallExpression(new VariableExpression(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error")), "getMessage", ArgumentListExpression.EMPTY_ARGUMENTS))))));
ExpressionStatement exp2 = new ExpressionStatement(new MethodCallExpression(newErrorVariableExpression, "setStackTrace", new ArgumentListExpression(new MethodCallExpression(new VariableExpression(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error")), "getStackTrace", ArgumentListExpression.EMPTY_ARGUMENTS))));
final BlockStatement catchBlock = new BlockStatement();
catchBlock.addStatement(expr);
catchBlock.addStatement(exp2);
final TryCatchStatement tryCatchStatement = new TryCatchStatement(assertBlockStatement, new EmptyStatement());
tryCatchStatement.addCatch(new CatchStatement(new Parameter(ClassHelper.makeWithoutCaching(powerAssertionErrorClass), "error"), catchBlock));
overallBlock.addStatement(tryCatchStatement);
overallBlock.addStatement(new ReturnStatement(variableExpression));
return overallBlock;
}
use of org.codehaus.groovy.GroovyBugError in project groovy by apache.
the class AsmClassGenerator method visitAttributeOrProperty.
private void visitAttributeOrProperty(PropertyExpression expression, MethodCallerMultiAdapter adapter) {
MethodVisitor mv = controller.getMethodVisitor();
Expression objectExpression = expression.getObjectExpression();
ClassNode classNode = controller.getClassNode();
if (isThisOrSuper(objectExpression)) {
// let's use the field expression if it's available
String name = expression.getPropertyAsString();
if (name != null) {
FieldNode field = null;
boolean privateSuperField = false;
if (isSuperExpression(objectExpression)) {
field = classNode.getSuperClass().getDeclaredField(name);
if (field != null && ((field.getModifiers() & ACC_PRIVATE) != 0)) {
privateSuperField = true;
}
} else {
if (controller.isNotExplicitThisInClosure(expression.isImplicitThis())) {
field = classNode.getDeclaredField(name);
if (field == null && classNode instanceof InnerClassNode) {
ClassNode outer = classNode.getOuterClass();
FieldNode outerClassField;
while (outer != null) {
outerClassField = outer.getDeclaredField(name);
if (outerClassField != null && outerClassField.isStatic() && outerClassField.isFinal()) {
if (outer != classNode.getOuterClass() && Modifier.isPrivate(outerClassField.getModifiers())) {
throw new GroovyBugError("Trying to access private constant field [" + outerClassField.getDeclaringClass() + "#" + outerClassField.getName() + "] from inner class");
}
PropertyExpression pexp = new PropertyExpression(new ClassExpression(outer), expression.getProperty());
pexp.visit(controller.getAcg());
return;
}
outer = outer.getSuperClass();
}
}
if (field == null && expression instanceof AttributeExpression && isThisExpression(objectExpression) && controller.isStaticContext()) {
// GROOVY-6183
ClassNode current = classNode.getSuperClass();
while (field == null && current != null) {
field = current.getDeclaredField(name);
current = current.getSuperClass();
}
if (field != null && (field.isProtected() || field.isPublic())) {
visitFieldExpression(new FieldExpression(field));
return;
}
}
}
}
if (field != null && !privateSuperField) {
//GROOVY-4497: don't visit super field if it is private
visitFieldExpression(new FieldExpression(field));
return;
}
if (isSuperExpression(objectExpression)) {
String prefix;
if (controller.getCompileStack().isLHS()) {
throw new GroovyBugError("Unexpected super property set for:" + expression.getText());
} else {
prefix = "get";
}
String propName = prefix + MetaClassHelper.capitalize(name);
visitMethodCallExpression(new MethodCallExpression(objectExpression, propName, MethodCallExpression.NO_ARGUMENTS));
return;
}
}
}
final String propName = expression.getPropertyAsString();
//TODO: add support for super here too
if (expression.getObjectExpression() instanceof ClassExpression && propName != null && propName.equals("this")) {
// we have something like A.B.this, and need to make it
// into this.this$0.this$0, where this.this$0 returns
// A.B and this.this$0.this$0 return A.
ClassNode type = objectExpression.getType();
ClassNode iterType = classNode;
if (controller.getCompileStack().isInSpecialConstructorCall() && classNode instanceof InnerClassNode) {
boolean staticInnerClass = classNode.isStaticClass();
// Outer.this in a special constructor call
if (classNode.getOuterClass().equals(type)) {
ConstructorNode ctor = controller.getConstructorNode();
Expression receiver = !staticInnerClass ? new VariableExpression(ctor.getParameters()[0]) : new ClassExpression(type);
receiver.setSourcePosition(expression);
receiver.visit(this);
return;
}
}
mv.visitVarInsn(ALOAD, 0);
while (!iterType.equals(type)) {
String ownerName = BytecodeHelper.getClassInternalName(iterType);
if (iterType.getOuterClass() == null)
break;
FieldNode thisField = iterType.getField("this$0");
iterType = iterType.getOuterClass();
if (thisField == null) {
// closure within inner class
mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ClassHelper.CLOSURE_TYPE), "getThisObject", "()Ljava/lang/Object;", false);
mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(iterType));
} else {
ClassNode thisFieldType = thisField.getType();
if (ClassHelper.CLOSURE_TYPE.equals(thisFieldType)) {
mv.visitFieldInsn(GETFIELD, ownerName, "this$0", BytecodeHelper.getTypeDescription(ClassHelper.CLOSURE_TYPE));
mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ClassHelper.CLOSURE_TYPE), "getThisObject", "()Ljava/lang/Object;", false);
mv.visitTypeInsn(CHECKCAST, BytecodeHelper.getClassInternalName(iterType));
} else {
String typeName = BytecodeHelper.getTypeDescription(iterType);
mv.visitFieldInsn(GETFIELD, ownerName, "this$0", typeName);
}
}
}
controller.getOperandStack().push(type);
return;
}
if (adapter == getProperty && !expression.isSpreadSafe() && propName != null) {
controller.getCallSiteWriter().makeGetPropertySite(objectExpression, propName, expression.isSafe(), expression.isImplicitThis());
} else if (adapter == getGroovyObjectProperty && !expression.isSpreadSafe() && propName != null) {
controller.getCallSiteWriter().makeGroovyObjectGetPropertySite(objectExpression, propName, expression.isSafe(), expression.isImplicitThis());
} else {
// todo: for improved modularity and extensibility, this should be moved into a writer
if (controller.getCompileStack().isLHS())
controller.getOperandStack().box();
controller.getInvocationWriter().makeCall(expression, // receiver
objectExpression, // messageName
new CastExpression(ClassHelper.STRING_TYPE, expression.getProperty()), MethodCallExpression.NO_ARGUMENTS, adapter, expression.isSafe(), expression.isSpreadSafe(), expression.isImplicitThis());
}
}
use of org.codehaus.groovy.GroovyBugError in project groovy by apache.
the class AsmClassGenerator method visitAnnotationDefaultExpression.
void visitAnnotationDefaultExpression(AnnotationVisitor av, ClassNode type, Expression exp) {
if (exp instanceof ClosureExpression) {
ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) exp, ACC_PUBLIC);
Type t = Type.getType(BytecodeHelper.getTypeDescription(closureClass));
av.visit(null, t);
} else if (type.isArray()) {
AnnotationVisitor avl = av.visitArray(null);
ClassNode componentType = type.getComponentType();
if (exp instanceof ListExpression) {
ListExpression list = (ListExpression) exp;
for (Expression lExp : list.getExpressions()) {
visitAnnotationDefaultExpression(avl, componentType, lExp);
}
} else {
visitAnnotationDefaultExpression(avl, componentType, exp);
}
} else if (ClassHelper.isPrimitiveType(type) || type.equals(ClassHelper.STRING_TYPE)) {
ConstantExpression constExp = (ConstantExpression) exp;
av.visit(null, constExp.getValue());
} else if (ClassHelper.CLASS_Type.equals(type)) {
ClassNode clazz = exp.getType();
Type t = Type.getType(BytecodeHelper.getTypeDescription(clazz));
av.visit(null, t);
} else if (type.isDerivedFrom(ClassHelper.Enum_Type)) {
PropertyExpression pExp = (PropertyExpression) exp;
ClassExpression cExp = (ClassExpression) pExp.getObjectExpression();
String desc = BytecodeHelper.getTypeDescription(cExp.getType());
String name = pExp.getPropertyAsString();
av.visitEnum(null, desc, name);
} else if (type.implementsInterface(ClassHelper.Annotation_TYPE)) {
AnnotationConstantExpression avExp = (AnnotationConstantExpression) exp;
AnnotationNode value = (AnnotationNode) avExp.getValue();
AnnotationVisitor avc = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(avExp.getType()));
visitAnnotationAttributes(value, avc);
} else {
throw new GroovyBugError("unexpected annotation type " + type.getName());
}
av.visitEnd();
}
use of org.codehaus.groovy.GroovyBugError in project groovy by apache.
the class GeneratorContext method encodeAsValidClassName.
public static String encodeAsValidClassName(String name) {
final int l = name.length();
StringBuilder b = null;
int lastEscape = -1;
for (int i = 0; i < l; ++i) {
final int encodeIndex = name.charAt(i) - MIN_ENCODING;
if (encodeIndex >= 0 && encodeIndex < CHARACTERS_TO_ENCODE.length) {
if (CHARACTERS_TO_ENCODE[encodeIndex]) {
if (b == null) {
b = new StringBuilder(name.length() + 3);
b.append(name, 0, i);
} else {
b.append(name, lastEscape + 1, i);
}
b.append('_');
lastEscape = i;
}
}
}
if (b == null)
return name.toString();
if (lastEscape == -1)
throw new GroovyBugError("unexpected escape char control flow in " + name);
b.append(name, lastEscape + 1, l);
return b.toString();
}
Aggregations