org.eclipse.xtext.common.types.JvmPrimitiveType Java Examples

The following examples show how to use org.eclipse.xtext.common.types.JvmPrimitiveType. 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: TypeInsteadOfConstructorLinkingCandidate.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean validate(IAcceptor<? super AbstractDiagnostic> result) {
	JvmType type = (JvmType) description.getElementOrProxy();
	String typeKind = "";
	if (type instanceof JvmPrimitiveType || type instanceof JvmVoid) {
		typeKind = "primitive type";
	} else if (type instanceof JvmAnnotationType) {
		typeKind = "annotation type";
	} else if (type instanceof JvmEnumerationType) {
		typeKind = "enum type";
	} else if (type instanceof JvmGenericType && ((JvmGenericType) type).isInterface()) {
		typeKind = "interface type";
	} else if (type instanceof JvmTypeParameter) {
		typeKind = "type parameter";
	}
	String message = String.format("Cannot instantiate the %s %s", typeKind, type.getSimpleName());
	AbstractDiagnostic diagnostic = new EObjectDiagnosticImpl(Severity.ERROR,
			IssueCodes.ILLEGAL_CLASS_INSTANTIATION, message, getExpression(),
			XbasePackage.Literals.XCONSTRUCTOR_CALL__CONSTRUCTOR, -1, null);
	result.accept(diagnostic);
	return false;
}
 
Example #2
Source File: DocumentSourceAppender.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public void appendType(final /* @NonNull */ JvmType type, /* @NonNull */ StringBuilder builder) {
	if (type instanceof JvmPrimitiveType || type instanceof JvmVoid || type instanceof JvmTypeParameter) {
		builder.append(type.getQualifiedName(getInnerTypeSeparator()));
	} else if (type instanceof JvmArrayType) {
		appendType(((JvmArrayType) type).getComponentType(), builder);
		builder.append("[]");
	} else {
		final String qualifiedName = type.getQualifiedName(getInnerTypeSeparator());
		final String simpleName = type.getSimpleName();
		JvmDeclaredType importedType = getImportedType(type);
		if (importedType == type) {
			builder.append(simpleName);
		} else if (importedType == null) {
			importSection.addImport((JvmDeclaredType) type);
			builder.append(simpleName);
		} else {
			builder.append(qualifiedName);
		}
	}
}
 
Example #3
Source File: Primitives.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public JvmType getWrapperType(JvmPrimitiveType primitive) {
	switch (primitiveKind(primitive)) {
		case Byte :
			return getType(Byte.class, primitive);
		case Short :
			return getType(Short.class, primitive);
		case Char :
			return getType(Character.class, primitive);
		case Int :
			return getType(Integer.class, primitive);
		case Long :
			return getType(Long.class, primitive);
		case Float :
			return getType(Float.class, primitive);
		case Double :
			return getType(Double.class, primitive);
		case Boolean :
			return getType(Boolean.class, primitive);
		case Void :
			return getType(Void.class, primitive);
		default :
			throw new IllegalArgumentException("Not a primitive : "+primitive);
	}
}
 
Example #4
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFields_publicInt_01() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, Fields.class, "publicInt");
	assertSame(type, field.getDeclaringType());
	assertEquals(JvmVisibility.PUBLIC, field.getVisibility());
	JvmType fieldType = field.getType().getType();
	assertEquals("int", fieldType.getIdentifier());
	assertTrue(field.getType().getType() instanceof JvmPrimitiveType);
}
 
Example #5
Source File: AnnotationValidation.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public boolean isValidAnnotationValueType(final JvmTypeReference reference) {
  JvmTypeReference _switchResult = null;
  boolean _matched = false;
  if (reference instanceof JvmGenericArrayTypeReference) {
    _matched=true;
    _switchResult = ((JvmGenericArrayTypeReference)reference).getComponentType();
  }
  if (!_matched) {
    _switchResult = reference;
  }
  final JvmTypeReference toCheck = _switchResult;
  if ((toCheck == null)) {
    return true;
  }
  JvmType _type = toCheck.getType();
  if ((_type instanceof JvmPrimitiveType)) {
    return true;
  }
  JvmType _type_1 = toCheck.getType();
  if ((_type_1 instanceof JvmEnumerationType)) {
    return true;
  }
  JvmType _type_2 = toCheck.getType();
  if ((_type_2 instanceof JvmAnnotationType)) {
    return true;
  }
  if ((Objects.equal(toCheck.getType().getQualifiedName(), "java.lang.String") || Objects.equal(toCheck.getType().getQualifiedName(), "java.lang.Class"))) {
    return true;
  }
  return false;
}
 
Example #6
Source File: PrimitiveAwareScope.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void doGetElements(JvmType type, List<IEObjectDescription> result) {
	if (type instanceof JvmVoid) {
		result.add(EObjectDescription.create("void", type));
		return;
	}
	if (type instanceof JvmPrimitiveType) {
		result.add(EObjectDescription.create(((JvmPrimitiveType) type).getSimpleName(), type));
		return;
	}
	parent.doGetElements(type, result);
}
 
Example #7
Source File: CreateMemberQuickfixes.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.3
 */
protected String getDefaultValueLiteral(/* @Nullable */ LightweightTypeReference type) {
	if (type != null && type.isPrimitive()) {
		Primitive primitiveKind = primitives.primitiveKind((JvmPrimitiveType) type.getType());
		if (primitiveKind == Primitive.Boolean)
			return "false";
		else
			return "0 as " + type.getSimpleName();
	}
	return "null";
}
 
Example #8
Source File: JvmPrimitiveTypeItemProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * This handles model notifications by calling {@link #updateChildren} to update any cached
 * children and by creating a viewer notification, which it passes to {@link #fireNotifyChanged}.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void notifyChanged(Notification notification)
{
	updateChildren(notification);

	switch (notification.getFeatureID(JvmPrimitiveType.class))
	{
		case TypesPackage.JVM_PRIMITIVE_TYPE__SIMPLE_NAME:
			fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
			return;
	}
	super.notifyChanged(notification);
}
 
Example #9
Source File: JvmPrimitiveTypeItemProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * This returns the label text for the adapted class.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public String getText(Object object)
{
	String label = ((JvmPrimitiveType)object).getSimpleName();
	return label == null || label.length() == 0 ?
		getString("_UI_JvmPrimitiveType_type") :
		getString("_UI_JvmPrimitiveType_type") + " " + label;
}
 
Example #10
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFields_publicInt_01() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, Fields.class, "publicInt");
	assertSame(type, field.getDeclaringType());
	assertEquals(JvmVisibility.PUBLIC, field.getVisibility());
	JvmType fieldType = field.getType().getType();
	assertEquals("int", fieldType.getIdentifier());
	assertTrue(field.getType().getType() instanceof JvmPrimitiveType);
}
 
Example #11
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFindTypeByName_int() {
	String typeName = "int";
	JvmType type = getTypeProvider().findTypeByName(typeName);
	assertNotNull(type);
	assertTrue(type instanceof JvmPrimitiveType);
	assertEquals(typeName, type.getIdentifier());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #12
Source File: JvmTypeConstraintImplCustom.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Constraint bounds are definitely invalid if they are <code>not null</code> and point to a primitive type.
 * {@link JvmSpecializedTypeReference} will not be resolved by this check thus they may lead to finally 
 * invalid constraint bounds.
 * 
 * @param constraintBound the reference that shall be come the new constraint.
 * @return <code>false</code> if the given constraint is definitely invalid. 
 */
protected boolean isLikelyAValidConstraintBound(JvmTypeReference constraintBound) {
	if (constraintBound == null)
		return true;
	if (constraintBound instanceof JvmSpecializedTypeReference) {
		JvmTypeReference equivalent = (JvmTypeReference) constraintBound.eGet(TypesPackage.Literals.JVM_SPECIALIZED_TYPE_REFERENCE__EQUIVALENT, false);
		if (equivalent != null) {
			return isLikelyAValidConstraintBound(equivalent);
		}
		return true;
	}
	boolean invalid = (constraintBound.getType() instanceof JvmPrimitiveType 
				|| (constraintBound.getType() instanceof JvmVoid && !constraintBound.getType().eIsProxy()));
	return !invalid;
}
 
Example #13
Source File: Primitives.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public JvmTypeReference asWrapperTypeIfPrimitive(JvmTypeReference primitive) {
	if (primitive == null || !isPrimitive(primitive)) {
		return primitive;
	}
	final JvmType wrapperType = getWrapperType((JvmPrimitiveType) primitive.getType());
	if (wrapperType == null)
		return primitive;
	return typeReferences.createTypeRef(wrapperType);
}
 
Example #14
Source File: Primitives.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public Primitive primitiveKind(JvmPrimitiveType primitiveType) {
	final String name = primitiveType.getIdentifier();
	if (Boolean.TYPE.getName().equals(name)) {
		return Primitive.Boolean;
	}
	if (Integer.TYPE.getName().equals(name)) {
		return Primitive.Int;
	}
	if (Byte.TYPE.getName().equals(name)) {
		return Primitive.Byte;
	}
	if (Short.TYPE.getName().equals(name)) {
		return Primitive.Short;
	}
	if (Character.TYPE.getName().equals(name)) {
		return Primitive.Char;
	}
	if (Long.TYPE.getName().equals(name)) {
		return Primitive.Long;
	}
	if (Float.TYPE.getName().equals(name)) {
		return Primitive.Float;
	}
	if (Double.TYPE.getName().equals(name)) {
		return Primitive.Double;
	}
	if (Void.TYPE.getName().equals(name)) {
		return Primitive.Void;
	}
	throw new IllegalArgumentException("Unkown primitive "+name);
}
 
Example #15
Source File: PrimitiveTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public JvmType createType(Class<?> clazz) {
	if (Void.TYPE.equals(clazz))
		return TypesFactory.eINSTANCE.createJvmVoid();
	JvmPrimitiveType result = TypesFactory.eINSTANCE.createJvmPrimitiveType();
	result.setSimpleName(clazz.getName());
	return result;
}
 
Example #16
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFields_publicInt_01() {
	String typeName = Fields.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, Fields.class, "publicInt");
	assertSame(type, field.getDeclaringType());
	assertEquals(JvmVisibility.PUBLIC, field.getVisibility());
	JvmType fieldType = field.getType().getType();
	assertEquals("int", fieldType.getIdentifier());
	assertTrue(field.getType().getType() instanceof JvmPrimitiveType);
}
 
Example #17
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFindTypeByName_int() {
	String typeName = "int";
	JvmType type = getTypeProvider().findTypeByName(typeName);
	assertNotNull(type);
	assertTrue(type instanceof JvmPrimitiveType);
	assertEquals(typeName, type.getIdentifier());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #18
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Object _doEvaluate(XVariableDeclaration variableDecl, IEvaluationContext context, CancelIndicator indicator) {
	Object initialValue = null;
	if (variableDecl.getRight()!=null) {
		initialValue = internalEvaluate(variableDecl.getRight(), context, indicator);
	} else {
		if (services.getPrimitives().isPrimitive(variableDecl.getType())) {
			Primitive primitiveKind = services.getPrimitives().primitiveKind((JvmPrimitiveType) variableDecl.getType().getType());
			switch(primitiveKind) {
				case Boolean:
					initialValue = Boolean.FALSE; break;
				case Char:
					initialValue = Character.valueOf((char) 0); break;
				case Double:
					initialValue = Double.valueOf(0d); break;
				case Byte:
					initialValue = Byte.valueOf((byte) 0); break;
				case Float:
					initialValue = Float.valueOf(0f); break;
				case Int:
					initialValue = Integer.valueOf(0); break;
				case Long:
					initialValue = Long.valueOf(0L); break;
				case Short:
					initialValue = Short.valueOf((short) 0); break;
				case Void:
					throw new IllegalStateException("Void is not a valid variable type.");
				default:
					throw new IllegalStateException("Unknown primitive type " + primitiveKind);
			}
		}
	}
	context.newValue(QualifiedName.create(variableDecl.getName()), initialValue);
	return null;
}
 
Example #19
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Object getDefaultObjectValue(LightweightTypeReference type) {
	if(!type.isPrimitive())
		return null;
	else {
		JvmPrimitiveType primitive = (JvmPrimitiveType) type.getType();
		switch (services.getPrimitives().primitiveKind(primitive)) {
			case Byte :
				return Byte.valueOf((byte)0);
			case Short :
				return Short.valueOf((short)0);
			case Char :
				return Character.valueOf((char)0);
			case Int :
				return Integer.valueOf(0);
			case Long :
				return Long.valueOf(0l);
			case Float :
				return Float.valueOf(0f);
			case Double :
				return Double.valueOf("0.");
			case Boolean :
				return Boolean.FALSE;
			case Void :
				return null;
			default :
				throw new IllegalArgumentException("Not a primitive : "+primitive);
		}
	}
}
 
Example #20
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected String getDefaultLiteral(JvmPrimitiveType primitiveType) {
	final String name = primitiveType.getIdentifier();
	if (Boolean.TYPE.getName().equals(name)) {
		return "false";
	}
	if (Integer.TYPE.getName().equals(name)) {
		return "0";
	}
	if (Byte.TYPE.getName().equals(name)) {
		return "(byte) 0";
	}
	if (Short.TYPE.getName().equals(name)) {
		return "(short) 0";
	}
	if (Character.TYPE.getName().equals(name)) {
		return "(char) 0";
	}
	if (Long.TYPE.getName().equals(name)) {
		return "0l";
	}
	if (Float.TYPE.getName().equals(name)) {
		return "0f";
	}
	if (Double.TYPE.getName().equals(name)) {
		return "0.0";
	}
	throw new IllegalArgumentException("Unkown primitive " + name);
}
 
Example #21
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendNullValueUntyped(LightweightTypeReference type, @SuppressWarnings("unused") EObject context, ITreeAppendable b) {
	if (!type.isPrimitive()) {
		b.append("null");
	} else {
		b.append(getDefaultLiteral((JvmPrimitiveType) type.getType()));
	}
}
 
Example #22
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendNullValue(JvmTypeReference type, EObject context, ITreeAppendable b) {
	if (!primitives.isPrimitive(type)) {
		if (!(type.getType() instanceof JvmVoid)) {
			b.append("(");
			serialize(type, context, b);
			b.append(")");
		}
		b.append("null");
	} else {
		b.append(getDefaultLiteral((JvmPrimitiveType) type.getType()));
	}
}
 
Example #23
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
String getDefaultValue(LightweightTypeReference type) {
	if (type != null && type.isPrimitive()) {
		Primitive primitiveKind = primitives.primitiveKind((JvmPrimitiveType) type.getType());
		if (primitiveKind == Primitive.Boolean)
			return "false";
		else
			return "0";
	}
	return "null";
}
 
Example #24
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFindTypeByName_int() {
	String typeName = "int";
	JvmType type = getTypeProvider().findTypeByName(typeName);
	assertNotNull(type);
	assertTrue(type instanceof JvmPrimitiveType);
	assertEquals(typeName, type.getIdentifier());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example #25
Source File: PrimitiveTypeFactoryTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public JvmType createType(Class<?> clazz) {
	JvmType result = provider.createType(clazz);
	assertTrue(result instanceof JvmPrimitiveType);
	return result;
}
 
Example #26
Source File: Primitives.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public boolean isPrimitive(JvmTypeReference type) {
	return type!=null && type.getType() instanceof JvmPrimitiveType && !type.getType().eIsProxy();
}
 
Example #27
Source File: JavaElementFinder.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public IJavaElement caseJvmPrimitiveType(JvmPrimitiveType object) {
	return null;
}
 
Example #28
Source File: Bug92Test.java    From sarl with Apache License 2.0 4 votes vote down vote up
protected static JvmType createPrimitiveType(Class<?> t) {
	JvmPrimitiveType result = TypesFactory.eINSTANCE.createJvmPrimitiveType();
	result.setSimpleName(t.getName());
	return result;
}