use of org.eclipse.xtext.EcoreUtil2 in project n4js by eclipse.
the class ScriptApiTracker method computeMethodDiff.
/**
* Find last missing methods, which the implType would have if it would follow the inheritance as defined in the API
*
* @param implType
* Type of implementation project, calculated from AST
* @param collector
* member collector for the project.
* @param ownedAndMixedInConcreteMember
* already computed for implType
* @param missingApiMethods2
* already computed for implType
* @return list of virtual Methods
*/
public List<VirtualApiTMethod> computeMethodDiff(TClass implType, MemberCollector collector, List<TMember> ownedAndMixedInConcreteMember, MemberList<TMethod> missingApiMethods2) {
Optional<ProjectComparisonAdapter> optAdapt = firstProjectComparisonAdapter(implType.eResource());
if (optAdapt.isPresent()) {
ProjectComparisonEntry compareEntry = optAdapt.get().getEntryFor(EcoreUtil2.getContainerOfType(implType, TModule.class));
ProjectComparisonEntry typeCompare = compareEntry.getChildForElementImpl(implType);
if (typeCompare != null && typeCompare.getElementAPI() != null) {
TClass apiType = (TClass) typeCompare.getElementAPI();
MemberList<TMember> implMembers = collector.allMembers(implType, false, true);
MemberList<TMember> apiMembers = collector.allMembers(apiType, false, true);
final HashSet<String> methodNamesAlreadyDefined = new HashSet<>();
Stream.concat(implMembers.stream(), Stream.concat(ownedAndMixedInConcreteMember.stream(), missingApiMethods2.stream())).forEach(m -> {
if (m instanceof TMethod) {
methodNamesAlreadyDefined.add(m.getName());
}
});
return apiMembers.stream().filter(t -> t instanceof TMethod).filter(m -> !methodNamesAlreadyDefined.contains(((TMethod) m).getName())).map(m2 -> {
TMethod m = (TMethod) m2;
VirtualApiTMethod vMethod = new VirtualApiTMethod(m.getName(), TypeUtils.copyPartial(m, TypesPackage.Literals.SYNTAX_RELATED_TELEMENT__AST_ELEMENT));
return vMethod;
}).collect(Collectors.toList());
}
}
return emptyList();
}
use of org.eclipse.xtext.EcoreUtil2 in project n4js by eclipse.
the class GrammarLinter method printKeywordsOnlyInDatatypeRules.
private void printKeywordsOnlyInDatatypeRules() {
Grammar grammar = grammarAccess.getGrammar();
ListMultimap<String, Keyword> allKeywords = getAllKeywords(grammar);
System.out.println("Keywords which do not occur in production rules: ");
outer: for (Collection<Keyword> chunk : allKeywords.asMap().values()) {
for (Keyword keyword : chunk) {
AbstractRule rule = EcoreUtil2.getContainerOfType(keyword, AbstractRule.class);
if (!GrammarUtil.isDatatypeRule(rule)) {
continue outer;
}
}
System.out.println(" " + ((List<Keyword>) chunk).get(0).getValue());
}
System.out.println();
}
use of org.eclipse.xtext.EcoreUtil2 in project n4js by eclipse.
the class N4JSScopingConsumableMethodsDiagnosis method diagnose.
@Override
DiagnosticMessage diagnose(QualifiedName name, ParameterizedPropertyAccessExpression propertyAccess) {
// determine containing member declaration and classifier definition
N4MemberDeclaration containingMemberDeclaration = EcoreUtil2.getContainerOfType(propertyAccess, N4MemberDeclaration.class);
N4ClassifierDefinition classifierDefinition = EcoreUtil2.getContainerOfType(containingMemberDeclaration, N4ClassifierDefinition.class);
// if ancestors present and non-static context (no super in static context)
if (containingMemberDeclaration != null && !containingMemberDeclaration.isStatic() && classifierDefinition != null) {
// Get candidate methods
MemberList<TMember>.MemberIterable<TMethod> methods = containerTypesHelper.fromContext(propertyAccess).membersOfImplementedInterfacesForConsumption((TClassifier) classifierDefinition.getDefinedType()).methods();
boolean hasMethod = methods.stream().filter(// Filter for non-static non-abstract methods
m -> !m.isHasNoBody() && !m.isStatic()).anyMatch(m -> m.getName().equals(name.toString()));
if (hasMethod) {
return createMessage(IssueCodes.CLF_CANNOT_REFER_TO_DEFAULT_METHOD_WITH_SUPER, IssueCodes.getMessageForCLF_CANNOT_REFER_TO_DEFAULT_METHOD_WITH_SUPER());
}
}
return null;
}
use of org.eclipse.xtext.EcoreUtil2 in project n4js by eclipse.
the class ScriptApiTracker method computeMissingApiFields.
/**
* Computes the list of virtual AccessorTuples for missing static fields on Interfaces
*
* @return List of {@link VirtualApiTField}
*/
private List<AccessorTuple> computeMissingApiFields(TInterface declaration) {
Optional<ProjectComparisonAdapter> optAdapt = firstProjectComparisonAdapter(declaration.eResource());
if (optAdapt.isPresent()) {
ProjectComparisonAdapter projectComparisonAdapter = optAdapt.get();
ProjectComparisonEntry compareEntry = projectComparisonAdapter.getEntryFor(EcoreUtil2.getContainerOfType(declaration, TModule.class));
ProjectComparisonEntry typeCompare = compareEntry.getChildForElementImpl(declaration);
if (typeCompare != null) {
ArrayList<AccessorTuple> collectedMissingFields = new ArrayList<>();
Predicate<ProjectComparisonEntry> filter = (pce -> pce.getElementAPI() instanceof TField);
filter = filter.and(pce -> pce.getElementImpl()[0] == null).and(pce -> PROVIDES_INITIALZER.hasAnnotation((TField) pce.getElementAPI()));
Function<TInterface, Consumer<? super ProjectComparisonEntry>> actionProvider = pivot -> pce -> {
TField apiField = ((TField) pce.getElementAPI());
VirtualApiMissingFieldAccessorTuple ret = createVirtFieldAccessorTuple(apiField);
collectedMissingFields.add(ret);
};
if (!checkInterfaceImplementsInterface(declaration, typeCompare.getElementAPI())) {
return emptyList();
}
// Call the supertype iterations scaffolding:
interfaceApiSupertypeWalker(filter, actionProvider, projectComparisonAdapter, (TInterface) typeCompare.getElementAPI(), TInterface.class);
return collectedMissingFields;
}
}
return emptyList();
}
use of org.eclipse.xtext.EcoreUtil2 in project n4js by eclipse.
the class StaticWriteAccessFilterScope method isAccepted.
@Override
protected boolean isAccepted(IEObjectDescription description) {
EObject proxyOrInstance = description.getEObjectOrProxy();
if (proxyOrInstance instanceof TMember && !proxyOrInstance.eIsProxy()) {
TMember member = (TMember) proxyOrInstance;
// this particular message to hide the better one.
if (member.isStatic() && member.isWriteable() && /* i.e. (member.isField(), not const || member.isSetter()) */
isWriteAccess()) {
ContainerType<?> memberType = member.getContainingType();
memberTypeName = memberType.getName();
// Access only allowed for Direct access, so AST must be IdentifierRef.
final boolean isTargetGivenByIdentifier = getTarget() instanceof IdentifierRef;
if (!isTargetGivenByIdentifier) {
// Not an IdentifierRef --> disallowed for write access.
return false;
}
IdentifierRef idref = (IdentifierRef) getTarget();
// this also covers aliased imports:
if (idref.getId().getName().equals(memberTypeName)) {
// correct name.
return true;
} else {
// wrong name, disallowed
// search for alias, for better error reporting.
Script sc = EcoreUtil2.getContainerOfType(context, Script.class);
Optional<NamedImportSpecifier> namedImport = sc.getScriptElements().stream().filter(se -> se instanceof ImportDeclaration).map(se -> (ImportDeclaration) se).flatMap(idecl -> {
return idecl.getImportSpecifiers().stream().filter(is -> is instanceof NamedImportSpecifier).map(is -> (NamedImportSpecifier) is);
}).filter(s -> s.getImportedElement() == memberType).findFirst();
if (namedImport.isPresent()) {
// if alias is present assign, otherwise null will be passed through
memberTypeAlias = namedImport.get().getAlias();
}
return false;
}
}
}
return true;
}
Aggregations