org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper Java Examples

The following examples show how to use org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper. 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: NonOverridableTypesProvider.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void process(JvmMember context, Map<String, JvmIdentifiableElement> result) {
	if (context instanceof JvmDeclaredType) {
		if (!result.containsKey(context.getSimpleName()))
			result.put(context.getSimpleName(), context);
		JvmDeclaredType contextType = (JvmDeclaredType) context;
		ContextualVisibilityHelper visibilityHelper = new ContextualVisibilityHelper(this.visibilityHelper, contextType);
		addInnerTypes(contextType, "", visibilityHelper, result);
		
		Set<JvmType> superTypes = rawSuperTypes.collect(contextType);
		for (JvmType superType : superTypes) {
			if (superType instanceof JvmDeclaredType)
				addInnerTypes((JvmDeclaredType) superType, "", visibilityHelper, result);
		}
	}
	if (context instanceof JvmTypeParameterDeclarator)
		addTypeParameters((JvmTypeParameterDeclarator) context, result);
	JvmDeclaredType declaringType = context.getDeclaringType();
	if (declaringType != null)
		process(declaringType, result);
}
 
Example #2
Source File: InteractiveUnresolvedTypeResolver.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected void findCandidateTypes(final JvmDeclaredType contextType, final String typeSimpleName,
		IJavaSearchScope searchScope, final IAcceptor<JvmDeclaredType> acceptor) throws JavaModelException {
	BasicSearchEngine searchEngine = new BasicSearchEngine();
	final IVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper, contextType);
	searchEngine.searchAllTypeNames(null, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, typeSimpleName.toCharArray(),
			SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, IJavaSearchConstants.TYPE, searchScope,
			new IRestrictedAccessTypeRequestor() {
				@Override
				public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName,
						char[][] enclosingTypeNames, String path, AccessRestriction access) {
					final String qualifiedTypeName = getQualifiedTypeName(packageName, enclosingTypeNames,
							simpleTypeName);
					if ((access == null
							|| (access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter()))
						&& !TypeFilter.isFiltered(packageName, simpleTypeName)) {
						JvmType importType = typeRefs.findDeclaredType(qualifiedTypeName, contextType.eResource());
						if (importType instanceof JvmDeclaredType
								&& contextualVisibilityHelper.isVisible((JvmDeclaredType) importType)) {
							acceptor.accept((JvmDeclaredType) importType);
						}
					}
				}
			}, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
}
 
Example #3
Source File: DispatchHelper.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Computes all the dispatch methods that are declared in the given type or altered
 * by additional cases in this type. The associated operations are sorted by according their parameter types
 * from left to right where the most special types occur before more common types. Ambiguous
 * ordering is resolved alphabetically.
 * 
    * An exemplary order would look like this
 * <pre>
 *   method(String)
 *   method(Serializable)
 *   method(CharSequence)
 *   method(Object)
 * </pre>
 * 
 * @return a mapping from {@link DispatchSignature signature} to sorted operations.
 */
public ListMultimap<DispatchSignature, JvmOperation> getDeclaredOrEnhancedDispatchMethods(JvmDeclaredType type) {
	ListMultimap<DispatchSignature, JvmOperation> result = Multimaps2.newLinkedHashListMultimap(2,4);
	Iterable<JvmOperation> operations = type.getDeclaredOperations();
	ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, type);
	ContextualVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper, owner.newParameterizedTypeReference(type));
	for(JvmOperation operation: operations) {
		if (isDispatchFunction(operation)) {
			DispatchSignature signature = new DispatchSignature(operation.getSimpleName().substring(1), operation.getParameters().size());
			if (!result.containsKey(signature)) {
				List<JvmOperation> allOperations = getAllDispatchMethods(signature, type,
						contextualVisibilityHelper);
				result.putAll(signature, allOperations);
			}
		}
	}
	return result;
}
 
Example #4
Source File: OverrideHelper.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public JvmOperation findOverriddenOperation(JvmOperation operation) {
	if (operation.getVisibility() == JvmVisibility.PRIVATE) {
		return null;
	}
	ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, operation.eResource().getResourceSet());
	LightweightTypeReference declaringType = owner.toLightweightTypeReference(operation.getDeclaringType());
	TypeParameterSubstitutor<?> substitutor = createSubstitutor(owner, declaringType);
	return findOverriddenOperation(operation, declaringType, substitutor, owner, new ContextualVisibilityHelper(visibilityHelper, declaringType));
}
 
Example #5
Source File: StaticallyImportedMemberProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public IVisibilityHelper getVisibilityHelper(Resource resource) {
	if (resource instanceof XtextResource) {
		String packageName = importsConfiguration.getPackageName(((XtextResource) resource));
		if (packageName != null) {
			return new ContextualVisibilityHelper(visibilityHelper, packageName);
		}
	}
	return visibilityHelper;
}
 
Example #6
Source File: OverrideProposalUtil.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public List<IResolvedExecutable> getImplementationCandidates(JvmDeclaredType type, boolean isAnonymous) {
	if (type == null || !(type instanceof JvmGenericType)) {
		return Collections.emptyList();
	}
	JavaVersion sourceVersion = generatorConfigProvider.get(type).getJavaSourceVersion();
	ResolvedFeatures resolvedFeatures = overrideHelper.getResolvedFeatures(type, sourceVersion);
	List<IResolvedExecutable> result = new ArrayList<>();
	ContextualVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper,
			resolvedFeatures.getType());
	addOperationCandidates(resolvedFeatures, contextualVisibilityHelper, result);
	if (!isAnonymous && !((JvmGenericType) type).isInterface()) {
		addConstructorCandidates(resolvedFeatures, contextualVisibilityHelper, result);
	}
	return result;
}
 
Example #7
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 #8
Source File: DispatchHelper.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected List<JvmOperation> getAllDispatchMethods(DispatchSignature signature, JvmDeclaredType type,
		ContextualVisibilityHelper contextualVisibilityHelper) {
	List<JvmOperation> allOperations = Lists.newArrayListWithExpectedSize(5);
	Iterable<JvmFeature> allFeatures = type.findAllFeaturesByName(signature.getDispatchCaseName());
	for(JvmFeature feature: allFeatures) {
		if (feature instanceof JvmOperation) {
			JvmOperation operationByName = (JvmOperation) feature;
			if (signature.isDispatchCase(operationByName) && contextualVisibilityHelper.isVisible(operationByName)) {
				allOperations.add(operationByName);
			}
		}
	}
	sort(allOperations);
	return allOperations;
}
 
Example #9
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 #10
Source File: FeatureScopeSessionWithContext.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public FeatureScopeSessionWithContext(AbstractFeatureScopeSession parent, LightweightTypeReference contextType) {
	super(parent);
	this.visibilityHelper = new ContextualVisibilityHelper(parent, contextType);
}
 
Example #11
Source File: FeatureScopeSessionWithContext.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public FeatureScopeSessionWithContext(AbstractFeatureScopeSession parent, LightweightTypeReference contextType, String packageName) {
	super(parent);
	this.visibilityHelper = new ContextualVisibilityHelper(parent, contextType, packageName);
}
 
Example #12
Source File: JavaTypeQuickfixes.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected ContextualVisibilityHelper getVisibilityHelper(final JvmDeclaredType contextType) {
	StandardTypeReferenceOwner owner = new StandardTypeReferenceOwner(services, contextType);
	final ParameterizedTypeReference contextTypeRef =  owner.newParameterizedTypeReference(contextType);
	final ContextualVisibilityHelper visibilityHelper = new ContextualVisibilityHelper(contextTypeRef);
	return visibilityHelper;
}
 
Example #13
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void doCheckOverriddenMethods(XtendTypeDeclaration xtendType, JvmGenericType inferredType, ResolvedFeatures resolvedFeatures,
		Set<EObject> flaggedOperations) {
	List<IResolvedOperation> operationsMissingImplementation = null;
	boolean doCheckAbstract = !inferredType.isAbstract();
	if (doCheckAbstract) {
		operationsMissingImplementation = Lists.newArrayList();
	}
	IVisibilityHelper visibilityHelper = new ContextualVisibilityHelper(this.visibilityHelper, resolvedFeatures.getType());
	boolean flaggedType = false;
	for (IResolvedOperation operation : resolvedFeatures.getAllOperations()) {
		JvmDeclaredType operationDeclaringType = operation.getDeclaration().getDeclaringType();
		if (operationDeclaringType != inferredType) {
			if (operationsMissingImplementation != null && operation.getDeclaration().isAbstract()) {
				operationsMissingImplementation.add(operation);
			}
			if (visibilityHelper.isVisible(operation.getDeclaration())) {
				String erasureSignature = operation.getResolvedErasureSignature();
				List<IResolvedOperation> declaredOperationsWithSameErasure = resolvedFeatures.getDeclaredOperations(erasureSignature);
				for (IResolvedOperation localOperation : declaredOperationsWithSameErasure) {
					if (!localOperation.isOverridingOrImplementing(operation.getDeclaration()).isOverridingOrImplementing()) {
						EObject source = findPrimarySourceElement(localOperation);
						if (operation.getDeclaration().isStatic() && !localOperation.getDeclaration().isStatic()) {
							if (!isInterface(operationDeclaringType)) {
								if (flaggedOperations.add(source)) {
									error("The instance method "
											+ localOperation.getSimpleSignature()
											+ " cannot override the static method "
											+ operation.getSimpleSignature() + " of type "
											+ getDeclaratorName(operation.getDeclaration()) + ".",
											source, nameFeature(source), DUPLICATE_METHOD);
								}
							}
						} else if (!operation.getDeclaration().isStatic() && localOperation.getDeclaration().isStatic()) {
							if (flaggedOperations.add(source)) {
								error("The static method "
										+ localOperation.getSimpleSignature()
										+ " cannot hide the instance method "
										+ operation.getSimpleSignature() + " of type "
										+ getDeclaratorName(operation.getDeclaration()) + ".",
										source, nameFeature(source), DUPLICATE_METHOD);
							}
						} else if (flaggedOperations.add(source)) {
							error("Name clash: The method "
									+ localOperation.getSimpleSignature() + " of type "
									+ inferredType.getSimpleName()
									+ " has the same erasure as "
									+ operation.getSimpleSignature() + " of type "
									+ getDeclaratorName(operation.getDeclaration()) + " but does not override it.",
									source, nameFeature(source), DUPLICATE_METHOD);
						}
					}
				}
				if (operation instanceof ConflictingDefaultOperation
						&& contributesToConflict(inferredType, (ConflictingDefaultOperation) operation) && !flaggedType) {
					IResolvedOperation conflictingOperation = ((ConflictingDefaultOperation) operation).getConflictingOperations()
							.get(0);
					// Include the declaring class in the issue code in order to give better quick fixes
					String[] uris = new String[] {
							getDeclaratorName(operation.getDeclaration()) + "|"
									+ EcoreUtil.getURI(operation.getDeclaration()).toString(),
							getDeclaratorName(conflictingOperation.getDeclaration()) + "|"
									+ EcoreUtil.getURI(conflictingOperation.getDeclaration()).toString() };
					if (!operation.getDeclaration().isAbstract() && !conflictingOperation.getDeclaration().isAbstract()) {
						error("The type " + inferredType.getSimpleName() + " inherits multiple implementations of the method "
								+ conflictingOperation.getSimpleSignature() + " from "
								+ getDeclaratorName(conflictingOperation.getDeclaration()) + " and "
								+ getDeclaratorName(operation.getDeclaration()) + ".", xtendType,
								XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME, CONFLICTING_DEFAULT_METHODS, uris);
					} else {
						// At least one of the operations is non-abstract
						IResolvedOperation abstractOp, nonabstractOp;
						if (operation.getDeclaration().isAbstract()) {
							abstractOp = operation;
							nonabstractOp = conflictingOperation;
						} else {
							abstractOp = conflictingOperation;
							nonabstractOp = operation;
						}
						error("The non-abstract method " + nonabstractOp.getSimpleSignature() + " inherited from "
								+ getDeclaratorName(nonabstractOp.getDeclaration()) + " conflicts with the method "
								+ abstractOp.getSimpleSignature() + " inherited from " + getDeclaratorName(abstractOp.getDeclaration())
								+ ".", xtendType, XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME, CONFLICTING_DEFAULT_METHODS,
								uris);
					}
					flaggedType = true;
				}
			}
		}
	}
	if (operationsMissingImplementation != null && !operationsMissingImplementation.isEmpty() && !flaggedType) {
		reportMissingImplementations(xtendType, inferredType, operationsMissingImplementation);
	}
}