org.eclipse.xtext.xbase.XThrowExpression Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XThrowExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EarlyExitValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkInvalidReturnExpression(XExpression expression) {
	final EReference contFeature = (EReference) expression.eContainingFeature();
	final Map<EReference, EarlyExitKind> map = getDisallowedEarlyExitReferences();
	if (map.containsKey(contFeature)) {
		EarlyExitKind exitKind = map.get(contFeature);
		List<XExpression> returns = newArrayList();
		collectExits(expression, returns);
		for (XExpression expr : returns) {
			if (expr instanceof XReturnExpression && (exitKind == EarlyExitKind.RETURN || exitKind == EarlyExitKind.BOTH)) {
				error("A return expression is not allowed in this context.", expr, null, IssueCodes.INVALID_EARLY_EXIT);
			}
			if (expr instanceof XThrowExpression && (exitKind == EarlyExitKind.THROW || exitKind == EarlyExitKind.BOTH)) {
				error("A throw expression is not allowed in this context.", expr, null, IssueCodes.INVALID_EARLY_EXIT);
			}
		}
	}
}
 
Example #2
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void checkValidReturn(XReturnExpression object, ITypeComputationState state) {
	// if the expectation comes from a method's return type
	// then it is legal, thus we must check if the return is
	// contained in a throw expression
	if (hasThrowableExpectation(state) &&
			EcoreUtil2.getContainerOfType(object, XThrowExpression.class) != null) {
		state.addDiagnostic(new EObjectDiagnosticImpl(
				Severity.ERROR,
				IssueCodes.INVALID_RETURN,
				"Invalid return inside throw.",
				object,
				null,
				-1,
				new String[] { 
				}));
	}
}
 
Example #3
Source File: ValidationTests.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testUnreachableCode_01() throws Exception {
	XBlockExpression expression = (XBlockExpression) expression(
			"{" +
			"	while (false) {" +
			"      throw new Exception() " +
			"   }" +
			"   null" +
			"}");
	XWhileExpression whileLoop = (XWhileExpression) expression.getExpressions().get(0);
	XThrowExpression throwExpression = (XThrowExpression) ((XBlockExpression) whileLoop.getBody()).getExpressions().get(0);
	helper.assertError(throwExpression, XbasePackage.Literals.XTHROW_EXPRESSION, UNREACHABLE_CODE);
}
 
Example #4
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Object _doEvaluate(XThrowExpression throwExpression, IEvaluationContext context, CancelIndicator indicator) {
	Object thrown = internalEvaluate(throwExpression.getExpression(), context, indicator);
	if (thrown == null) {
		return throwNullPointerException(throwExpression, "throwable expression evaluated to 'null'");
	}
	if (!(thrown instanceof Throwable)) {
		return throwClassCastException(throwExpression.getExpression(), thrown, Throwable.class);
	}
	throw new EvaluationException((Throwable) thrown);
}
 
Example #5
Source File: XbaseFormatter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _format(final XThrowExpression expr, @Extension final IFormattableDocument format) {
  final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
    it.oneSpace();
  };
  format.<XExpression>prepend(expr.getExpression(), _function);
  format.<XExpression>format(expr.getExpression());
}
 
Example #6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature 
 */
protected void _toJavaStatement(XThrowExpression expr, ITreeAppendable b, boolean isReferenced) {
	internalToJavaStatement(expr.getExpression(), b, true);
	b.newLine().append("throw ");
	internalToConvertedExpression(expr.getExpression(), b, null);
	b.append(";");
}
 
Example #7
Source File: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Collection<IEarlyExitComputer.ExitPoint> exitPoints(final XExpression expression) {
  if (expression instanceof XDoWhileExpression) {
    return _exitPoints((XDoWhileExpression)expression);
  } else if (expression instanceof XWhileExpression) {
    return _exitPoints((XWhileExpression)expression);
  } else if (expression instanceof XAbstractFeatureCall) {
    return _exitPoints((XAbstractFeatureCall)expression);
  } else if (expression instanceof XBasicForLoopExpression) {
    return _exitPoints((XBasicForLoopExpression)expression);
  } else if (expression instanceof XBlockExpression) {
    return _exitPoints((XBlockExpression)expression);
  } else if (expression instanceof XConstructorCall) {
    return _exitPoints((XConstructorCall)expression);
  } else if (expression instanceof XForLoopExpression) {
    return _exitPoints((XForLoopExpression)expression);
  } else if (expression instanceof XIfExpression) {
    return _exitPoints((XIfExpression)expression);
  } else if (expression instanceof XReturnExpression) {
    return _exitPoints((XReturnExpression)expression);
  } else if (expression instanceof XSwitchExpression) {
    return _exitPoints((XSwitchExpression)expression);
  } else if (expression instanceof XSynchronizedExpression) {
    return _exitPoints((XSynchronizedExpression)expression);
  } else if (expression instanceof XThrowExpression) {
    return _exitPoints((XThrowExpression)expression);
  } else if (expression instanceof XTryCatchFinallyExpression) {
    return _exitPoints((XTryCatchFinallyExpression)expression);
  } else if (expression instanceof XVariableDeclaration) {
    return _exitPoints((XVariableDeclaration)expression);
  } else if (expression != null) {
    return _exitPoints(expression);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression).toString());
  }
}
 
Example #8
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void internalToConvertedExpression(XExpression obj, ITreeAppendable appendable) {
	if (obj instanceof XBlockExpression) {
		_toJavaExpression((XBlockExpression) obj, appendable);
	} else if (obj instanceof XCastedExpression) {
		_toJavaExpression((XCastedExpression) obj, appendable);
	} else if (obj instanceof XClosure) {
		_toJavaExpression((XClosure) obj, appendable);
	} else if (obj instanceof XAnnotation) {
		_toJavaExpression((XAnnotation) obj, appendable);
	} else if (obj instanceof XConstructorCall) {
		_toJavaExpression((XConstructorCall) obj, appendable);
	} else if (obj instanceof XIfExpression) {
		_toJavaExpression((XIfExpression) obj, appendable);
	} else if (obj instanceof XInstanceOfExpression) {
		_toJavaExpression((XInstanceOfExpression) obj, appendable);
	} else if (obj instanceof XSwitchExpression) {
		_toJavaExpression((XSwitchExpression) obj, appendable);
	} else if (obj instanceof XTryCatchFinallyExpression) {
		_toJavaExpression((XTryCatchFinallyExpression) obj, appendable);
	} else if (obj instanceof XListLiteral) {
		_toJavaExpression((XListLiteral) obj, appendable);
	} else if (obj instanceof XSetLiteral) {
		_toJavaExpression((XSetLiteral) obj, appendable);
	} else if (obj instanceof XSynchronizedExpression) {
		_toJavaExpression((XSynchronizedExpression) obj, appendable);
	} else if (obj instanceof XReturnExpression) {
		_toJavaExpression((XReturnExpression) obj, appendable);
	} else if (obj instanceof XThrowExpression) {
		_toJavaExpression((XThrowExpression) obj, appendable);
	} else {
		super.internalToConvertedExpression(obj, appendable);
	}
}
 
Example #9
Source File: XbaseFormatter2.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _format(final XThrowExpression expr, final FormattableDocument format) {
  final Procedure1<FormattingDataInit> _function = (FormattingDataInit it) -> {
    it.oneSpace();
  };
  Function1<? super FormattableDocument, ? extends Iterable<FormattingData>> _prepend = this._formattingDataFactory.prepend(this._nodeModelAccess.nodeForEObject(expr.getExpression()), _function);
  format.operator_add(_prepend);
  this.format(expr.getExpression(), format);
}
 
Example #10
Source File: AbstractBatchReturnTypeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public boolean hasReturnExpression(final XExpression expression) {
  boolean _switchResult = false;
  boolean _matched = false;
  if (expression instanceof XReturnExpression) {
    _matched=true;
    _switchResult = true;
  }
  if (!_matched) {
    if (expression instanceof XThrowExpression) {
      _matched=true;
      _switchResult = true;
    }
  }
  if (!_matched) {
    if (expression instanceof XClosure) {
      _matched=true;
      _switchResult = false;
    }
  }
  if (!_matched) {
    final Function1<EObject, Boolean> _function = (EObject content) -> {
      boolean _switchResult_1 = false;
      boolean _matched_1 = false;
      if (content instanceof XExpression) {
        _matched_1=true;
        _switchResult_1 = this.hasReturnExpression(((XExpression)content));
      }
      if (!_matched_1) {
        _switchResult_1 = false;
      }
      return Boolean.valueOf(_switchResult_1);
    };
    _switchResult = IterableExtensions.<EObject>exists(expression.eContents(), _function);
  }
  return _switchResult;
}
 
Example #11
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _computeTypes(XThrowExpression object, ITypeComputationState state) {
	LightweightTypeReference throwable = getRawTypeForName(Throwable.class, state);
	ITypeComputationState expressionState = state.withExpectation(throwable);
	ITypeComputationResult types = expressionState.computeTypes(object.getExpression());
	
	state.acceptActualType(getPrimitiveVoid(state), ConformanceFlags.NO_IMPLICIT_RETURN | ConformanceFlags.THROWN_EXCEPTION);
	
	LightweightTypeReference thrownException = types.getActualExpressionType();
	if (thrownException != null && !thrownException.isUnknown()) {
		validateUnhandledException(thrownException, object, XbasePackage.Literals.XTHROW_EXPRESSION__EXPRESSION, state, 
				(type)->"Unhandled exception type " + type.getSimpleName());
	}
}
 
Example #12
Source File: EarlyExitValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void collectExits(EObject expr, List<XExpression> found) {
	if (expr instanceof XReturnExpression) {
		found.add((XExpression) expr);
	} else if (expr instanceof XThrowExpression) {
		found.add((XExpression) expr);
	} else if (expr instanceof XClosure) {
		return;
	}
	for (EObject child : expr.eContents()) {
		collectExits(child, found);
	}
}
 
Example #13
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void doTestTryCatchExpression(XTryCatchFinallyExpression tryEx) {
	assertFeatureCall("foo", ((XThrowExpression)tryEx.getExpression()).getExpression());
	assertFeatureCall("baz", tryEx.getFinallyExpression());
	
	assertEquals(1,tryEx.getCatchClauses().size());
	XCatchClause clause = tryEx.getCatchClauses().get(0);
	assertFeatureCall("bar", clause.getExpression());
	assertEquals("java.lang.Exception", clause.getDeclaredParam().getParameterType().getIdentifier());
	assertEquals("e", clause.getDeclaredParam().getName());
}
 
Example #14
Source File: AbstractXbaseSemanticSequencer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Deprecated
protected void sequence_XThrowExpression(EObject context, XThrowExpression semanticObject) {
	sequence_XThrowExpression(createContext(context, semanticObject), semanticObject);
}
 
Example #15
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testThrowExpression() throws Exception {
	XThrowExpression throwEx = (XThrowExpression) expression("throw foo");
	assertFeatureCall("foo", throwEx.getExpression());
}
 
Example #16
Source File: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected Collection<IEarlyExitComputer.ExitPoint> _exitPoints(final XThrowExpression expression) {
  IEarlyExitComputer.ExitPoint _exitPoint = new IEarlyExitComputer.ExitPoint(expression, true);
  return Collections.<IEarlyExitComputer.ExitPoint>singletonList(_exitPoint);
}
 
Example #17
Source File: XbaseExpectedTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testThrowExpression() throws Exception {
	XThrowExpression exp = (XThrowExpression) expression("throw null");
	assertExpected("java.lang.Throwable", exp.getExpression());
}
 
Example #18
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * don't call this directly. Always call evaluate() internalEvaluate()
 */
protected Object doEvaluate(XExpression expression, IEvaluationContext context, CancelIndicator indicator) {
	if (expression instanceof XAssignment) {
      return _doEvaluate((XAssignment)expression, context, indicator);
    } else if (expression instanceof XDoWhileExpression) {
      return _doEvaluate((XDoWhileExpression)expression, context, indicator);
    } else if (expression instanceof XMemberFeatureCall) {
      return _doEvaluate((XMemberFeatureCall)expression, context, indicator);
    } else if (expression instanceof XWhileExpression) {
      return _doEvaluate((XWhileExpression)expression, context, indicator);
    } else if (expression instanceof XFeatureCall) {
    	return _doEvaluate((XFeatureCall)expression, context, indicator);
    } else if (expression instanceof XAbstractFeatureCall) {
    	return _doEvaluate((XAbstractFeatureCall)expression, context, indicator);
    } else if (expression instanceof XBlockExpression) {
      return _doEvaluate((XBlockExpression)expression, context, indicator);
    } else if (expression instanceof XSynchronizedExpression) {
	  return _doEvaluate((XSynchronizedExpression)expression, context, indicator);
	} else if (expression instanceof XBooleanLiteral) {
      return _doEvaluate((XBooleanLiteral)expression, context, indicator);
    } else if (expression instanceof XCastedExpression) {
      return _doEvaluate((XCastedExpression)expression, context, indicator);
    } else if (expression instanceof XClosure) {
      return _doEvaluate((XClosure)expression, context, indicator);
    } else if (expression instanceof XConstructorCall) {
      return _doEvaluate((XConstructorCall)expression, context, indicator);
    } else if (expression instanceof XForLoopExpression) {
      return _doEvaluate((XForLoopExpression)expression, context, indicator);
    } else if (expression instanceof XBasicForLoopExpression) {
	  return _doEvaluate((XBasicForLoopExpression)expression, context, indicator);
	} else if (expression instanceof XIfExpression) {
      return _doEvaluate((XIfExpression)expression, context, indicator);
    } else if (expression instanceof XInstanceOfExpression) {
      return _doEvaluate((XInstanceOfExpression)expression, context, indicator);
    } else if (expression instanceof XNullLiteral) {
      return _doEvaluate((XNullLiteral)expression, context, indicator);
    } else if (expression instanceof XNumberLiteral) {
      return _doEvaluate((XNumberLiteral)expression, context, indicator);
    } else if (expression instanceof XReturnExpression) {
      return _doEvaluate((XReturnExpression)expression, context, indicator);
    } else if (expression instanceof XStringLiteral) {
      return _doEvaluate((XStringLiteral)expression, context, indicator);
    } else if (expression instanceof XSwitchExpression) {
      return _doEvaluate((XSwitchExpression)expression, context, indicator);
    } else if (expression instanceof XThrowExpression) {
      return _doEvaluate((XThrowExpression)expression, context, indicator);
    } else if (expression instanceof XTryCatchFinallyExpression) {
      return _doEvaluate((XTryCatchFinallyExpression)expression, context, indicator);
    } else if (expression instanceof XTypeLiteral) {
      return _doEvaluate((XTypeLiteral)expression, context, indicator);
    } else if (expression instanceof XVariableDeclaration) {
	      return _doEvaluate((XVariableDeclaration)expression, context, indicator);
    } else if (expression instanceof XListLiteral) {
	      return _doEvaluate((XListLiteral)expression, context, indicator);
    } else if (expression instanceof XSetLiteral) {
	      return _doEvaluate((XSetLiteral)expression, context, indicator);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(expression, context, indicator).toString());
    }
}
 
Example #19
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
	if (expr instanceof XAnnotation) {
		return false;
	}
	if (expr instanceof XListLiteral) {
		return false;
	}
	if (expr instanceof XSetLiteral) {
		return false;
	}
	if (expr instanceof XCastedExpression) {
		return false;
	}
	if (expr instanceof XInstanceOfExpression) {
		return false;
	}
	if (expr instanceof XMemberFeatureCall && isVariableDeclarationRequired((XMemberFeatureCall) expr, b))
		return true;
	EObject container = expr.eContainer();
	if ((container instanceof XVariableDeclaration)
		|| (container instanceof XReturnExpression) 
		|| (container instanceof XThrowExpression)) {
		return false;
	}
	if (container instanceof XIfExpression) {
		XIfExpression ifExpression = (XIfExpression) container;
		if (ifExpression.getThen() == expr || ifExpression.getElse() == expr) {
			return false;
		}
	}
	if (container instanceof XCasePart) {
		XCasePart casePart = (XCasePart) container;
		if (casePart.getThen() == expr) {
			return false;
		}
	}
	if (container instanceof XSwitchExpression) {
		XSwitchExpression switchExpression = (XSwitchExpression) container;
		if (switchExpression.getDefault() == expr) {
			return false;
		}
	}
	if (container instanceof XBlockExpression) {
		List<XExpression> siblings = ((XBlockExpression) container).getExpressions();
		if (siblings.get(siblings.size() - 1) == expr) {
			return isVariableDeclarationRequired(getFeatureCall(expr), expr, b);
		}
	}
	if (container instanceof XClosure) {
		if (((XClosure) container).getExpression() == expr) {
			return false;
		}
	}
	if (expr instanceof XAssignment) {
		XAssignment a = (XAssignment) expr;
		for (XExpression arg : getActualArguments(a)) {
			if (isVariableDeclarationRequired(arg, b, recursive)) {
				return true;
			}
		}
	}
	return super.isVariableDeclarationRequired(expr, b, recursive);
}
 
Example #20
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void _toJavaExpression(XThrowExpression throwExpression, ITreeAppendable b) {
	b.append("/* error - couldn't compile invalid throw */");
}
 
Example #21
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void doInternalToJavaStatement(XExpression obj, ITreeAppendable appendable, boolean isReferenced) {
	if (obj instanceof XBlockExpression) {
		_toJavaStatement((XBlockExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XCastedExpression) {
		_toJavaStatement((XCastedExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XClosure) {
		_toJavaStatement((XClosure) obj, appendable, isReferenced);
	} else if (obj instanceof XConstructorCall) {
		_toJavaStatement((XConstructorCall) obj, appendable, isReferenced);
	} else if (obj instanceof XDoWhileExpression) {
		_toJavaStatement((XDoWhileExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XForLoopExpression) {
		_toJavaStatement((XForLoopExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XBasicForLoopExpression) {
		_toJavaStatement((XBasicForLoopExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XIfExpression) {
		_toJavaStatement((XIfExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XInstanceOfExpression) {
		_toJavaStatement((XInstanceOfExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XReturnExpression) {
		_toJavaStatement((XReturnExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XSwitchExpression) {
		_toJavaStatement((XSwitchExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XThrowExpression) {
		_toJavaStatement((XThrowExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XTryCatchFinallyExpression) {
		_toJavaStatement((XTryCatchFinallyExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XVariableDeclaration) {
		_toJavaStatement((XVariableDeclaration) obj, appendable, isReferenced);
	} else if (obj instanceof XWhileExpression) {
		_toJavaStatement((XWhileExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XListLiteral) {
		_toJavaStatement((XListLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XSetLiteral) {
		_toJavaStatement((XSetLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XSynchronizedExpression) {
		_toJavaStatement((XSynchronizedExpression) obj, appendable, isReferenced);
	} else {
		super.doInternalToJavaStatement(obj, appendable, isReferenced);
	}
}
 
Example #22
Source File: ExtendedEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public boolean isDefiniteEarlyExit(XExpression expression) {
	// TODO further improvements
	if (expression instanceof XIfExpression) {
		XIfExpression ifExpression = (XIfExpression) expression;
		return isDefiniteEarlyExit(ifExpression.getThen()) && isDefiniteEarlyExit(ifExpression.getElse());
	} else if (expression instanceof XSwitchExpression) {
		XSwitchExpression switchExpression = (XSwitchExpression) expression;
		if (isDefiniteEarlyExit(switchExpression.getDefault())) {
			for(XCasePart caseExpression: switchExpression.getCases()) {
				if (!isDefiniteEarlyExit(caseExpression.getThen())) {
					return false;
				}
			}
			return true;
		}
		return false;
	} else if (expression instanceof XTryCatchFinallyExpression) {
		XTryCatchFinallyExpression tryExpression = (XTryCatchFinallyExpression) expression;
		if (isDefiniteEarlyExit(tryExpression.getFinallyExpression())) {
			return true;
		}
		if (isDefiniteEarlyExit(tryExpression.getExpression())) {
			for(XCatchClause catchClause: tryExpression.getCatchClauses()) {
				if (!isDefiniteEarlyExit(catchClause.getExpression())) {
					return false;
				}
			}
			return true;
		}
		return false;
	} else if (expression instanceof XBlockExpression) {
		List<XExpression> expressions = ((XBlockExpression) expression).getExpressions();
		for(int i = expressions.size() - 1; i >= 0; i--) {
			if (isDefiniteEarlyExit(expressions.get(i))) {
				return true;
			}
		}
	} else if (expression instanceof XSynchronizedExpression) {
		return isDefiniteEarlyExit(((XSynchronizedExpression) expression).getExpression());
	}
	return expression instanceof XReturnExpression || expression instanceof XThrowExpression;
}
 
Example #23
Source File: ExtendedEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Returns <code>true</code> for expressions that seem to be early exit expressions, e.g.
 * <pre>
 *   while(condition) {
 *     if (anotherCondition)
 *       return value
 *     changeResultOfFirstCondition
 *   }
 * </pre>
 */
public boolean isIntentionalEarlyExit(/* @Nullable */ XExpression expression) {
	if (expression == null) {
		return true;
	}
	if (expression instanceof XBlockExpression) {
		XBlockExpression block = (XBlockExpression) expression;
		List<XExpression> children = block.getExpressions();
		for(XExpression child: children) {
			if (isIntentionalEarlyExit(child)) {
				return true;
			}
		}
	} else if (expression instanceof XIfExpression) {
		return isIntentionalEarlyExit(((XIfExpression) expression).getThen()) 
				|| isIntentionalEarlyExit(((XIfExpression) expression).getElse());
	} else if (expression instanceof XSwitchExpression) {
		XSwitchExpression switchExpression = (XSwitchExpression) expression;
		for(XCasePart caseExpression: switchExpression.getCases()) {
			if (isIntentionalEarlyExit(caseExpression.getThen())) {
				return true;
			}
		}
		if (isIntentionalEarlyExit(switchExpression.getDefault())) {
			return true;
		}
	} else if (expression instanceof XTryCatchFinallyExpression) {
		XTryCatchFinallyExpression tryCatchFinally = (XTryCatchFinallyExpression) expression;
		if (isIntentionalEarlyExit(tryCatchFinally.getExpression())) {
			for(XCatchClause catchClause: tryCatchFinally.getCatchClauses()) {
				if (!isIntentionalEarlyExit(catchClause.getExpression()))
					return false;
			}
			return true;
		}
		return false;
	} else if (expression instanceof XAbstractWhileExpression) {
		return isIntentionalEarlyExit(((XAbstractWhileExpression) expression).getBody());
	} else if (expression instanceof XForLoopExpression) {
		return isIntentionalEarlyExit(((XForLoopExpression) expression).getEachExpression());
	} else if (expression instanceof XBasicForLoopExpression) {
		return isIntentionalEarlyExit(((XBasicForLoopExpression) expression).getEachExpression());
	} else if (expression instanceof XSynchronizedExpression) {
		return isIntentionalEarlyExit(((XSynchronizedExpression) expression).getExpression());
	}
	return expression instanceof XReturnExpression || expression instanceof XThrowExpression;
}
 
Example #24
Source File: ThrownExceptionSwitch.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Boolean caseXThrowExpression(XThrowExpression object) {
	accept(getType(object.getExpression()));
	return Boolean.TRUE;
}
 
Example #25
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void computeTypes(XExpression expression, ITypeComputationState state) {
	if (expression instanceof XAssignment) {
		_computeTypes((XAssignment)expression, state);
	} else if (expression instanceof XAbstractFeatureCall) {
		_computeTypes((XAbstractFeatureCall)expression, state);
	} else if (expression instanceof XDoWhileExpression) {
		_computeTypes((XDoWhileExpression)expression, state);
	} else if (expression instanceof XWhileExpression) {
		_computeTypes((XWhileExpression)expression, state);
	} else if (expression instanceof XBlockExpression) {
		_computeTypes((XBlockExpression)expression, state);
	} else if (expression instanceof XBooleanLiteral) {
		_computeTypes((XBooleanLiteral)expression, state);
	} else if (expression instanceof XCastedExpression) {
		_computeTypes((XCastedExpression)expression, state);
	} else if (expression instanceof XClosure) {
		_computeTypes((XClosure)expression, state);
	} else if (expression instanceof XConstructorCall) {
		_computeTypes((XConstructorCall)expression, state);
	} else if (expression instanceof XForLoopExpression) {
		_computeTypes((XForLoopExpression)expression, state);
	} else if (expression instanceof XBasicForLoopExpression) {
		_computeTypes((XBasicForLoopExpression)expression, state);
	} else if (expression instanceof XIfExpression) {
		_computeTypes((XIfExpression)expression, state);
	} else if (expression instanceof XInstanceOfExpression) {
		_computeTypes((XInstanceOfExpression)expression, state);
	} else if (expression instanceof XNumberLiteral) {
		_computeTypes((XNumberLiteral)expression, state);
	} else if (expression instanceof XNullLiteral) {
		_computeTypes((XNullLiteral)expression, state);
	} else if (expression instanceof XReturnExpression) {
		_computeTypes((XReturnExpression)expression, state);
	} else if (expression instanceof XStringLiteral) {
		_computeTypes((XStringLiteral)expression, state);
	} else if (expression instanceof XSwitchExpression) {
		_computeTypes((XSwitchExpression)expression, state);
	} else if (expression instanceof XThrowExpression) {
		_computeTypes((XThrowExpression)expression, state);
	} else if (expression instanceof XTryCatchFinallyExpression) {
		_computeTypes((XTryCatchFinallyExpression)expression, state);
	} else if (expression instanceof XTypeLiteral) {
		_computeTypes((XTypeLiteral)expression, state);
	} else if (expression instanceof XVariableDeclaration) {
		_computeTypes((XVariableDeclaration)expression, state);
	} else if (expression instanceof XListLiteral) {
		_computeTypes((XListLiteral)expression, state);
	} else if (expression instanceof XSetLiteral) {
		_computeTypes((XSetLiteral)expression, state);
	} else if (expression instanceof XSynchronizedExpression) {
		_computeTypes((XSynchronizedExpression)expression, state);
	} else {
		throw new UnsupportedOperationException("Missing type computation for expression type: " + expression.eClass().getName() + " / " + state);
	}
}
 
Example #26
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isValueExpectedRecursive(XExpression expr) {
	EStructuralFeature feature = expr.eContainingFeature();
	EObject container = expr.eContainer();
	
	// is part of block
	if (container instanceof XBlockExpression) {
		XBlockExpression blockExpression = (XBlockExpression) container;
		final List<XExpression> expressions = blockExpression.getExpressions();
		if (expressions.get(expressions.size()-1) != expr) {
			return false;
		}
	}
	// no expectation cases
	if (feature == XbasePackage.Literals.XTRY_CATCH_FINALLY_EXPRESSION__FINALLY_EXPRESSION
		|| feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__BODY
		|| feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__EACH_EXPRESSION) {
		return false;
	}
	// is value expected
	if (container instanceof XAbstractFeatureCall 
		|| container instanceof XConstructorCall
		|| container instanceof XAssignment
		|| container instanceof XVariableDeclaration
		|| container instanceof XReturnExpression
		|| container instanceof XThrowExpression
		|| feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__FOR_EXPRESSION
		|| feature == XbasePackage.Literals.XSWITCH_EXPRESSION__SWITCH
		|| feature == XbasePackage.Literals.XCASE_PART__CASE
		|| feature == XbasePackage.Literals.XIF_EXPRESSION__IF
		|| feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__PREDICATE
		|| feature == XbasePackage.Literals.XBASIC_FOR_LOOP_EXPRESSION__EXPRESSION
		|| feature == XbasePackage.Literals.XSYNCHRONIZED_EXPRESSION__PARAM) {
		return true;
	}
	if (isLocalClassSemantics(container) || logicalContainerProvider.getLogicalContainer(expr) != null) {
		LightweightTypeReference expectedReturnType = typeResolver.resolveTypes(expr).getExpectedReturnType(expr);
		return expectedReturnType == null || !expectedReturnType.isPrimitiveVoid();
	}
	if (container instanceof XCasePart || container instanceof XCatchClause) {
		container = container.eContainer();
	}
	if (container instanceof XExpression) {
		return isValueExpectedRecursive((XExpression) container);
	}
	return true;
}
 
Example #27
Source File: AbstractXbaseSemanticSequencer.java    From xtext-extras with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Contexts:
 *     XExpression returns XThrowExpression
 *     XAssignment returns XThrowExpression
 *     XAssignment.XBinaryOperation_1_1_0_0_0 returns XThrowExpression
 *     XOrExpression returns XThrowExpression
 *     XOrExpression.XBinaryOperation_1_0_0_0 returns XThrowExpression
 *     XAndExpression returns XThrowExpression
 *     XAndExpression.XBinaryOperation_1_0_0_0 returns XThrowExpression
 *     XEqualityExpression returns XThrowExpression
 *     XEqualityExpression.XBinaryOperation_1_0_0_0 returns XThrowExpression
 *     XRelationalExpression returns XThrowExpression
 *     XRelationalExpression.XInstanceOfExpression_1_0_0_0_0 returns XThrowExpression
 *     XRelationalExpression.XBinaryOperation_1_1_0_0_0 returns XThrowExpression
 *     XOtherOperatorExpression returns XThrowExpression
 *     XOtherOperatorExpression.XBinaryOperation_1_0_0_0 returns XThrowExpression
 *     XAdditiveExpression returns XThrowExpression
 *     XAdditiveExpression.XBinaryOperation_1_0_0_0 returns XThrowExpression
 *     XMultiplicativeExpression returns XThrowExpression
 *     XMultiplicativeExpression.XBinaryOperation_1_0_0_0 returns XThrowExpression
 *     XUnaryOperation returns XThrowExpression
 *     XCastedExpression returns XThrowExpression
 *     XCastedExpression.XCastedExpression_1_0_0_0 returns XThrowExpression
 *     XPostfixOperation returns XThrowExpression
 *     XPostfixOperation.XPostfixOperation_1_0_0 returns XThrowExpression
 *     XMemberFeatureCall returns XThrowExpression
 *     XMemberFeatureCall.XAssignment_1_0_0_0_0 returns XThrowExpression
 *     XMemberFeatureCall.XMemberFeatureCall_1_1_0_0_0 returns XThrowExpression
 *     XPrimaryExpression returns XThrowExpression
 *     XParenthesizedExpression returns XThrowExpression
 *     XExpressionOrVarDeclaration returns XThrowExpression
 *     XThrowExpression returns XThrowExpression
 *
 * Constraint:
 *     expression=XExpression
 */
protected void sequence_XThrowExpression(ISerializationContext context, XThrowExpression semanticObject) {
	if (errorAcceptor != null) {
		if (transientValues.isValueTransient(semanticObject, XbasePackage.Literals.XTHROW_EXPRESSION__EXPRESSION) == ValueTransient.YES)
			errorAcceptor.accept(diagnosticProvider.createFeatureValueMissing(semanticObject, XbasePackage.Literals.XTHROW_EXPRESSION__EXPRESSION));
	}
	SequenceFeeder feeder = createSequencerFeeder(context, semanticObject);
	feeder.accept(grammarAccess.getXThrowExpressionAccess().getExpressionXExpressionParserRuleCall_2_0(), semanticObject.getExpression());
	feeder.finish();
}
 
Example #28
Source File: SARLOperationHelper.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Test if the given expression has side effects.
 *
 * @param expression the expression.
 * @param context the list of context expressions.
 * @return {@code true} if the expression has side effects.
 */
@SuppressWarnings("static-method")
protected Boolean _hasSideEffects(XThrowExpression expression, ISideEffectContext context) {
	return true;
}
 
Example #29
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Generate the given object.
 *
 * @param throwStatement the throw statement.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the statement.
 */
protected XExpression _generate(XThrowExpression throwStatement, IAppendable it, IExtraLanguageGeneratorContext context) {
	it.append("raise "); //$NON-NLS-1$
	generate(throwStatement.getExpression(), it, context);
	return throwStatement;
}