Java Code Examples for org.eclipse.xtext.common.types.JvmField#getType()

The following examples show how to use org.eclipse.xtext.common.types.JvmField#getType() . 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: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected Object _assignValueTo(JvmField jvmField, XAbstractFeatureCall assignment, Object value,
		IEvaluationContext context, CancelIndicator indicator) {
	Object receiver = getReceiver(assignment, context, indicator);
	Field field = javaReflectAccess.getField(jvmField);
	try {
		if (field == null) {
			throw new NoSuchFieldException("Could not find field " + jvmField.getIdentifier());
		}
		if (!Modifier.isStatic(field.getModifiers()) && receiver == null)
			throw new EvaluationException(new NullPointerException("Cannot assign value to field: "
					+ jvmField.getIdentifier() + " on null instance"));
		JvmTypeReference type = jvmField.getType();
		Object coerced = coerceArgumentType(value, type);
		field.setAccessible(true);
		field.set(receiver, coerced);
		return value;
	} catch (Exception e) {
		throw new IllegalStateException("Could not access field: " + jvmField.getIdentifier()
				+ " on instance: " + receiver, e);
	}
}
 
Example 2
Source File: XtendReentrantTypeResolver.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Initializes the type inference strategy for the cache field for create extensions.
 */
@Override
protected void _doPrepare(ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, JvmField field,
		Map<JvmIdentifiableElement, ResolvedTypes> resolvedTypesByContext) {
	JvmTypeReference knownType = field.getType();
	if (InferredTypeIndicator.isInferred(knownType)) {
		XComputedTypeReference castedKnownType = (XComputedTypeReference) knownType;
		EObject sourceElement = associations.getPrimarySourceElement(field);
		if (sourceElement instanceof XtendFunction) {
			XtendFunction function = (XtendFunction) sourceElement;
			if (function.getCreateExtensionInfo() != null) {
				JvmOperation operation = associations.getDirectlyInferredOperation(function);
				if (operation != null) {
					declareTypeParameters(resolvedTypes, field, resolvedTypesByContext);
					XComputedTypeReference fieldType = getServices().getXtypeFactory().createXComputedTypeReference();
					fieldType.setTypeProvider(new CreateCacheFieldTypeReferenceProvider(operation, resolvedTypes, featureScopeSession));
					castedKnownType.setEquivalent(fieldType);
					return;
				}
			}
		}
	}
	super._doPrepare(resolvedTypes, featureScopeSession, field, resolvedTypesByContext);
	doPrepareLocalTypes(resolvedTypesByContext.get(field), featureScopeSession, field, resolvedTypesByContext);
}
 
Example 3
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkLocalUsageOfDeclaredFields(XtendField field){
	if(doCheckValidMemberName(field) && !isIgnored(UNUSED_PRIVATE_MEMBER)) {
		JvmField jvmField = associations.getJvmField(field);
		if (jvmField == null || jvmField.getVisibility() != JvmVisibility.PRIVATE || jvmField.eContainer() == null)
			return;
		if (isLocallyUsed(jvmField, getOutermostType(field))) 
			return;
		String message;
		if(field.isExtension()) {
			if(field.getName() == null && jvmField.getType() != null)
				message = "The extension " + jvmField.getType().getIdentifier() 
					+ " is not used in " + getDeclaratorName(jvmField);
			else
				message = "The extension " + getDeclaratorName(jvmField) + "."
						+ jvmField.getSimpleName() + " is not used";
		} else {
			message = "The value of the field " + getDeclaratorName(jvmField) + "."
				+ jvmField.getSimpleName() + " is not used";
		}
		addIssueToState(UNUSED_PRIVATE_MEMBER, message, XtendPackage.Literals.XTEND_FIELD__NAME);
	}
}
 
Example 4
Source File: XtendLabelProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected JvmTypeReference getDisplayedType(final XtendField field) {
  Object _xblockexpression = null;
  {
    final JvmField jvmField = this._iXtendJvmAssociations.getJvmField(field);
    if ((jvmField != null)) {
      return jvmField.getType();
    } else {
      final Iterator<EObject> i = this._iXtendJvmAssociations.getJvmElements(field).iterator();
      boolean _hasNext = i.hasNext();
      if (_hasNext) {
        final EObject next = i.next();
        if ((next instanceof JvmOperation)) {
          return ((JvmOperation)next).getReturnType();
        }
      }
    }
    _xblockexpression = null;
  }
  return ((JvmTypeReference)_xblockexpression);
}
 
Example 5
Source File: XtendHoverSignatureProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected String _signature(XtendField field, boolean typeAtEnd) {
	if (field.getName() == null && field.isExtension())
		return getTypeName(field.getType());
	JvmField jvmField = associations.getJvmField(field);
	if (jvmField != null) {
		JvmTypeReference type = jvmField.getType();
		if (type != null) {
			if (field.getName() == null)
				return getTypeName(type);
			return getTypeName(type) + " " + field.getName();
		}
	}
	if (field.getName() == null)
		return "";
	return field.getName();
}
 
Example 6
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFields_innerFields_01() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, Fields.Inner.class, "innerFields");
	JvmGenericType innerType = (JvmGenericType) getTypeProvider().findTypeByName(Fields.Inner.class.getName());
	assertSame(innerType, field.getDeclaringType());
	assertSame(type, innerType.getDeclaringType());
	assertEquals(JvmVisibility.PUBLIC, field.getVisibility());
	JvmTypeReference fieldType = field.getType();
	assertEquals(typeName + "<java.lang.String>", fieldType.getIdentifier());
	assertTrue(field.getType() instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterizedFieldType = (JvmParameterizedTypeReference) fieldType;
	assertSame(type, parameterizedFieldType.getType());
}
 
Example 7
Source File: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected Object evaluateField(final XAbstractFeatureCall call, final JvmField field, final Context context) {
  if ((field.isSetConstant() || (field.eResource() instanceof TypeResource))) {
    boolean _isConstant = field.isConstant();
    if (_isConstant) {
      return field.getConstantValue();
    } else {
      String _simpleName = field.getDeclaringType().getSimpleName();
      String _plus = ("Field " + _simpleName);
      String _plus_1 = (_plus + ".");
      String _simpleName_1 = field.getSimpleName();
      String _plus_2 = (_plus_1 + _simpleName_1);
      String _plus_3 = (_plus_2 + " is not a constant");
      throw new ConstantExpressionEvaluationException(_plus_3);
    }
  }
  final XExpression expression = this.containerProvider.getAssociatedExpression(field);
  boolean _contains = context.getAlreadyEvaluating().contains(expression);
  if (_contains) {
    throw new ConstantExpressionEvaluationException("Endless recursive evaluation detected.");
  }
  try {
    final Map<String, JvmIdentifiableElement> visibleFeatures = this.findVisibleFeatures(expression);
    JvmTypeReference _type = field.getType();
    ClassFinder _classFinder = context.getClassFinder();
    Set<XExpression> _alreadyEvaluating = context.getAlreadyEvaluating();
    final Context ctx = new Context(_type, _classFinder, visibleFeatures, _alreadyEvaluating);
    return this.evaluate(expression, ctx);
  } catch (final Throwable _t) {
    if (_t instanceof ConstantExpressionEvaluationException) {
      final ConstantExpressionEvaluationException e = (ConstantExpressionEvaluationException)_t;
      throw new StackedConstantExpressionEvaluationException(call, field, e);
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
}
 
Example 8
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkNonRawTypeInferred(XtendField field) {
	if (field.getType() == null) {
		JvmField jvmField = associations.getJvmField(field);
		// field could have been removed by AA, thus the resource is possibly null
		if (jvmField != null && jvmField.eResource() != null) {
			JvmTypeReference fieldType = jvmField.getType();
			validateInferredType(fieldType, field, "The inferred field type ", XTEND_FIELD__NAME);
		}
	}
}
 
Example 9
Source File: UIStringsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testReferenceToString_4() throws Exception {
	XtendFile file = file("class Foo { var test }");
	assertFalse(validationTestHelper.validate(file).isEmpty());
	XtendClass clazz = (XtendClass) file.getXtendTypes().get(0);
	XtendField field = (XtendField) clazz.getMembers().get(0);
	JvmField jvmField = associations.getJvmField(field);
	JvmTypeReference reference = jvmField.getType();
	assertNotNull(reference);
	assertNotNull(reference.getType());
	assertFalse(reference.getType().eIsProxy());
	assertNotNull(reference.eResource());
	assertEquals("Object", this.uiStrings.referenceToString(reference, "the-default-label"));
}
 
Example 10
Source File: XbaseDeclarativeHoverSignatureProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String _signature(JvmField jvmField, boolean typeAtEnd) {
	JvmTypeReference type = jvmField.getType();
	if (type != null) {
		String signature = jvmField.getSimpleName();
		if (typeAtEnd)
			return signature + " : " + type.getSimpleName();
		return type.getSimpleName() + " " + enrichWithDeclarator(signature, jvmField);
	}
	return "";
}
 
Example 11
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFields_innerFields_01() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, Fields.Inner.class, "innerFields");
	JvmGenericType innerType = (JvmGenericType) getTypeProvider().findTypeByName(Fields.Inner.class.getName());
	assertSame(innerType, field.getDeclaringType());
	assertSame(type, innerType.getDeclaringType());
	assertEquals(JvmVisibility.PUBLIC, field.getVisibility());
	JvmTypeReference fieldType = field.getType();
	assertEquals(typeName + "<java.lang.String>", fieldType.getIdentifier());
	assertTrue(field.getType() instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterizedFieldType = (JvmParameterizedTypeReference) fieldType;
	assertSame(type, parameterizedFieldType.getType());
}
 
Example 12
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFields_defaultListT_01() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, Fields.class, "defaultListT");
	assertSame(type, field.getDeclaringType());
	assertEquals(JvmVisibility.DEFAULT, field.getVisibility());
	JvmTypeReference fieldType = field.getType();
	assertEquals("java.util.List<T>", fieldType.getIdentifier());
	assertTrue(fieldType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterizedFieldType = (JvmParameterizedTypeReference) fieldType;
	JvmTypeReference refTypeArg = parameterizedFieldType.getArguments().get(0);
	assertSame(type.getTypeParameters().get(0), refTypeArg.getType());
}
 
Example 13
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFields_innerFields_01() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, Fields.Inner.class, "innerFields");
	JvmGenericType innerType = (JvmGenericType) getTypeProvider().findTypeByName(Fields.Inner.class.getName());
	assertSame(innerType, field.getDeclaringType());
	assertSame(type, innerType.getDeclaringType());
	assertEquals(JvmVisibility.PUBLIC, field.getVisibility());
	JvmTypeReference fieldType = field.getType();
	assertEquals(typeName + "<java.lang.String>", fieldType.getIdentifier());
	assertTrue(field.getType() instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterizedFieldType = (JvmParameterizedTypeReference) fieldType;
	assertSame(type, parameterizedFieldType.getType());
}
 
Example 14
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFields_defaultListT_01() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, Fields.class, "defaultListT");
	assertSame(type, field.getDeclaringType());
	assertEquals(JvmVisibility.DEFAULT, field.getVisibility());
	JvmTypeReference fieldType = field.getType();
	assertEquals("java.util.List<T>", fieldType.getIdentifier());
	assertTrue(fieldType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterizedFieldType = (JvmParameterizedTypeReference) fieldType;
	JvmTypeReference refTypeArg = parameterizedFieldType.getArguments().get(0);
	assertSame(type.getTypeParameters().get(0), refTypeArg.getType());
}
 
Example 15
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFields_defaultListT_01() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, Fields.class, "defaultListT");
	assertSame(type, field.getDeclaringType());
	assertEquals(JvmVisibility.DEFAULT, field.getVisibility());
	JvmTypeReference fieldType = field.getType();
	assertEquals("java.util.List<T>", fieldType.getIdentifier());
	assertTrue(fieldType instanceof JvmParameterizedTypeReference);
	JvmParameterizedTypeReference parameterizedFieldType = (JvmParameterizedTypeReference) fieldType;
	JvmTypeReference refTypeArg = parameterizedFieldType.getArguments().get(0);
	assertSame(type.getTypeParameters().get(0), refTypeArg.getType());
}
 
Example 16
Source File: XtypeProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected StyledString getStyledDisplayString(JvmFeature feature, boolean withParents, int insignificantParameters, String qualifiedNameAsString, String shortName,
		LightweightTypeReferenceFactory converter) {
	StyledString result = new StyledString(shortName);
	if (feature instanceof JvmOperation) {
		JvmOperation operation = (JvmOperation) feature;
		if (withParents) {
			result.append('(');
			appendParameters(result, (JvmExecutable)feature, insignificantParameters, converter);
			result.append(')');
		}
		JvmTypeReference returnType = operation.getReturnType();
		if (returnType != null && returnType.getSimpleName() != null) {
			result.append(" : ");
			result.append(converter.toLightweightReference(returnType).getHumanReadableName());
		}
		result.append(" - ", StyledString.QUALIFIER_STYLER);
		result.append(converter.toPlainTypeReference(feature.getDeclaringType()).getHumanReadableName(), StyledString.QUALIFIER_STYLER);
		if (!withParents) {
			result.append(".", StyledString.QUALIFIER_STYLER);
			result.append(feature.getSimpleName(), StyledString.QUALIFIER_STYLER);
			result.append("()", StyledString.QUALIFIER_STYLER);
		}
	} else if (feature instanceof JvmField) {
		JvmField field = (JvmField) feature;
		result.append(" : ");
		if (field.getType() != null) {
			String fieldType = converter.toLightweightReference(field.getType()).getHumanReadableName();
			if (fieldType != null)
				result.append(fieldType);
		}
		result.append(" - ", StyledString.QUALIFIER_STYLER);
		result.append(converter.toPlainTypeReference(feature.getDeclaringType()).getHumanReadableName(), StyledString.QUALIFIER_STYLER);
	} else if (feature instanceof JvmConstructor) {
		if (withParents) {
			result.append('(');
			appendParameters(result, (JvmExecutable)feature, insignificantParameters, converter);
			result.append(')');
		}
	}
	return result;
}
 
Example 17
Source File: XbaseIdeCrossrefProposalProvider.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void addNameAndDescription(ContentAssistEntry entry, JvmFeature feature, boolean withParents,
		int insignificantParameters, String shortName, LightweightTypeReferenceFactory converter) {
	StringBuilder labelBuilder = new StringBuilder(shortName);
	StringBuilder descriptionBuilder = new StringBuilder();
	if (feature instanceof JvmOperation) {
		if (withParents) {
			labelBuilder.append("(");
			appendParameters(labelBuilder, (JvmExecutable) feature, insignificantParameters, converter);
			labelBuilder.append(")");
		}
		JvmOperation jvmOperation = (JvmOperation) feature;
		JvmTypeReference returnType = jvmOperation.getReturnType();
		if (returnType != null && returnType.getSimpleName() != null) {
			labelBuilder.append(" : ");
			labelBuilder.append(converter.toLightweightReference(returnType).getHumanReadableName());
		}
		descriptionBuilder.append(
				converter.toPlainTypeReference(jvmOperation.getDeclaringType()).getHumanReadableName());
		if (!withParents) {
			descriptionBuilder.append(".");
			descriptionBuilder.append(jvmOperation.getSimpleName());
			descriptionBuilder.append("()");
		}
	} else {
		if (feature instanceof JvmField) {
			labelBuilder.append(" : ");
			JvmField jvmField = (JvmField) feature;
			if (jvmField.getType() != null) {
				String fieldType = converter.toLightweightReference(jvmField.getType())
						.getHumanReadableName();
				if (fieldType != null) {
					labelBuilder.append(fieldType);
				}
			}
			descriptionBuilder.append(
					converter.toPlainTypeReference(jvmField.getDeclaringType()).getHumanReadableName());
		} else if (feature instanceof JvmConstructor) {
			if (withParents) {
				labelBuilder.append("(");
				appendParameters(labelBuilder, ((JvmExecutable) feature), insignificantParameters, converter);
				labelBuilder.append(")");
			}
		}
	}
	entry.setLabel(labelBuilder.toString());
	entry.setDescription(descriptionBuilder.toString());
}