Java Code Examples for org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#trace()

The following examples show how to use org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#trace() . 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: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void compileWithJvmConstructorCall(XBlockExpression obj, ITreeAppendable apendable) {
	EList<XExpression> expressions = obj.getExpressions();
	internalToJavaStatement(expressions.get(0), apendable.trace(obj, false), false);
	if (expressions.size() == 1) {
		return;
	}
	
	apendable.newLine().append("try {").increaseIndentation();
	
	ITreeAppendable b = apendable.trace(obj, false);
	for (int i = 1; i < expressions.size(); i++) {
		XExpression ex = expressions.get(i);
		internalToJavaStatement(ex, b, false);
	}
	
	generateCheckedExceptionHandling(apendable);
}
 
Example 2
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void generateParameter(final JvmFormalParameter it, final ITreeAppendable appendable, final boolean vararg, final GeneratorConfig config) {
  final ITreeAppendable tracedAppendable = appendable.trace(it);
  this.generateAnnotations(it.getAnnotations(), tracedAppendable, false, config);
  tracedAppendable.append("final ");
  if (vararg) {
    JvmTypeReference _parameterType = it.getParameterType();
    boolean _not = (!(_parameterType instanceof JvmGenericArrayTypeReference));
    if (_not) {
      tracedAppendable.append("/* Internal Error: Parameter was vararg but not an array type. */");
    } else {
      JvmTypeReference _parameterType_1 = it.getParameterType();
      this._errorSafeExtensions.serializeSafely(((JvmGenericArrayTypeReference) _parameterType_1).getComponentType(), "Object", tracedAppendable);
    }
    tracedAppendable.append("...");
  } else {
    this._errorSafeExtensions.serializeSafely(it.getParameterType(), "Object", tracedAppendable);
  }
  tracedAppendable.append(" ");
  final String name = tracedAppendable.declareVariable(it, this.makeJavaIdentifier(it.getSimpleName()));
  this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(name);
}
 
Example 3
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected ITreeAppendable _generateMember(final JvmConstructor it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    appendable.newLine();
    appendable.openScope();
    this.generateJavaDoc(it, appendable, config);
    final ITreeAppendable tracedAppendable = appendable.trace(it);
    this.generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
    this.generateModifier(it, tracedAppendable, config);
    this.generateTypeParameterDeclaration(it, tracedAppendable, config);
    this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(this.makeJavaIdentifier(it.getSimpleName()));
    tracedAppendable.append("(");
    this.generateParameters(it, tracedAppendable, config);
    tracedAppendable.append(")");
    this.generateThrowsClause(it, tracedAppendable, config);
    tracedAppendable.append(" ");
    this.generateExecutableBody(it, tracedAppendable, config);
    appendable.closeScope();
    _xblockexpression = appendable;
  }
  return _xblockexpression;
}
 
Example 4
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected ITreeAppendable _generateMember(final JvmField it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    appendable.newLine();
    this.generateJavaDoc(it, appendable, config);
    final ITreeAppendable tracedAppendable = appendable.trace(it);
    this.generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
    this.generateModifier(it, tracedAppendable, config);
    this._errorSafeExtensions.serializeSafely(it.getType(), "Object", tracedAppendable);
    tracedAppendable.append(" ");
    final String name = tracedAppendable.declareVariable(it, this.makeJavaIdentifier(it.getSimpleName()));
    this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(name);
    this.generateInitialization(it, tracedAppendable, config);
    _xblockexpression = tracedAppendable.append(";");
  }
  return _xblockexpression;
}
 
Example 5
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void generateAnnotationMethod(final JvmOperation it, final ITreeAppendable appendable, final GeneratorConfig config) {
  appendable.increaseIndentation().newLine();
  appendable.openScope();
  this.generateJavaDoc(it, appendable, config);
  final ITreeAppendable tracedAppendable = appendable.trace(it);
  this.generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
  this.generateModifier(it, tracedAppendable, config);
  this._errorSafeExtensions.serializeSafely(it.getReturnType(), "Object", tracedAppendable);
  tracedAppendable.append(" ");
  this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(this.makeJavaIdentifier(it.getSimpleName()));
  tracedAppendable.append("()");
  this.generateDefaultExpression(it, tracedAppendable, config);
  tracedAppendable.append(";");
  appendable.decreaseIndentation();
  appendable.closeScope();
}
 
Example 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected ITreeAppendable appendTypeArguments(XAbstractFeatureCall call, ITreeAppendable original) {
	if (!call.getTypeArguments().isEmpty()) {
		return super.appendTypeArguments(call, original);
	}
	ILocationData completeLocationData = getLocationWithTypeArguments(call);
	ITreeAppendable completeFeatureCallAppendable = completeLocationData != null ? original.trace(completeLocationData) : original;
	IResolvedTypes resolvedTypes = batchTypeResolver.resolveTypes(call);
	List<LightweightTypeReference> typeArguments = resolvedTypes.getActualTypeArguments(call);
	if (!typeArguments.isEmpty()) {
		for(LightweightTypeReference typeArgument: typeArguments) {
			if (typeArgument.isWildcard()) {
				return completeFeatureCallAppendable;
			}
		}
		completeFeatureCallAppendable.append("<");
		for (int i = 0; i < typeArguments.size(); i++) {
			if (i != 0) {
				completeFeatureCallAppendable.append(", ");
			}
			completeFeatureCallAppendable.append(typeArguments.get(i));
		}
		completeFeatureCallAppendable.append(">");
	}
	return completeFeatureCallAppendable;
}
 
Example 7
Source File: GenerateTestsMojo.java    From sarl with Apache License 2.0 5 votes vote down vote up
private void generateDynamicTests(ITreeAppendable parent, ImportManager importManager,
		File inputFile, List<DynamicValidationComponent> specificComponents) {
	int i = 0;
	for (final DynamicValidationComponent component : specificComponents) {
		getLog().debug(MessageFormat.format(Messages.GenerateTestsMojo_4,
				inputFile.getName(), component.functionName() + i));
		final String actionName = toActionName("dyn_" + component.functionName(), component, i);
		final String displayName = toTestDisplayName(Messages.GenerateTestsMojo_10, i, component);
		final ILocationData location = new LocationData(
				// Offset
				component.getOffsetInSourceFile(),
				// Length
				component.getLengthInSourceFile(),
				// Line number
				component.getLinenoInSourceFile(),
				// End line number
				component.getEndLinenoInSourceFile(),
				// Source URI
				null);
		final ITreeAppendable it = parent.trace(location);
		//
		it.append("@").append(Test.class).newLine();
		it.append("@").append(DisplayName.class).append("(\"").append(displayName).append("\")").newLine();
		it.append("@").append(Tag.class).append("(\"other\")").newLine();
		it.append("@").append(Tag.class).append("(\"other_").append(Integer.toString(i)).append("\")").newLine();
		it.append("public void ").append(actionName).append("() throws ")
			.append(Exception.class).append(" {").increaseIndentation().newLine();
		component.generateValidationCode(it);
		it.decreaseIndentation().newLine();
		it.append("}").newLine();
		//
		++i;
	}
}
 
Example 8
Source File: SARLJvmGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
protected ITreeAppendable _generateMember(JvmOperation it, ITreeAppendable appendable, GeneratorConfig config) {
	if (Utils.STATIC_CONSTRUCTOR_NAME.equals(it.getSimpleName())) {
		// The constructor name is not the same as the declaring type.
		// We assume that the constructor is a static constructor.
		return generateStaticConstructor(it, appendable, config);
	}
	// The code below is adapted from the code of the Xtend super type.
	appendable.newLine();
	appendable.openScope();
	generateJavaDoc(it, appendable, config);
	final ITreeAppendable tracedAppendable = appendable.trace(it);
	generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
	// Specific case: automatic generation
	if (this.operationHelper.isPureOperation(it)
			&& this.annotations.findAnnotation(it, Pure.class) == null) {
		tracedAppendable.append("@").append(Pure.class).newLine(); //$NON-NLS-1$
	}
	generateModifier(it, tracedAppendable, config);
	generateTypeParameterDeclaration(it, tracedAppendable, config);
	if (it.getReturnType() == null) {
		tracedAppendable.append("void"); //$NON-NLS-1$
	} else {
		this._errorSafeExtensions.serializeSafely(it.getReturnType(), Object.class.getSimpleName(), tracedAppendable);
	}
	tracedAppendable.append(" "); //$NON-NLS-1$
	this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(makeJavaIdentifier(it.getSimpleName()));
	tracedAppendable.append("("); //$NON-NLS-1$
	generateParameters(it, tracedAppendable, config);
	tracedAppendable.append(")"); //$NON-NLS-1$
	generateThrowsClause(it, tracedAppendable, config);
	if (it.isAbstract() || !hasBody(it)) {
		tracedAppendable.append(";"); //$NON-NLS-1$
	} else {
		tracedAppendable.append(" "); //$NON-NLS-1$
		generateExecutableBody(it, tracedAppendable, config);
	}
	appendable.closeScope();
	return appendable;
}
 
Example 9
Source File: SARLJvmGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate a static constructor from the given Jvm constructor.
 *
 * @param it the container of the code.
 * @param appendable the output.
 * @param config the generation configuration.
 * @return the appendable.
 */
protected ITreeAppendable generateStaticConstructor(JvmOperation it, ITreeAppendable appendable, GeneratorConfig config) {
	appendable.newLine();
	appendable.openScope();
	generateJavaDoc(it, appendable, config);
	final ITreeAppendable tracedAppendable = appendable.trace(it);
	tracedAppendable.append("static "); //$NON-NLS-1$
	generateExecutableBody(it, tracedAppendable, config);
	return appendable;
}
 
Example 10
Source File: TreeAppendableUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public ITreeAppendable traceSignificant(ITreeAppendable appendable, EObject source, boolean useForDebugging) {
	if (appendable instanceof TreeAppendable) {
		return ((TreeAppendable) appendable).trace(source,
				ILocationInFileProviderExtension.RegionDescription.SIGNIFICANT, useForDebugging);
	} else {
		ITextRegionWithLineInformation it = (ITextRegionWithLineInformation) locationProvider
				.getSignificantTextRegion(source);
		if (it != null && it != ITextRegion.EMPTY_REGION) {
			return appendable.trace(new LocationData(it.getOffset(), it.getLength(), it.getLineNumber(),
					it.getEndLineNumber(), null), useForDebugging);
		} else {
			return appendable;
		}
	}
}
 
Example 11
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ITreeAppendable _generateBody(final JvmGenericType it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    this.generateJavaDoc(it, appendable, config);
    final ITreeAppendable childAppendable = appendable.trace(it);
    this.generateAnnotations(it.getAnnotations(), childAppendable, true, config);
    this.generateModifier(it, childAppendable, config);
    boolean _isInterface = it.isInterface();
    if (_isInterface) {
      childAppendable.append("interface ");
    } else {
      childAppendable.append("class ");
    }
    this._treeAppendableUtil.traceSignificant(childAppendable, it).append(this.makeJavaIdentifier(it.getSimpleName()));
    this.generateTypeParameterDeclaration(it, childAppendable, config);
    boolean _isEmpty = it.getTypeParameters().isEmpty();
    if (_isEmpty) {
      childAppendable.append(" ");
    }
    this.generateExtendsClause(it, childAppendable, config);
    this.generateMembersInBody(it, childAppendable, config);
    ITreeAppendable _xifexpression = null;
    if (((!it.isAnonymous()) && (!(it.eContainer() instanceof JvmType)))) {
      _xifexpression = appendable.newLine();
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example 12
Source File: GenerateTestsMojo.java    From sarl with Apache License 2.0 5 votes vote down vote up
private void generateTestsForSuccessCode(ITreeAppendable parent, ImportManager importManager,
		File inputFile, List<ValidationComponent> successCompilationComponents) {
	int i = 0;
	for (final ValidationComponent component : successCompilationComponents) {
		getLog().debug(MessageFormat.format(Messages.GenerateTestsMojo_1,
				inputFile.getName(), component.getLinenoInSourceFile(), component.getCode()));
		final String actionName = toActionName("success", component, i); //$NON-NLS-1$
		final String displayName = toTestDisplayName(Messages.GenerateTestsMojo_7, i, component);
		final ILocationData location = new LocationData(
				// Offset
				component.getOffsetInSourceFile(),
				// Length
				component.getLengthInSourceFile(),
				// Line number
				component.getLinenoInSourceFile(),
				// End line number
				component.getEndLinenoInSourceFile(),
				// Source URI
				null);
		final ITreeAppendable it = parent.trace(location);
		//
		it.append("@").append(Test.class).newLine();
		it.append("@").append(DisplayName.class).append("(\"").append(displayName).append("\")").newLine();
		it.append("@").append(Tag.class).append("(\"success\")").newLine();
		it.append("@").append(Tag.class).append("(\"success_").append(Integer.toString(i)).append("\")").newLine();
		it.append("public void ").append(actionName).append("() throws ")
			.append(Exception.class).append(" {").increaseIndentation().newLine();
		it.append(List.class).append("<String> issues = getScriptExecutor().compile(")
			.append(str(component.getLinenoInSourceFile())).append(", \"")
			.append(str(component.getCode())).append("\");").newLine();
		it.append("assertNoIssue(").append(str(component.getLinenoInSourceFile()))
			.append(", issues);").decreaseIndentation().newLine();
		it.append("}").newLine();
		//
		++i;
	}
}
 
Example 13
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ITreeAppendable _generateMember(final JvmOperation it, final ITreeAppendable appendable, final GeneratorConfig config) {
  ITreeAppendable _xblockexpression = null;
  {
    appendable.newLine();
    appendable.openScope();
    this.generateJavaDoc(it, appendable, config);
    final ITreeAppendable tracedAppendable = appendable.trace(it);
    this.generateAnnotations(it.getAnnotations(), tracedAppendable, true, config);
    this.generateModifier(it, tracedAppendable, config);
    this.generateTypeParameterDeclaration(it, tracedAppendable, config);
    JvmTypeReference _returnType = it.getReturnType();
    boolean _tripleEquals = (_returnType == null);
    if (_tripleEquals) {
      tracedAppendable.append("void");
    } else {
      this._errorSafeExtensions.serializeSafely(it.getReturnType(), "Object", tracedAppendable);
    }
    tracedAppendable.append(" ");
    this._treeAppendableUtil.traceSignificant(tracedAppendable, it).append(this.makeJavaIdentifier(it.getSimpleName()));
    tracedAppendable.append("(");
    this.generateParameters(it, tracedAppendable, config);
    tracedAppendable.append(")");
    this.generateThrowsClause(it, tracedAppendable, config);
    if ((it.isAbstract() || (!this.hasBody(it)))) {
      tracedAppendable.append(";");
    } else {
      tracedAppendable.append(" ");
      this.generateExecutableBody(it, tracedAppendable, config);
    }
    appendable.closeScope();
    _xblockexpression = appendable;
  }
  return _xblockexpression;
}
 
Example 14
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _toJavaExpression(XBlockExpression expr, ITreeAppendable b) {
	if (expr.getExpressions().isEmpty()) {
		b.append("null");
		return;
	}
	if (expr.getExpressions().size()==1) {
		// conversion was already performed for single expression blocks
		internalToConvertedExpression(expr.getExpressions().get(0), b, null);
		return;
	}
	b = b.trace(expr, false);
	b.append(getVarName(expr, b));
}
 
Example 15
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _toJavaStatement(XBlockExpression expr, ITreeAppendable b, boolean isReferenced) {
	b = b.trace(expr, false);
	if (expr.getExpressions().isEmpty())
		return;
	if (expr.getExpressions().size()==1) {
		internalToJavaStatement(expr.getExpressions().get(0), b, isReferenced);
		return;
	}
	if (isReferenced)
		declareSyntheticVariable(expr, b);
	boolean needsBraces = isReferenced || !bracesAreAddedByOuterStructure(expr);
	if (needsBraces) {
		b.newLine().append("{").increaseIndentation();
		b.openPseudoScope();
	}
	final EList<XExpression> expressions = expr.getExpressions();
	for (int i = 0; i < expressions.size(); i++) {
		XExpression ex = expressions.get(i);
		if (i < expressions.size() - 1) {
			internalToJavaStatement(ex, b, false);
		} else {
			internalToJavaStatement(ex, b, isReferenced);
			if (isReferenced) {
				b.newLine().append(getVarName(expr, b)).append(" = ");
				internalToConvertedExpression(ex, b, getLightweightType(expr));
				b.append(";");
			}
		}
	}
	if (needsBraces) {
		b.closeScope();
		b.decreaseIndentation().newLine().append("}");
	}
}
 
Example 16
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void toJavaWhileStatement(XBasicForLoopExpression expr, ITreeAppendable b, boolean isReferenced) {
	ITreeAppendable loopAppendable = b.trace(expr);
	
	boolean needBraces = !bracesAreAddedByOuterStructure(expr);
	if (needBraces) {
		loopAppendable.newLine().increaseIndentation().append("{");
		loopAppendable.openPseudoScope();
	}
	
	EList<XExpression> initExpressions = expr.getInitExpressions();
	for (int i = 0; i < initExpressions.size(); i++) {
		XExpression initExpression = initExpressions.get(i);
		if (i < initExpressions.size() - 1) {
			internalToJavaStatement(initExpression, loopAppendable, false);
		} else {
			internalToJavaStatement(initExpression, loopAppendable, isReferenced);
			if (isReferenced) {
				loopAppendable.newLine().append(getVarName(expr, loopAppendable)).append(" = (");
				internalToConvertedExpression(initExpression, loopAppendable, getLightweightType(expr));
				loopAppendable.append(");");
			}
		}
	}

	final String varName = loopAppendable.declareSyntheticVariable(expr, "_while");
	
	XExpression expression = expr.getExpression();
	if (expression != null) {
		internalToJavaStatement(expression, loopAppendable, true);
		loopAppendable.newLine().append("boolean ").append(varName).append(" = ");
		internalToJavaExpression(expression, loopAppendable);
		loopAppendable.append(";");
	} else {
		loopAppendable.newLine().append("boolean ").append(varName).append(" = true;");
	}
	loopAppendable.newLine();
	loopAppendable.append("while (");
	loopAppendable.append(varName);
	loopAppendable.append(") {").increaseIndentation();
	loopAppendable.openPseudoScope();
	
	XExpression eachExpression = expr.getEachExpression();
	internalToJavaStatement(eachExpression, loopAppendable, false);
	
	EList<XExpression> updateExpressions = expr.getUpdateExpressions();
	if (!updateExpressions.isEmpty()) {
		for (XExpression updateExpression : updateExpressions) {
			internalToJavaStatement(updateExpression, loopAppendable, false);
		}
	}
	
	if (!isEarlyExit(eachExpression)) {
		if (expression != null) {
			internalToJavaStatement(expression, loopAppendable, true);
			loopAppendable.newLine().append(varName).append(" = ");
			internalToJavaExpression(expression, loopAppendable);
			loopAppendable.append(";");
		} else {
			loopAppendable.newLine().append(varName).append(" = true;");
		}
	}
	
	loopAppendable.closeScope();
	loopAppendable.decreaseIndentation().newLine().append("}");
	
	if (needBraces) {
		loopAppendable.closeScope();
		loopAppendable.decreaseIndentation().newLine().append("}");
	}
}
 
Example 17
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected ITreeAppendable appendOpenIfStatement(XCasePart casePart, ITreeAppendable b, String matchedVariable, String variableName, XSwitchExpressionCompilationState state) {
	ITreeAppendable caseAppendable = b.trace(casePart, true);
	if (state.caseNeedsIfNotMatchedCheck()) {
		caseAppendable.newLine().append("if (!").append(matchedVariable).append(") {");
		caseAppendable.increaseIndentation();
	}
	JvmTypeReference typeGuard = casePart.getTypeGuard();
	if (typeGuard != null) {
		ITreeAppendable typeGuardAppendable = caseAppendable.trace(typeGuard, true);
		typeGuardAppendable.newLine().append("if (");
		if (typeGuard instanceof JvmSynonymTypeReference) {
			Iterator<JvmTypeReference> iter = ((JvmSynonymTypeReference) typeGuard).getReferences().iterator();
			while(iter.hasNext()) {
				typeGuardAppendable.append(variableName);
				typeGuardAppendable.append(" instanceof ");
				typeGuardAppendable.trace(typeGuard).append(iter.next().getType());
				if (iter.hasNext()) {
					typeGuardAppendable.append(" || ");
				}
			}
		} else {
			typeGuardAppendable.append(variableName);
			typeGuardAppendable.append(" instanceof ");
			typeGuardAppendable.trace(typeGuard).append(typeGuard.getType());
		}
		typeGuardAppendable.append(") {");
		typeGuardAppendable.increaseIndentation();
		typeGuardAppendable.openPseudoScope();
	}
	if (casePart.getCase() != null) {
		ITreeAppendable conditionAppendable = caseAppendable.trace(casePart.getCase(), true);
		internalToJavaStatement(casePart.getCase(), conditionAppendable, true);
		conditionAppendable.newLine().append("if (");
		LightweightTypeReference convertedType = getLightweightType(casePart.getCase());
		if (convertedType.isType(Boolean.TYPE) || convertedType.isType(Boolean.class)) {
			internalToJavaExpression(casePart.getCase(), conditionAppendable);
		} else {
			JvmType objectsType = findKnownType(Objects.class, casePart);
			if (objectsType != null) {
				conditionAppendable.append(objectsType);
				conditionAppendable.append(".equal(").append(variableName).append(", ");
				internalToJavaExpression(casePart.getCase(), conditionAppendable);
				conditionAppendable.append(")");
			} else {
				// use ObjectExtensions rather than a == b || a != null && a.equals(b) since
				// that won't work with primitive types and other incompatible conditional operands
				conditionAppendable.append(ObjectExtensions.class);
				conditionAppendable.append(".operator_equals(").append(variableName).append(", ");
				internalToJavaExpression(casePart.getCase(), conditionAppendable);
				conditionAppendable.append(")");
			}
		}
		conditionAppendable.append(")");
		caseAppendable.append(" {");
		caseAppendable.increaseIndentation();
	}
	// set matched to true
	return caseAppendable.newLine().append(matchedVariable).append("=true;");
}
 
Example 18
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected final void internalToJavaStatement(XExpression obj, ITreeAppendable builder, boolean isReferenced) {
	final ITreeAppendable trace = builder.trace(obj, true);
	doInternalToJavaStatement(obj, trace, isReferenced);
}
 
Example 19
Source File: GenerateTestsMojo.java    From sarl with Apache License 2.0 4 votes vote down vote up
private void generateTestsForFacts(ITreeAppendable parent, ImportManager importManager,
		File inputFile, List<ValidationComponent> factualComponents) {
	int i = 0;
	for (final ValidationComponent component : factualComponents) {
		getLog().debug(MessageFormat.format(Messages.GenerateTestsMojo_3,
				inputFile.getName(), component.getLinenoInSourceFile(), component.getCode()));
		final String actionName = toActionName("fact", component, i); //$NON-NLS-1$
		final String displayName = toTestDisplayName(Messages.GenerateTestsMojo_9, i, component);
		final ILocationData location = new LocationData(
				// Offset
				component.getOffsetInSourceFile(),
				// Length
				component.getLengthInSourceFile(),
				// Line number
				component.getLinenoInSourceFile(),
				// End line number
				component.getEndLinenoInSourceFile(),
				// Source URI
				null);
		final ITreeAppendable it = parent.trace(location);
		//
		it.append("@").append(Test.class).newLine();
		it.append("@").append(DisplayName.class).append("(\"").append(displayName).append("\")").newLine();
		it.append("@").append(Tag.class).append("(\"fact\")").newLine();
		it.append("@").append(Tag.class).append("(\"fact_").append(Integer.toString(i)).append("\")").newLine();
		it.append("public void ").append(actionName).append("() throws ")
			.append(Exception.class).append(" {").increaseIndentation().newLine();
		it.append("final String expected = ").append(Utils.class).append(".dump(")
			.append(Boolean.class).append(".TRUE, false) + \"\\nOR\\nObject {\\n}\\n\";").newLine();
		it.append("Object result;").newLine();
		it.append("try {").increaseIndentation().newLine();
		it.append("result = getScriptExecutor().execute(").append(str(component.getLinenoInSourceFile()))
			.append(", \"").append(str(component.getCode())).append("\");").decreaseIndentation().newLine();
		it.append("} catch (").append(Throwable.class).append(" exception) {").increaseIndentation().newLine();
		it.append("throw new ").append(AssertionFailedError.class)
			.append("(exception.getLocalizedMessage() + \" [line: ")
			.append(str(component.getLinenoInSourceFile())).append("]\", expected, ")
			.append(Throwables.class).append(".getStackTraceAsString(exception));").decreaseIndentation().newLine();
		it.append("}").newLine();
		it.append("if (result instanceof ").append(Boolean.class).append(") {").increaseIndentation().newLine();
		it.append("boolean boolResult = ((").append(Boolean.class).append(") result).booleanValue();").newLine();
		it.append("if (!boolResult) {").increaseIndentation().newLine();
		it.append("throw new ").append(AssertionFailedError.class)
			.append("(\"Invalid expression result [line: ").append(str(component.getLinenoInSourceFile()))
			.append("]\", expected, ").append(Utils.class).append(".dump(result, false));").decreaseIndentation().newLine();
		it.append("}").decreaseIndentation().newLine();
		it.append("} else if (result == null || result instanceof ").append(Exception.class)
			.append(") {").increaseIndentation().newLine();
		it.append("throw new ").append(AssertionFailedError.class)
			.append("(\"Invalid expression result [line: ").append(str(component.getLinenoInSourceFile()))
			.append("]\", expected, ").append(Utils.class).append(".dump(result, false));").decreaseIndentation().newLine();
		it.append("}").decreaseIndentation().newLine();
		it.append("}").newLine();
		//
		++i;
	}
}
 
Example 20
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendCatchClause(XCatchClause catchClause, boolean parentIsReferenced, String parentVariable,
		ITreeAppendable appendable) {
	JvmTypeReference type = catchClause.getDeclaredParam().getParameterType();
	final String declaredParamName = makeJavaIdentifier(catchClause.getDeclaredParam().getName());
	final String name = appendable.declareVariable(catchClause.getDeclaredParam(), declaredParamName);
	appendable.append("if (");
	JvmTypeReference typeToUse = type;
	if (type instanceof JvmSynonymTypeReference) {
		List<JvmTypeReference> references = ((JvmSynonymTypeReference) type).getReferences();
		Iterator<JvmTypeReference> iter = references.iterator();
		while(iter.hasNext()) {
			appendable.append(parentVariable).append(" instanceof ");
			serialize(iter.next(), catchClause, appendable);
			if (iter.hasNext()) {
				appendable.append(" || ");
			}
		}
		typeToUse = resolveSynonymType((JvmSynonymTypeReference) type, catchClause);
	} else {
		appendable.append(parentVariable).append(" instanceof ");
		serialize(type, catchClause, appendable);			
	}
	appendable.append(") ").append("{");
	appendable.increaseIndentation();
	ITreeAppendable withDebugging = appendable.trace(catchClause, true);
	if (!XbaseUsageCrossReferencer.find(catchClause.getDeclaredParam(), catchClause.getExpression()).isEmpty()) {
		ITreeAppendable parameterAppendable = withDebugging.trace(catchClause.getDeclaredParam());
		appendCatchClauseParameter(catchClause, typeToUse, name, parameterAppendable.newLine());
		withDebugging.append(" = (");
		serialize(typeToUse, catchClause, withDebugging);
		withDebugging.append(")").append(parentVariable).append(";");
	}
	final boolean canBeReferenced = parentIsReferenced && ! isPrimitiveVoid(catchClause.getExpression());
	internalToJavaStatement(catchClause.getExpression(), withDebugging, canBeReferenced);
	if (canBeReferenced) {
		appendable.newLine().append(getVarName(catchClause.eContainer(), appendable)).append(" = ");
		internalToConvertedExpression(catchClause.getExpression(), appendable, getLightweightType((XExpression) catchClause.eContainer()));
		appendable.append(";");
	}
	closeBlock(appendable);
}