use of org.eclipse.n4js.n4JS.ImportDeclaration 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