Java Code Examples for org.eclipse.xtext.common.types.JvmType#eClass()

The following examples show how to use org.eclipse.xtext.common.types.JvmType#eClass() . 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: FunctionTypes.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected JvmOperation findImplementingOperation(List<JvmType> rawTypes) {
	if (rawTypes.size() == 1) {
		JvmType rawType = rawTypes.get(0);
		if (rawType.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
			JvmGenericType castedRawType = (JvmGenericType) rawType;
			if (!castedRawType.isFinal()) {
				Iterable<JvmFeature> features = castedRawType.getAllFeatures();
				JvmOperation result = null;
				for (JvmFeature feature : features) {
					if (feature.eClass() == TypesPackage.Literals.JVM_OPERATION) {
						JvmOperation op = (JvmOperation) feature;
						if (isValidFunction(op)) {
							if (result == null)
								result = op;
							else {
								return null;
							}
						}
					}
				}
				return result;
			}
		}
	}
	return null;
}
 
Example 2
Source File: StandardTypeReferenceOwner.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a references to the given class or returns an {@link UnknownTypeReference} if no
 * JRE is available. If the type is available, the given acceptor is used to initialize it further.
 */
@Override
public LightweightTypeReference newReferenceTo(Class<?> type, TypeReferenceInitializer<? super ParameterizedTypeReference> init) {
	if (init == null) {
		throw new NullPointerException("initializer");
	}
	JvmType rawType = getServices().getTypeReferences().findDeclaredType(type, getContextResourceSet());
	if (rawType == null) {
		return newUnknownTypeReference(type.getName());
	}
	if (rawType.eClass() == TypesPackage.Literals.JVM_ARRAY_TYPE) {
		throw new IllegalArgumentException("given type is an array type: " + type);
	}
	ParameterizedTypeReference result = newParameterizedTypeReference(rawType);
	return init.enhance(result);
}
 
Example 3
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Java 5 does not allow forward references in type parameters, so we have to validate this, too
 */
public void doCheckTypeParameterForwardReference(List<JvmTypeParameter> sourceTypeParameters) {
	if (sourceTypeParameters.size() >= 1) {
		final Set<JvmTypeParameter> allowed = newHashSet();
		for(int i = 0; i < sourceTypeParameters.size(); i++) {
			JvmTypeParameter current = sourceTypeParameters.get(i);
			for(JvmTypeConstraint constraint: current.getConstraints()) {
				JvmTypeReference constraintRef = constraint.getTypeReference();
				if (constraintRef != null) {
					JvmType constraintType = constraintRef.getType();
					if (constraintType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
						EObject sourceElement = associations.getPrimarySourceElement(constraintType);
						if (sourceElement!=null && sourceElement.eContainer() == current.eContainer() && !allowed.contains(sourceElement)) {
							error("Illegal forward reference to type parameter " + ((JvmTypeParameter)constraintType).getSimpleName(), 
									constraintRef, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE, -1, TYPE_PARAMETER_FORWARD_REFERENCE);
						}
					}
				}
			}
			allowed.add(current);
		}
	}
}
 
Example 4
Source File: ParameterizedTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isInner(JvmType type) {
	if (type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
		if (type.eContainer() instanceof JvmDeclaredType) {
			return !((JvmGenericType) type).isStatic();
		}
	}
	return false;
}
 
Example 5
Source File: ArrayTypes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the array representation of this reference if its represents a sub-type of {@link Iterable}.
 * If the iterable's type is a primitive wrapper, the array is <em>not</em> the primitive array but
 * the wrapper array. May return <code>null</code> if the conversion is not possible.
 * 
 * This is the externalized and thereby
 * exchangeable implementation of {@link ParameterizedTypeReference#tryConvertToArray()}.
 * 
 * @param typeReference the to-be-converted reference.
 * @return an equivalent {@link ArrayTypeReference} or <code>null</code>.
 */
/* @Nullable */
public ArrayTypeReference tryConvertToArray(ParameterizedTypeReference typeReference) {
	ArrayTypeReference result = doTryConvertToArray(typeReference);
	if (result != null) {
		return result;
	} else {
		JvmType type = typeReference.getType();
		if (type.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
			return doTryConvertToArray(typeReference, new RecursionGuard<JvmTypeParameter>());
		}
	}
	return null;
}
 
Example 6
Source File: InnerTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public LightweightTypeReference getRawTypeReference() {
	if (isRawType()) {
		return this;
	}
	LightweightTypeReference rawOuter = outer.getRawTypeReference();
	JvmType type = getType();
	if (type.eClass() != TypesPackage.Literals.JVM_TYPE_PARAMETER) {
		return getOwner().newParameterizedTypeReference(rawOuter, type);
	}
	return getServices().getRawTypeHelper().getRawTypeReference(this, getOwner().getContextResourceSet());
}
 
Example 7
Source File: XbaseWithAnnotationsTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private ITypeComputationState addEnumImportsIfNecessary(ITypeComputationState state, LightweightTypeReference expectedType, JvmType enumTypeCandidate) {
	ITypeComputationState expectationState = state.withExpectation(expectedType);
	if (enumTypeCandidate != null && enumTypeCandidate.eClass() == TypesPackage.Literals.JVM_ENUMERATION_TYPE) {
		expectationState.addImports(new EnumLiteralImporter((JvmDeclaredType) enumTypeCandidate));
	}
	return expectationState;
}
 
Example 8
Source File: LightweightTypeReferenceFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isInner(JvmType type) {
	if (type != null && type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
		if (type.eContainer() instanceof JvmDeclaredType) {
			return !((JvmGenericType) type).isStatic();
		}
	}
	return false;
}
 
Example 9
Source File: LightweightTypeReferenceFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public LightweightTypeReference toPlainTypeReference(JvmType type) {
	if (type.eClass() == TypesPackage.Literals.JVM_ARRAY_TYPE) {
		JvmComponentType componentType = ((JvmArrayType) type).getComponentType();
		LightweightTypeReference componentTypeReference = toPlainTypeReference(componentType);
		return owner.newArrayTypeReference(componentTypeReference);
	}
	return owner.newParameterizedTypeReference(type);
}
 
Example 10
Source File: ParameterizedTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns <code>null</code> if no such super type exists, a {@link LightweightTypeReference}
 * if it is more efficient to create it directly, or a {@link JvmTypeReference} with or without unresolved
 * type arguments that can be processed later on.
 */
/* @Nullable */
private Object internalGetSuperType(JvmType rawType) {
	if (rawType == type) {
		return this;
	}
	if (isPrimitive() || isPrimitiveVoid()) {
		return null;
	}
	EClass otherEClass = rawType.eClass();
	if (otherEClass == TypesPackage.Literals.JVM_PRIMITIVE_TYPE || otherEClass == TypesPackage.Literals.JVM_VOID) {
		return null;
	}
	boolean interfaceType = false;
	if (otherEClass != TypesPackage.Literals.JVM_TYPE_PARAMETER) {
		if (Object.class.getName().equals(rawType.getIdentifier())) {
			return getOwner().newParameterizedTypeReference(rawType);
		}
		if (otherEClass != TypesPackage.Literals.JVM_ARRAY_TYPE) {
			// declared type
			JvmDeclaredType declaredType = (JvmDeclaredType) rawType;
			if (type.eClass() != TypesPackage.Literals.JVM_TYPE_PARAMETER && declaredType.isFinal()) {
				return null;
			}
		} else if (isInterfaceType()) {
			return null;
		}
		if (otherEClass == TypesPackage.Literals.JVM_GENERIC_TYPE && !(interfaceType = ((JvmGenericType) rawType).isInterface()) && isInterfaceType()) {
			return null;
		}
	}
	JvmTypeReference superType = getSuperType(rawType, interfaceType, type, new RecursionGuard<JvmType>());
	return superType;
}
 
Example 11
Source File: ParameterizedTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isWrapper(JvmTypeParameter typeParameter, /* @Nullable */ RecursionGuard<JvmTypeParameter> stack) {
	for(JvmTypeConstraint constraint: typeParameter.getConstraints()) {
		if (constraint.eClass() == TypesPackage.Literals.JVM_UPPER_BOUND) {
			JvmTypeReference upperBound = constraint.getTypeReference();
			if (upperBound != null) {
				JvmType upperBoundType = upperBound.getType();
				if (upperBoundType == null) {
					return false;
				}
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
					return isWrapper((JvmGenericType)upperBoundType);
				}
				// guard against recursive deps
				if (upperBoundType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
					if (typeParameter == upperBoundType || stack != null && !stack.tryNext((JvmTypeParameter) upperBoundType)) {
						return false;
					}
					if (stack == null) {
						stack = new RecursionGuard<JvmTypeParameter>();
						stack.tryNext(typeParameter);
						stack.tryNext((JvmTypeParameter) upperBoundType);
					}
					return isWrapper((JvmTypeParameter) upperBoundType, stack);
				}
			}
		}
	}
	return false;
}
 
Example 12
Source File: AnnotationReferenceBuildContextImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void checkType(final JvmAnnotationValue it, final String componentType, final boolean mustBeArray) {
  if ((componentType == null)) {
    return;
  }
  JvmTypeReference _returnType = it.getOperation().getReturnType();
  JvmType _type = null;
  if (_returnType!=null) {
    _type=_returnType.getType();
  }
  final JvmType returnType = _type;
  boolean _or = false;
  if (mustBeArray) {
    _or = true;
  } else {
    EClass _eClass = null;
    if (returnType!=null) {
      _eClass=returnType.eClass();
    }
    boolean _equals = Objects.equal(_eClass, TypesPackage.Literals.JVM_ARRAY_TYPE);
    _or = _equals;
  }
  if (_or) {
    ConditionUtils.checkTypeName(this.getAnnotationValueTypeName(returnType), (componentType + "[]"));
  } else {
    ConditionUtils.checkTypeName(this.getAnnotationValueTypeName(returnType), componentType);
  }
}
 
Example 13
Source File: ParameterizedTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isRawType(JvmTypeParameter current, RecursionGuard<JvmTypeParameter> guard) {
	if (guard.tryNext(current)) {
		List<JvmTypeConstraint> constraints = current.getConstraints();
		for(int i = 0, size = constraints.size(); i < size; i++) {
			JvmTypeConstraint constraint = constraints.get(i);
			if (constraint.eClass() == TypesPackage.Literals.JVM_UPPER_BOUND && constraint.getTypeReference() != null) {
				JvmTypeReference superType = constraint.getTypeReference();
				JvmType rawSuperType = superType.getType();
				if (rawSuperType != null) {
					if (rawSuperType.eClass() == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
						if (isRawType((JvmTypeParameter) rawSuperType, guard)) {
							return true;
						}
					}
					if (rawSuperType.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
						if (!((JvmGenericType) rawSuperType).getTypeParameters().isEmpty()) {
							if (superType.eClass() == TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE) {
								JvmParameterizedTypeReference casted = (JvmParameterizedTypeReference) superType;
								if (casted.getArguments().isEmpty()) {
									return true;
								}
							}
						}
					}
				}
			}
		}
	}
	return false;
}
 
Example 14
Source File: InnerFunctionTypeReference.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public LightweightTypeReference getRawTypeReference() {
	if (isRawType()) {
		return this;
	}
	LightweightTypeReference rawOuter = outer.getRawTypeReference();
	JvmType type = getType();
	if (type.eClass() != TypesPackage.Literals.JVM_TYPE_PARAMETER) {
		return getOwner().newParameterizedTypeReference(rawOuter, type);
	}
	return getServices().getRawTypeHelper().getRawTypeReference(this, getOwner().getContextResourceSet());
}
 
Example 15
Source File: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private int internalGetPrimitiveKindFromWrapper(ParameterizedTypeReference typeReference) {
	JvmType type = typeReference.getType();
	if (type == null || type.eIsProxy()) {
		return PRIMITIVE_NONE; 
	}
	EClass eClass = type.eClass();
	if (eClass != TypesPackage.Literals.JVM_GENERIC_TYPE) {
		if (eClass == TypesPackage.Literals.JVM_TYPE_PARAMETER) {
			return internalGetPrimitiveKindFromWrapper((JvmTypeParameter)type, null);
		}
		return PRIMITIVE_NONE;
	}
	return internalGetPrimitiveKindFromWrapper((JvmGenericType)type);
}
 
Example 16
Source File: FunctionTypes.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public FunctionTypeKind getFunctionTypeKind(ParameterizedTypeReference typeReference) {
	JvmType type = typeReference.getType();
	if (type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
		JvmDeclaredType outerType = ((JvmGenericType) type).getDeclaringType();
		if (outerType != null) {
			if (Procedures.class.getName().equals(outerType.getIdentifier())) {
				return FunctionTypeKind.PROCEDURE;
			}
			if (Functions.class.getName().equals(outerType.getIdentifier())) {
				return FunctionTypeKind.FUNCTION;
			}
		}
	}
	return FunctionTypeKind.NONE;
}
 
Example 17
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** Create the operations into the SARL feature container.
 *
 * @param codeBuilder the builder of the script.
 * @param methods the operations to create.
 * @param context the context of the actions. If {@code null}, the block of the actions are not generated.
 * @throws JavaModelException if the Java model is invalid.
 * @throws IllegalArgumentException if the signature is not syntactically correct.
 */
public void createActionsWith(
		ActionBuilder codeBuilder,
		Collection<IMethod> methods,
		XtendTypeDeclaration context) throws JavaModelException, IllegalArgumentException {
	if (methods != null) {
		for (final IMethod operation : methods) {
			if (!isGeneratedOperation(operation)) {
				final ISarlActionBuilder action = codeBuilder.addAction(operation.getElementName());
				action.setReturnType(Signature.toString(operation.getReturnType()));
				final IFormalParameterBuilder[] sarlParams = createFormalParametersWith(
					name -> action.addParameter(name), operation);
				if (context != null) {
					final JvmType type = this.typeReferences.findDeclaredType(
							operation.getDeclaringType().getFullyQualifiedName(),
							context);
					final JvmOperation superOperation = getJvmOperation(operation, type);
					// Create the block
					final IBlockExpressionBuilder block = action.getExpression();
					if ((type.eClass() != TypesPackage.Literals.JVM_GENERIC_TYPE
							|| !((JvmGenericType) type).isInterface())
							&& superOperation != null
							&& !superOperation.isAbstract()) {
						// Create the super-call expression
						final IExpressionBuilder superCall = block.addExpression();
						final XMemberFeatureCall call = XbaseFactory.eINSTANCE.createXMemberFeatureCall();
						superCall.setXExpression(call);
						superCall.setDocumentation(block.getAutoGeneratedActionString());
						call.setFeature(superOperation);
						call.setMemberCallTarget(superCall.createReferenceToSuper());
						final List<XExpression> arguments = call.getMemberCallArguments();
						for (final IFormalParameterBuilder currentParam : sarlParams) {
							final XFeatureCall argumentSource = XbaseFactory.eINSTANCE.createXFeatureCall();
							arguments.add(argumentSource);
							currentParam.setReferenceInto(argumentSource);
						}
					} else {
						final JvmTypeReference ret = superOperation != null ? superOperation.getReturnType() : null;
						block.setDefaultAutoGeneratedContent(
								ret == null ? null : ret.getIdentifier());
					}
				}
			}
		}
	}
}
 
Example 18
Source File: RawTypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
private int internalGetPrimitiveKind(ParameterizedTypeReference typeReference) {
	JvmType type = typeReference.getType();
	EClass eClass = type.eClass();
	if (eClass == TypesPackage.Literals.JVM_PRIMITIVE_TYPE) {
		if (type.eIsProxy())
			return PRIMITIVE_NONE;
		String name = type.getSimpleName();
		switch (name.length()) {
			case 3:
				if ("int".equals(name)) {
					return PRIMITIVE_INT;
				}
				break;
			case 4:
				if ("long".equals(name)) {
					return PRIMITIVE_LONG;
				} else if ("char".equals(name)) {
					return PRIMITIVE_CHAR;
				} else if ("byte".equals(name)) {
					return PRIMITIVE_BYTE;
				}
				break;
			case 5:
				if ("short".equals(name)) {
					return PRIMITIVE_SHORT;
				} else if ("float".equals(name)) {
					return PRIMITIVE_FLOAT;
				}
				break;
			case 6:
				if ("double".equals(name)) {
					return PRIMITIVE_DOUBLE;
				}
				break;
			case 7:
				if ("boolean".equals(name)) {
					return PRIMITIVE_BOOLEAN;
				}
				break;
		}
	} else if (eClass == TypesPackage.Literals.JVM_VOID) {
		if (type.eIsProxy())
			return PRIMITIVE_NONE;
		return PRIMITIVE_VOID;
	}
	return PRIMITIVE_NONE;
}
 
Example 19
Source File: TypeConformanceComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isClass(JvmType type) {
	EClass eClass = type.eClass();
	if (eClass == TypesPackage.Literals.JVM_ARRAY_TYPE)
		return isClass(((JvmArrayType) type).getComponentType());
	return eClass == TypesPackage.Literals.JVM_GENERIC_TYPE && !((JvmGenericType) type).isInterface();
}
 
Example 20
Source File: SARLLabelProvider.java    From sarl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("checkstyle:npathcomplexity")
@Override
public ImageDescriptor getImageDescriptorForQualifiedName(String qualifiedName, Notifier context,
		IJvmTypeProvider typeProvider) {
	JvmType type = null;
	if (typeProvider != null) {
		type = typeProvider.findTypeByName(qualifiedName);
	}
	if (type == null && context != null) {
		type = this.services.getTypeReferences().findDeclaredType(qualifiedName, context);
	}
	int adornments = this.adornments.get(type);
	JvmVisibility visibility = JvmVisibility.DEFAULT;
	if (type != null) {
		if (type.eClass() == TypesPackage.Literals.JVM_GENERIC_TYPE) {
			final JvmGenericType gtype = (JvmGenericType) type;
			visibility = gtype.getVisibility();
			final int ecoreCode = this.inheritanceHelper.getSarlElementEcoreType(gtype);
			switch (ecoreCode) {
			case SarlPackage.SARL_AGENT:
				return this.images.forAgent(visibility, this.adornments.get(gtype));
			case SarlPackage.SARL_BEHAVIOR:
				return this.images.forBehavior(visibility, this.adornments.get(gtype));
			case SarlPackage.SARL_CAPACITY:
				// Remove the "abstract" ornment because capacities are always abstract.
				adornments = (adornments & JavaElementImageDescriptor.ABSTRACT) ^ adornments;
				return this.images.forCapacity(visibility, adornments);
			case SarlPackage.SARL_EVENT:
				return this.images.forEvent(visibility, this.adornments.get(gtype));
			case SarlPackage.SARL_SKILL:
				return this.images.forSkill(visibility, this.adornments.get(gtype));
			default:
				if (gtype.isInterface()) {
					return this.images.forInterface(visibility, this.adornments.get(gtype));
				}
			}
		} else if (type.eClass() == TypesPackage.Literals.JVM_ENUMERATION_TYPE) {
			final JvmEnumerationType etype = (JvmEnumerationType) type;
			visibility = etype.getVisibility();
			return this.images.forEnum(visibility, adornments);
		} else if (type.eClass() == TypesPackage.Literals.JVM_ANNOTATION_TYPE) {
			final JvmAnnotationType atype = (JvmAnnotationType) type;
			visibility = atype.getVisibility();
			return this.images.forAnnotation(visibility, adornments);
		} else {
			visibility = JvmVisibility.DEFAULT;
		}
	}
	// Default icon is the class icon.
	return this.images.forClass(visibility, adornments);
}