use of org.eclipse.xtext.xtype.XImportDeclaration in project xtext-extras by eclipse.
the class RewritableImportSection method addStaticImport.
/**
* @param typeFqn
* The fully qualified name of the type to import. E.g. <code>java.util.List</code>. May not be
* <code>null</code>.
* @param member
* member name to import. May not be <code>null</code>. For wildcard use <code>*</code>
*/
public boolean addStaticImport(final String typeFqn, final String member) {
if (typeFqn == null || member == null) {
throw new IllegalArgumentException("Type name " + typeFqn + ". Member name: " + member);
}
if (hasStaticImport(typeFqn, member, false)) {
return false;
}
XImportDeclaration importDecl = createImport(typeFqn, member);
importDecl.setStatic(true);
return addedImportDeclarations.add(importDecl);
}
use of org.eclipse.xtext.xtype.XImportDeclaration in project xtext-extras by eclipse.
the class XbaseValidator method populateMaps.
protected void populateMaps(XImportSection importSection, final Map<String, List<XImportDeclaration>> imports, final Map<String, List<XImportDeclaration>> staticImports, final Map<String, List<XImportDeclaration>> extensionImports, final Map<String, JvmType> importedNames) {
for (XImportDeclaration imp : importSection.getImportDeclarations()) {
if (imp.getImportedNamespace() != null) {
addIssue("The use of wildcard imports is deprecated.", imp, IMPORT_WILDCARD_DEPRECATED);
continue;
}
JvmType importedType = imp.getImportedType();
if (importedType == null || importedType.eIsProxy()) {
continue;
}
Map<String, List<XImportDeclaration>> map = imp.isStatic() ? (imp.isExtension() ? extensionImports : staticImports) : imports;
List<XImportDeclaration> list = map.get(importedType.getIdentifier());
if (list != null) {
list.add(imp);
continue;
}
list = Lists.newArrayListWithCapacity(2);
list.add(imp);
map.put(importedType.getIdentifier(), list);
if (!imp.isStatic()) {
JvmType currentType = importedType;
String currentSuffix = currentType.getSimpleName();
JvmType collidingImport = importedNames.put(currentSuffix, importedType);
if (collidingImport != null)
error("The import '" + importedType.getIdentifier() + "' collides with the import '" + collidingImport.getIdentifier() + "'.", imp, null, IssueCodes.IMPORT_COLLISION);
while (currentType.eContainer() instanceof JvmType) {
currentType = (JvmType) currentType.eContainer();
currentSuffix = currentType.getSimpleName() + "$" + currentSuffix;
JvmType collidingImport2 = importedNames.put(currentSuffix, importedType);
if (collidingImport2 != null)
error("The import '" + importedType.getIdentifier() + "' collides with the import '" + collidingImport2.getIdentifier() + "'.", imp, null, IssueCodes.IMPORT_COLLISION);
}
} else if (!imp.isWildcard()) {
Iterable<JvmFeature> allFeatures = staticallyImportedMemberProvider.getAllFeatures(imp);
if (!allFeatures.iterator().hasNext()) {
addIssue("The import " + imp.getImportedName() + " cannot be resolved.", imp, IMPORT_UNRESOLVED);
continue;
}
}
}
}
use of org.eclipse.xtext.xtype.XImportDeclaration in project xtext-extras by eclipse.
the class XbaseValidator method removeStaticImport.
private boolean removeStaticImport(Map<String, List<XImportDeclaration>> staticImports, JvmMember member) {
JvmDeclaredType declaringType = member.getDeclaringType();
String identifier = declaringType.getIdentifier();
List<XImportDeclaration> list = staticImports.get(identifier);
if (list == null) {
return false;
}
if (list.size() == 1) {
staticImports.remove(identifier);
return true;
}
int indexToRemove = -1;
for (int i = 0; i < list.size(); i++) {
XImportDeclaration staticImportDeclaration = list.get(i);
if (staticImportDeclaration.isWildcard()) {
if (indexToRemove == -1) {
indexToRemove = i;
}
continue;
}
if (Objects.equal(member.getSimpleName(), staticImportDeclaration.getMemberName())) {
indexToRemove = i;
break;
}
}
if (indexToRemove == -1) {
indexToRemove = 0;
}
list.remove(indexToRemove);
return true;
}
use of org.eclipse.xtext.xtype.XImportDeclaration in project xtext-extras by eclipse.
the class SerializerScopeProvider method doGetTypeScope.
protected IScope doGetTypeScope(XFeatureCall call, JvmType type) {
if (call.isPackageFragment()) {
if (type instanceof JvmDeclaredType) {
String packageName = ((JvmDeclaredType) type).getPackageName();
int dot = packageName.indexOf('.');
if (dot == -1) {
return new SingletonScope(EObjectDescription.create(packageName, type), IScope.NULLSCOPE);
} else {
String firstSegment = packageName.substring(0, dot);
return new SingletonScope(EObjectDescription.create(firstSegment, type), IScope.NULLSCOPE);
}
}
return IScope.NULLSCOPE;
} else {
if (type instanceof JvmDeclaredType && ((JvmDeclaredType) type).getDeclaringType() != null) {
Resource resource = call.eResource();
if (resource instanceof XtextResource) {
XImportSection importSection = importsConfiguration.getImportSection((XtextResource) resource);
if (importSection != null) {
List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
List<IEObjectDescription> descriptions = Lists.newArrayList();
for (XImportDeclaration importDeclaration : importDeclarations) {
if (!importDeclaration.isStatic() && !importDeclaration.isWildcard() && !importDeclaration.isExtension()) {
JvmDeclaredType importedType = importDeclaration.getImportedType();
if (importedType == type) {
String syntax = importsConfiguration.getLegacyImportSyntax(importDeclaration);
if (syntax != null && /* no node model attached */
syntax.equals(type.getQualifiedName())) {
String packageName = importedType.getPackageName();
descriptions.add(EObjectDescription.create(syntax.substring(packageName.length() + 1), type));
}
}
if (EcoreUtil.isAncestor(importedType, type)) {
String name = type.getSimpleName();
JvmType worker = type;
while (worker != importedType) {
worker = (JvmType) worker.eContainer();
name = worker.getSimpleName() + "$" + name;
}
descriptions.add(EObjectDescription.create(name, type));
}
}
}
return new SimpleScope(descriptions);
}
}
return new SingletonScope(EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);
} else {
return new SingletonScope(EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);
}
}
}
Aggregations