org.eclipse.xtext.xbase.XListLiteral Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XListLiteral. 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: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
private boolean isEnumExpectationInAnnotationValue(final XFeatureCall it, final JvmType expectedRawType) {
  if (((expectedRawType instanceof JvmEnumerationType) || ((expectedRawType instanceof JvmArrayType) && (((JvmArrayType) expectedRawType).getComponentType() instanceof JvmEnumerationType)))) {
    EObject container = it.eContainer();
    if ((container instanceof XAnnotationElementValuePair)) {
      return true;
    }
    if ((container instanceof XAnnotation)) {
      return true;
    }
    if ((container instanceof XListLiteral)) {
      container = ((XListLiteral)container).eContainer();
      if ((container instanceof XAnnotationElementValuePair)) {
        return true;
      }
      if ((container instanceof XAnnotation)) {
        return true;
      }
    }
  }
  return false;
}
 
Example #2
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected Object _doEvaluate(XListLiteral literal, IEvaluationContext context, CancelIndicator indicator) {
	IResolvedTypes resolveTypes = typeResolver.resolveTypes(literal);
	LightweightTypeReference type = resolveTypes.getActualType(literal);
	List<Object> list = newArrayList();
	for(XExpression element: literal.getElements()) {
		if (indicator.isCanceled())
			throw new InterpreterCanceledException();
		list.add(internalEvaluate(element, context, indicator));
	}
	if(type != null && type.isArray()) {
		try {
			LightweightTypeReference componentType = type.getComponentType();
			return Conversions.unwrapArray(list, getJavaType(componentType.getType()));
		} catch (ClassNotFoundException e) {
		}
	}
	return Collections.unmodifiableList(list);
}
 
Example #3
Source File: JvmModelTests.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testEmptyListAsAnnotationValueDefault() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("annotation Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String[] bar = #[]");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    JvmMember _head = IterableExtensions.<JvmMember>head(this._iXtendJvmAssociations.getInferredAnnotationType(this.annotationType(_builder.toString())).getMembers());
    final JvmOperation inferred = ((JvmOperation) _head);
    Assert.assertEquals("java.lang.String[]", inferred.getReturnType().getIdentifier());
    JvmAnnotationValue _defaultValue = inferred.getDefaultValue();
    Assert.assertTrue((_defaultValue instanceof JvmCustomAnnotationValue));
    JvmAnnotationValue _defaultValue_1 = inferred.getDefaultValue();
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _defaultValue_1).getValues());
    Assert.assertTrue((_head_1 instanceof XListLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #4
Source File: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected Object translateAnnotationValue(final XExpression value, final JvmTypeReference expectedType) {
  Object _xblockexpression = null;
  {
    if ((value instanceof XAnnotation)) {
      return this.translateAnnotation(((XAnnotation)value));
    }
    if ((value instanceof XListLiteral)) {
      final Iterable<XAnnotation> annotations = Iterables.<XAnnotation>filter(((XListLiteral)value).getElements(), XAnnotation.class);
      int _size = ((XListLiteral)value).getElements().size();
      int _size_1 = IterableExtensions.size(annotations);
      boolean _equals = (_size == _size_1);
      if (_equals) {
        final ArrayList<AnnotationReference> annotationReferences = CollectionLiterals.<AnnotationReference>newArrayList();
        for (final XAnnotation annotation : annotations) {
          AnnotationReference _translateAnnotation = this.translateAnnotation(annotation);
          annotationReferences.add(_translateAnnotation);
        }
        final AnnotationReference[] result = ((AnnotationReference[])Conversions.unwrapArray(annotationReferences, AnnotationReference.class));
        return result;
      }
    }
    _xblockexpression = this.translate(this.evaluate(value, expectedType));
  }
  return _xblockexpression;
}
 
Example #5
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void computeArrayLiteralType(XListLiteral literal, LightweightTypeReference expectedArrayType, ITypeExpectation expectation,
		ITypeComputationState state) {
	LightweightTypeReference elementTypeExpectation = getElementOrComponentType(expectedArrayType, state);
	int allFlags = 0;
	for(XExpression element: literal.getElements()) {
		ITypeComputationResult elementTypeResult = computeTypes(element, elementTypeExpectation, state);
		deferredBindTypeArgument(elementTypeExpectation, elementTypeResult.getActualExpressionType(), state);
		allFlags |= elementTypeResult.getCheckedConformanceFlags();
	}
	if ((allFlags & ConformanceFlags.INCOMPATIBLE) != 0) {
		allFlags &= (~ConformanceFlags.SUCCESS);
		allFlags |= ConformanceFlags.SEALED | ConformanceFlags.CHECKED | ConformanceFlags.PROPAGATED_TYPE;
		expectation.acceptActualType(expectedArrayType, allFlags);
	} else if ((allFlags & ConformanceFlags.SUCCESS) != 0) {
		allFlags |= ConformanceFlags.SEALED | ConformanceFlags.CHECKED;
		expectation.acceptActualType(expectedArrayType, allFlags);
	} else {
		expectation.acceptActualType(expectedArrayType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.SEALED);
	}
}
 
Example #6
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void computeType(XListLiteral literal, JvmGenericType listType, ITypeExpectation expectation,
		ITypeComputationState state) {
	LightweightTypeReference elementTypeExpectation = null;
	final LightweightTypeReference expectedType = expectation.getExpectedType();
	if(expectedType != null) {
		if(expectedType.isArray()) {
			computeArrayLiteralType(literal, expectedType, expectation, state);
			return; 
		} 
		elementTypeExpectation = getElementOrComponentType(expectedType, state);
	}
	List<LightweightTypeReference> listTypeCandidates = computeCollectionTypeCandidates(literal, listType, elementTypeExpectation, state);
	LightweightTypeReference commonListType = getCommonSuperType(listTypeCandidates, state);
	if (commonListType != null) {
		LightweightTypeReference commonElementType = getElementOrComponentType(commonListType, state);
		ITypeReferenceOwner owner = state.getReferenceOwner();
		commonElementType = normalizeElementType(commonElementType, expectedType, owner);
		if (expectedType != null) {
			commonListType = createCollectionTypeReference(listType, commonElementType, expectedType, owner);
		}
		expectation.acceptActualType(commonListType, ConformanceFlags.UNCHECKED);
		refineElementTypeExpectation(literal, commonElementType, state);
	} else {
		setUnboundCollectionType(literal, listType, expectation, elementTypeExpectation, state);
	}
}
 
Example #7
Source File: CheckCompiler.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void appendImmutableCollectionExpression(final XCollectionLiteral literal, final ITreeAppendable b, final String collectionsMethod, final Class<?> guavaHelper, final String guavaHelperMethod) {
  // This is a work-around for a bug in the xbase compiler, which always constructs empty list literals #[] as List<Object>,
  // which then cannot be assigned (without cast) to any typed list. Note that this is not a problem in check; it also occurs
  // in plain xtend.
  if (literal.getElements().isEmpty()) {
    JvmType collectionsClass = findKnownTopLevelType(Collections.class, literal);
    if (collectionsClass != null) {
      if (literal instanceof XListLiteral) {
        b.append(collectionsClass).append(".emptyList()");
        return;
      } else if (literal instanceof XSetLiteral) {
        b.append(collectionsClass).append(".emptySet()");
        return;
      }
    }
  }
  super.appendImmutableCollectionExpression(literal, b, collectionsMethod, guavaHelper, guavaHelperMethod);
}
 
Example #8
Source File: FormalParameterCheckBase.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Validates that all XNumberLiterals in this expression, which occurs on the right-hand side of a formal parameter
 * declaration/definition, have indeed integral values.
 * 
 * @param value
 *          to check
 * @param issueCode
 *          to issue if the validation fails
 */
protected void checkRightHandSideHasOnlyIntegralNumbers(final XExpression value, final String issueCode) {
  if (value != null) {
    List<XExpression> exprs = (value instanceof XListLiteral) ? ((XListLiteral) value).getElements() : Collections.singletonList(value);
    for (XExpression expr : exprs) {
      XExpression e = expr;
      while (e instanceof XUnaryOperation) {
        e = ((XUnaryOperation) e).getOperand();
      }
      if (e instanceof XNumberLiteral) {
        try {
          Integer.decode(((XNumberLiteral) e).getValue());
        } catch (NumberFormatException ex) {
          error("Only integral values as numbers are allowed in check parameters", expr, null, issueCode); // TODO: NLS
        }
      }
    }
  }
}
 
Example #9
Source File: CheckConfigurationPropertiesGenerator.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluate an expression (value of a ConfiguredParameter).
 *
 * @param newValue
 *          the new value
 * @return the string
 */
private String evaluateParameterValue(final XExpression newValue) {
  if (newValue instanceof XListLiteral) {
    return evaluateList((XListLiteral) newValue);
  } else {
    Object evaluationResult = interpreter.evaluate(newValue).getResult();
    if (evaluationResult instanceof Boolean) {
      return ((Boolean) evaluationResult).toString();
    } else if (evaluationResult instanceof Integer) {
      return ((Integer) evaluationResult).toString();
    } else if (evaluationResult instanceof String) {
      // Note: control characters are no problem; java.util.Properties is smart enough to encode/decode those.
      return (String) evaluationResult;
    }
  }
  return null;
}
 
Example #10
Source File: CheckConfigurationPropertiesGenerator.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Evaluates a list.
 *
 * @param list
 *          to evaluate
 * @return the evaluated list.
 */
private String evaluateList(final XListLiteral list) {
  Iterable<Object> evaluated = Iterables.transform(list.getElements(), new Function<XExpression, Object>() {
    @Override
    public Object apply(final XExpression expr) {
      return interpreter.evaluate(expr).getResult();
    }
  });
  Object first = Iterables.getFirst(evaluated, null);
  if (first != null) {
    if (first instanceof Boolean) {
      return CheckPreferencesHelper.marshalBooleans(Iterables.filter(evaluated, Boolean.class));
    } else if (first instanceof Integer) {
      return CheckPreferencesHelper.marshalIntegers(Iterables.filter(evaluated, Integer.class));
    } else if (first instanceof String) {
      return CheckPreferencesHelper.marshalStrings(Iterables.filter(evaluated, String.class));
    }
  }
  return null;
}
 
Example #11
Source File: PyExpressionGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the given object.
 *
 * @param literal the list literal.
 * @param it the target for the generated content.
 * @param context the context.
 * @return the literal.
 */
protected XExpression _generate(XListLiteral literal, IAppendable it, IExtraLanguageGeneratorContext context) {
	appendReturnIfExpectedReturnedExpression(it, context);
	it.append("["); //$NON-NLS-1$
	boolean first = true;
	for (final XExpression value : literal.getElements()) {
		if (first) {
			first = false;
		} else {
			it.append(", "); //$NON-NLS-1$
		}
		generate(value, it, context);
	}
	it.append("]"); //$NON-NLS-1$
	return literal;
}
 
Example #12
Source File: CollectionLiteralsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Entry point from the {@link XbaseTypeComputer}.
 */
public void computeType(XListLiteral literal, ITypeComputationState state) {
	JvmGenericType listType = findDeclaredType(List.class, state);
	if (listType == null) {
		handleCollectionTypeNotAvailable(literal, state, List.class);
		return;
	}
	for(ITypeExpectation expectation: state.getExpectations()) {
		computeType(literal, listType, expectation, state);
	}
}
 
Example #13
Source File: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public Object internalEvaluate(final XExpression it, final Context ctx) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, ctx);
  } else if (it instanceof XFeatureCall) {
    return _internalEvaluate((XFeatureCall)it, ctx);
  } else if (it instanceof XListLiteral) {
    return _internalEvaluate((XListLiteral)it, ctx);
  } else if (it instanceof XMemberFeatureCall) {
    return _internalEvaluate((XMemberFeatureCall)it, ctx);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, ctx);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, ctx);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, ctx);
  } else if (it instanceof XNumberLiteral) {
    return _internalEvaluate((XNumberLiteral)it, ctx);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, ctx);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, ctx);
  } else if (it instanceof XAnnotation) {
    return _internalEvaluate((XAnnotation)it, ctx);
  } else if (it != null) {
    return _internalEvaluate(it, ctx);
  } else if (it == null) {
    return _internalEvaluate((Void)null, ctx);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, ctx).toString());
  }
}
 
Example #14
Source File: AnnotationValueValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isValidAnnotationValue(XExpression expression) {
	if (expression instanceof XListLiteral) {
		return _isValidAnnotationValue((XListLiteral) expression);
	} else if (expression instanceof XAbstractFeatureCall) {
		return _isValidAnnotationValue((XAbstractFeatureCall) expression);
	} else if (expression instanceof XAnnotation) {
		return _isValidAnnotationValue((XAnnotation) expression);
	} else if (expression != null) {
		return _isValidAnnotationValue(expression);
	}
	return _isValidAnnotationValue((Void) null);
}
 
Example #15
Source File: XtendCompiler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected boolean canUseArrayInitializer(XListLiteral literal, ITreeAppendable appendable) {
	EStructuralFeature feature = literal.eContainingFeature();
	if (feature == XtendPackage.Literals.XTEND_FIELD__INITIAL_VALUE) {
		return canUseArrayInitializerImpl(literal, appendable);
	}
	return super.canUseArrayInitializer(literal, appendable);
}
 
Example #16
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean canUseArrayInitializer(XListLiteral literal, ITreeAppendable appendable) {
	if (literal.eContainingFeature() == XbasePackage.Literals.XVARIABLE_DECLARATION__RIGHT
		|| literal.eContainingFeature() == XAnnotationsPackage.Literals.XANNOTATION_ELEMENT_VALUE_PAIR__VALUE
		|| literal.eContainingFeature() == XAnnotationsPackage.Literals.XANNOTATION__VALUE
		) {
		return canUseArrayInitializerImpl(literal, appendable);
	}
	return false;
}
 
Example #17
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean canUseArrayInitializerImpl(XListLiteral literal, ITreeAppendable appendable) {
	for(XExpression element: literal.getElements()) {
		if (isVariableDeclarationRequired(element, appendable, true))
			return false;
	}
	return true;
}
 
Example #18
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 #19
Source File: JvmAnnotationReferencePrinter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String internalToString(Object obj) {
	if (obj instanceof XBinaryOperation) {
		return _internalToString((XBinaryOperation) obj);
	} else if (obj instanceof XFeatureCall) {
		return _internalToString((XFeatureCall) obj);
	} else if (obj instanceof XListLiteral) {
		return _internalToString((XListLiteral) obj);
	} else if (obj instanceof XMemberFeatureCall) {
		return _internalToString((XMemberFeatureCall) obj);
	} else if (obj instanceof XBooleanLiteral) {
		return _internalToString((XBooleanLiteral) obj);
	} else if (obj instanceof XNumberLiteral) {
		return _internalToString((XNumberLiteral) obj);
	} else if (obj instanceof XStringLiteral) {
		return _internalToString((XStringLiteral) obj);
	} else if (obj instanceof XTypeLiteral) {
		return _internalToString((XTypeLiteral) obj);
	} else if (obj instanceof XAnnotation) {
		return _internalToString((XAnnotation) obj);
	} else if (obj instanceof JvmAnnotationReference) {
		return _internalToString((JvmAnnotationReference) obj);
	} else if (obj instanceof JvmAnnotationValue) {
		return _internalToString((JvmAnnotationValue) obj);
	} else {
		return _internalToString(obj);
	}
}
 
Example #20
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 #21
Source File: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected Object _internalEvaluate(final XListLiteral it, final Context ctx) {
  try {
    JvmTypeReference _switchResult = null;
    JvmTypeReference _expectedType = ctx.getExpectedType();
    final JvmTypeReference exp = _expectedType;
    boolean _matched = false;
    if (exp instanceof JvmGenericArrayTypeReference) {
      _matched=true;
      _switchResult = ((JvmGenericArrayTypeReference)exp).getComponentType();
    }
    final JvmTypeReference expectedComponentType = _switchResult;
    final Function1<XExpression, Object> _function = (XExpression it_1) -> {
      return this.evaluate(it_1, ctx.cloneWithExpectation(expectedComponentType));
    };
    final List<Object> elements = ListExtensions.<XExpression, Object>map(it.getElements(), _function);
    Class<?> _xifexpression = null;
    if ((expectedComponentType != null)) {
      _xifexpression = this.getJavaType(expectedComponentType.getType(), ctx.getClassFinder());
    } else {
      Class<?> _xifexpression_1 = null;
      boolean _isEmpty = elements.isEmpty();
      boolean _not = (!_isEmpty);
      if (_not) {
        Class<?> _switchResult_1 = null;
        Class<?> _class = IterableExtensions.<Object>head(elements).getClass();
        final Class<?> cl = _class;
        boolean _matched_1 = false;
        if (Objects.equal(cl, Integer.class)) {
          _matched_1=true;
          _switchResult_1 = Integer.TYPE;
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Long.class)) {
            _matched_1=true;
            _switchResult_1 = Long.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Short.class)) {
            _matched_1=true;
            _switchResult_1 = Short.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Boolean.class)) {
            _matched_1=true;
            _switchResult_1 = Boolean.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Double.class)) {
            _matched_1=true;
            _switchResult_1 = Double.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Byte.class)) {
            _matched_1=true;
            _switchResult_1 = Byte.TYPE;
          }
        }
        if (!_matched_1) {
          if (Objects.equal(cl, Float.class)) {
            _matched_1=true;
            _switchResult_1 = Float.TYPE;
          }
        }
        if (!_matched_1) {
          _switchResult_1 = cl;
        }
        _xifexpression_1 = _switchResult_1;
      } else {
        _xifexpression_1 = Object.class;
      }
      _xifexpression = _xifexpression_1;
    }
    final Class<?> componentType = _xifexpression;
    return Conversions.unwrapArray(elements, componentType);
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #22
Source File: JvmAnnotationReferencePrinter.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected String _internalToString(XListLiteral listLiteral) {
	return listLiteral.getElements().stream().map(element -> internalToString(element)).collect(Collectors.joining(", ", "#[", "]"));
}
 
Example #23
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 #24
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 #25
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void _toJavaExpression(XListLiteral literal, ITreeAppendable b) {
	LightweightTypeReference literalType = batchTypeResolver.resolveTypes(literal).getActualType(literal);
	if (literalType == null) {
		b.append("error - couldn't compute type for literal : "+literal);
		return;
	} 
	if (literalType.isArray()) {
		LightweightTypeReference expectedType = batchTypeResolver.resolveTypes(literal).getExpectedType(literal);
		boolean skipTypeName = false;
		if (expectedType != null && expectedType.isArray()) {
			if (canUseArrayInitializer(literal, b)) {
				skipTypeName = true;
			}
		}
		if (!skipTypeName) {
			if (literalType instanceof CompoundTypeReference) {
				for (LightweightTypeReference c : literalType.getMultiTypeComponents()) {
					if (c.isArray()) {
						b.append("new ")
						.append(c.getType()) // append raw type since we cannot create generic arrays
						.append(" ");
						break;
					}
				}
			} else {
				b.append("new ")
					.append(literalType.getType()) // append raw type since we cannot create generic arrays
					.append(" ");
			}
		}
		if (literal.getElements().isEmpty()) {
			b.append("{}");
		} else {
			b.append("{ ");
			boolean isFirst = true;
			for(XExpression element: literal.getElements())  {
				if(!isFirst)
					b.append(", ");
				isFirst = false;
				internalToJavaExpression(element, b);
			}
			b.append(" }");
		}
		return;
	} else {
		appendImmutableCollectionExpression(literal, b, "unmodifiableList", CollectionLiterals.class, "newArrayList");
	}
}
 
Example #26
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature 
 */
protected void _toJavaStatement(XListLiteral literal, ITreeAppendable b, boolean isReferenced) {
	for(XExpression element: literal.getElements()) 
		internalToJavaStatement(element, b, true);
}
 
Example #27
Source File: AnnotationValueValidator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean _isValidAnnotationValue(XListLiteral expression) {
	return expression.getElements().isEmpty()
			|| Iterables.all(expression.getElements(), this::isValidAnnotationValue);
}
 
Example #28
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void _computeTypes(XListLiteral literal, ITypeComputationState state) {
	collectionLiterals.computeType(literal, state);
}
 
Example #29
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 #30
Source File: AbstractXbaseWithAnnotationsSemanticSequencer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Deprecated
protected void sequence_XAnnotationElementValue_XListLiteral(EObject context, XListLiteral semanticObject) {
	sequence_XAnnotationElementValue_XListLiteral(createContext(context, semanticObject), semanticObject);
}