Java Code Examples for org.eclipse.xtext.common.types.JvmOperation#getDeclaringType()

The following examples show how to use org.eclipse.xtext.common.types.JvmOperation#getDeclaringType() . 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: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFindTypeByName_javaLangNumber_02() {
	String typeName = Number[][].class.getName();
	JvmArrayType type = (JvmArrayType) getTypeProvider().findTypeByName(typeName);
	JvmOperation longValue = (JvmOperation) type.eResource().getEObject("java.lang.Number.longValue()");
	assertNotNull(longValue);
	JvmDeclaredType number = longValue.getDeclaringType();
	assertNotNull(number.getArrayType());
	assertSame(type, number.getArrayType().getArrayType());
	assertNull(type.eGet(TypesPackage.Literals.JVM_COMPONENT_TYPE__ARRAY_TYPE));
	// array will created on the fly
	assertNotNull(type.getArrayType());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example 2
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFindTypeByName_javaLangNumber_02() {
	String typeName = Number[][].class.getName();
	JvmArrayType type = (JvmArrayType) getTypeProvider().findTypeByName(typeName);
	JvmOperation longValue = (JvmOperation) type.eResource().getEObject("java.lang.Number.longValue()");
	assertNotNull(longValue);
	JvmDeclaredType number = longValue.getDeclaringType();
	assertNotNull(number.getArrayType());
	assertSame(type, number.getArrayType().getArrayType());
	assertNull(type.eGet(TypesPackage.Literals.JVM_COMPONENT_TYPE__ARRAY_TYPE));
	// array will created on the fly
	assertNotNull(type.getArrayType());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example 3
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testFindTypeByName_javaLangNumber_02() {
	String typeName = Number[][].class.getName();
	JvmArrayType type = (JvmArrayType) getTypeProvider().findTypeByName(typeName);
	JvmOperation longValue = (JvmOperation) type.eResource().getEObject("java.lang.Number.longValue()");
	assertNotNull(longValue);
	JvmDeclaredType number = longValue.getDeclaringType();
	assertNotNull(number.getArrayType());
	assertSame(type, number.getArrayType().getArrayType());
	assertNull(type.eGet(TypesPackage.Literals.JVM_COMPONENT_TYPE__ARRAY_TYPE));
	// array will created on the fly
	assertNotNull(type.getArrayType());
	diagnose(type);
	Resource resource = type.eResource();
	getAndResolveAllFragments(resource);
	recomputeAndCheckIdentifiers(resource);
}
 
Example 4
Source File: SARLValidator.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
protected void checkAssignment(XExpression expression, EStructuralFeature feature, boolean simpleAssignment) {
	if (simpleAssignment && expression instanceof XAbstractFeatureCall) {
		final JvmIdentifiableElement assignmentFeature = ((XAbstractFeatureCall) expression).getFeature();
		if (assignmentFeature instanceof JvmField) {
			final JvmField field = (JvmField) assignmentFeature;
			if (!field.isFinal()) {
				return;
			}
			final JvmIdentifiableElement container = getLogicalContainerProvider().getNearestLogicalContainer(expression);
			if (container != null && container instanceof JvmOperation) {
				final JvmOperation operation = (JvmOperation) container;
				if (operation.isStatic() && field.getDeclaringType() == operation.getDeclaringType()
						&& Utils.STATIC_CONSTRUCTOR_NAME.equals(operation.getSimpleName())) {
					return;
				}
			}
		}
	}
	super.checkAssignment(expression, feature, simpleAssignment);
}
 
Example 5
Source File: InvokedResolvedOperation.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected LightweightTypeReference getReceiverType(XAbstractFeatureCall featureCall, IResolvedTypes resolvedTypes, ITypeReferenceOwner owner) {
	XExpression receiver = featureCall.getActualReceiver();
	if (receiver == null) {
		// static feature call
		JvmOperation operation = (JvmOperation) featureCall.getFeature();
		JvmDeclaredType declaringType = operation.getDeclaringType();
		return owner.newParameterizedTypeReference(declaringType);
	}
	return resolvedTypes.getActualType(receiver);
}
 
Example 6
Source File: ResolvedOperationInHierarchy.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IResolvedOperation getAsBottom() {
	JvmOperation operation = getDeclaration();
	JvmDeclaredType declaringType = operation.getDeclaringType();
	List<LightweightTypeReference> superTypes = getContextType().getAllSuperTypes();
	for(LightweightTypeReference superType: superTypes) {
		if (superType.getType() == declaringType) {
			return new BottomResolvedOperation(operation, superType, getBottom().getOverrideTester());
		}
	}
	throw new IllegalStateException(String.format("Could not find declaring type of method %s in hierarchy of %s", 
			operation.getIdentifier(), getContextType().getIdentifier()));
}
 
Example 7
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void appendOperationVisibility(final ITreeAppendable b, JvmOperation operation) {
	b.newLine();
	JvmDeclaredType declaringType = operation.getDeclaringType();
	GeneratorConfig config = b.getGeneratorConfig();
	if (config != null && config.getJavaSourceVersion().isAtLeast(JAVA6)
			|| declaringType instanceof JvmGenericType && !((JvmGenericType) declaringType).isInterface()) {
		b.append("@").append(Override.class).newLine();
	}
	switch(operation.getVisibility()) {
		case DEFAULT: break;
		case PUBLIC: b.append("public "); return;
		case PROTECTED: b.append("protected "); return;
		case PRIVATE: b.append("private "); return;
	}
}
 
Example 8
Source File: AbstractMultiModeOutlineTreeProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isInheritsDispatchCases(JvmDeclaredType baseType, List<JvmOperation> dispatchCases) {
	for (JvmOperation dispatchCase : dispatchCases) {
		if (dispatchCase.getDeclaringType() != baseType) {
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: DispatchHelper.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Return all the cases that are associated with the given dispatch operation.
 */
public List<JvmOperation> getAllDispatchCases(JvmOperation dispatcherOperation) {
	DispatchSignature dispatchSignature = new DispatchSignature(dispatcherOperation.getSimpleName(), dispatcherOperation.getParameters().size());
	JvmDeclaredType type = dispatcherOperation.getDeclaringType();
	ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, type);
	ContextualVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper, owner.newParameterizedTypeReference(type));
	return getAllDispatchMethods(dispatchSignature, type, contextualVisibilityHelper);
}
 
Example 10
Source File: SARLHighlightingCalculator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies if the given call is for a capacity function call.
 *
 * @param feature the feature to test.
 * @return {@code true} if the feature is capacity(s method.
 */
protected boolean isCapacityMethodCall(JvmOperation feature) {
	if (feature != null) {
		final JvmDeclaredType container = feature.getDeclaringType();
		if (container instanceof JvmGenericType) {
			return this.inheritanceHelper.isSarlCapacity((JvmGenericType) container);
		}
	}
	return false;
}
 
Example 11
Source File: ObjectAndPrimitiveBasedCastOperationCandidateSelector.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Validate the parameters of the operation.
 *
 * @param operation the operation from which the parameters are extracted.
 * @return {@code true} if the return type is valid; otherwise {@code false}.
 */
protected boolean isValidParameters(JvmOperation operation) {
	final List<JvmFormalParameter> parameters = operation.getParameters();
	if (parameters.size() == 0) {
		final JvmType originType = operation.getDeclaringType();
		return this.expressionType.isSubtypeOf(originType);
	} else if (parameters.size() == 1) {
		final JvmTypeReference parameterType = parameters.get(0).getParameterType();
		final LightweightTypeReference paramType = this.state.getReferenceOwner().toLightweightTypeReference(parameterType);
		if (parameterType != null) {
			return paramType.isAssignableFrom(this.expressionType);
		}
	}
	return false;
}
 
Example 12
Source File: PyGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
private void markCapacityFunctions(PyAppendable it) {
	final Map<JvmOperation, String> mapping = this.useCapacityMapping;
	this.useCapacityMapping = new HashMap<>();
	final ImportManager imports = it.getImportManager();
	for (final Entry<JvmOperation, String> entry : mapping.entrySet()) {
		final JvmOperation operation = entry.getKey();
		final JvmDeclaredType type = operation.getDeclaringType();
		imports.addImportFor(type);
		it.declareVariable(operation, entry.getValue());
	}
}
 
Example 13
Source File: OverrideTester.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Checks if the overriding method and the given overridden candidate have compatible subsignatures
 * according to JLS 8.4.2. Uses information about static-ness and visibility for early exits.
 * 
 * The implemented algorithm pretty much mirrors the one from
    * class <code>org.eclipse.jdt.internal.corext.util.MethodOverrideTester</code>.
 * 
 * @param checkInheritance <code>true</code> if it is unknown whether the given operations are declared in a valid type hierarchy.
 */
public IOverrideCheckResult isSubsignature(AbstractResolvedOperation overriding, JvmOperation overridden, boolean checkInheritance) {
	JvmOperation declaration = overriding.getDeclaration();
	if (declaration == overridden) {
		return new LazyOverrideCheckResult(overriding, overridden, OverrideCheckDetails.CURRENT);
	}
	if (overridden.getDeclaringType() == declaration.getDeclaringType()) {
		return new LazyOverrideCheckResult(overriding, overridden, OverrideCheckDetails.SAME_DECLARATOR);
	}
	ITypeReferenceOwner owner = overriding.getContextType().getOwner();
	LightweightTypeReference currentDeclarator = null;
	if (checkInheritance) {
		// here we use the raw types intentionally since there is no need to resolve
		// declarators to concrete bounds to determine the override relationship of types
		currentDeclarator = owner.newParameterizedTypeReference(declaration.getDeclaringType());
		if (!currentDeclarator.isSubtypeOf(overridden.getDeclaringType())) {
			return new LazyOverrideCheckResult(overriding, overridden, OverrideCheckDetails.NO_INHERITANCE);	
		}
	}
	if (!Strings.equal(overridden.getSimpleName(), declaration.getSimpleName())) {
		return new LazyOverrideCheckResult(overriding, overridden, OverrideCheckDetails.NAME_MISMATCH);
	}
	int parameterCount = overridden.getParameters().size();
	if (parameterCount != declaration.getParameters().size()) {
		return new LazyOverrideCheckResult(overriding, overridden, OverrideCheckDetails.ARITY_MISMATCH);
	}
	if (currentDeclarator == null) {
		currentDeclarator = owner.newParameterizedTypeReference(declaration.getDeclaringType());
	}
	if (!(new ContextualVisibilityHelper(visibilityHelper, currentDeclarator).isVisible(overridden))) {
		return new LazyOverrideCheckResult(overriding, overridden, OverrideCheckDetails.NOT_VISIBLE);
	}
	if (declaration.isStatic() != overridden.isStatic()) {
		return new LazyOverrideCheckResult(overriding, overridden, OverrideCheckDetails.STATIC_MISMATCH);
	}
	AbstractResolvedOperation overriddenInHierarchy = new ResolvedOperationInHierarchy(overridden, overriding.getBottom());
	if (parameterCount != 0 && !isMatchingParameterList(overriding, overriddenInHierarchy)) {
		return new LazyOverrideCheckResult(overriding, overridden, OverrideCheckDetails.PARAMETER_TYPE_MISMATCH);
	}
	if (!isMatchingTypeParameters(overriding, overriddenInHierarchy))
		return new LazyOverrideCheckResult(overriding, overridden, OverrideCheckDetails.TYPE_PARAMETER_MISMATCH);
	return new LazyOverrideCheckResult(overriding, overridden, getPrimaryValidDetail(overriding, overridden));
}
 
Example 14
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void addDispatchError(JvmGenericType type, Iterable<JvmOperation> operations, String message, String modifier, String ISSUE_ID) {
	for (JvmOperation jvmOperation : operations)
		if (jvmOperation.getDeclaringType() == type)
			addDispatchError(jvmOperation, message, modifier, ISSUE_ID);
}
 
Example 15
Source File: CacheMethodCompileStrategy.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void apply(ITreeAppendable appendable) {
	JvmOperation cacheMethod = (JvmOperation) logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
	JvmDeclaredType containerType = cacheMethod.getDeclaringType();
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(containerType);
	final ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, containerType);
	LightweightTypeReference listType = owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
		@Override
		public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
			reference.addTypeArgument(owner.newWildcardTypeReference());
			return reference;
		}
	});
	String cacheVarName = cacheField.getSimpleName();
	String cacheKeyVarName = appendable.declareSyntheticVariable("CacheKey", "_cacheKey");
	appendable.append("final ").append(listType).append(" ").append(cacheKeyVarName)
		.append(" = ").append(CollectionLiterals.class).append(".newArrayList(");
	List<JvmFormalParameter> list = cacheMethod.getParameters();
	for (Iterator<JvmFormalParameter> iterator = list.iterator(); iterator.hasNext();) {
		JvmFormalParameter jvmFormalParameter = iterator.next();
		appendable.append(getVarName(jvmFormalParameter));
		if (iterator.hasNext()) {
			appendable.append(", ");
		}
	}
	appendable.append(");");
	// declare result variable
	LightweightTypeReference returnType = resolvedTypes.getActualType(initializerMethod.getParameters().get(0));
	if (returnType != null) {
		appendable.newLine().append("final ").append(returnType);
	} else {
		appendable.newLine().append("final Object");
	}
	String resultVarName = "_result";
	appendable.append(" ").append(resultVarName).append(";");
	// open synchronize block
	appendable.newLine().append("synchronized (").append(cacheVarName).append(") {");
	appendable.increaseIndentation();
	// if the cache contains the key return the previously created object.
	appendable.newLine().append("if (").append(cacheVarName).append(".containsKey(").append(cacheKeyVarName)
			.append(")) {");
	appendable.increaseIndentation();
	appendable.newLine().append("return ").append(cacheVarName).append(".get(").append(cacheKeyVarName).append(");");
	appendable.decreaseIndentation().newLine().append("}");
	
	// execute the creation
	compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
	appendable.newLine();
	appendable.append(resultVarName).append(" = ");
	compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
	appendable.append(";");

	// store the newly created object in the cache
	appendable.newLine().append(cacheVarName).append(".put(").append(cacheKeyVarName).append(", ");
	LightweightTypeReference fieldType = resolvedTypes.getActualType(cacheField);
	LightweightTypeReference declaredResultType = fieldType.getTypeArguments().get(1);
	boolean castRequired = false;
	if (!declaredResultType.isAssignableFrom(returnType)) {
		castRequired = true;
		appendable.append("(").append(declaredResultType).append(")");
	}
	appendable.append(resultVarName).append(");");

	// close synchronize block
	appendable.decreaseIndentation();
	appendable.newLine().append("}");
	appendable.newLine().append(initializerMethod.getSimpleName()).append("(").append(resultVarName);
	for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
		appendable.append(", ").append(parameter.getName());
	}
	appendable.append(");");
	// return the result
	appendable.newLine().append("return ");
	if (castRequired) {
		appendable.append("(").append(declaredResultType).append(")");
	}
	appendable.append(resultVarName).append(";");
}
 
Example 16
Source File: DispatchHelper.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Return the local cases that contribute to the given dispatch operation (in no particular order, but usually as defined in the file).
 */
public List<JvmOperation> getLocalDispatchCases(JvmOperation dispatcherOperation) {
	DispatchSignature dispatchSignature = new DispatchSignature(dispatcherOperation.getSimpleName(), dispatcherOperation.getParameters().size());
	JvmDeclaredType type = dispatcherOperation.getDeclaringType();
	return getLocalDispatchCases(type, dispatchSignature);
}