use of spoon.reflect.declaration.CtImport in project dspot by STAMP-project.
the class AmplificationHelper method getImport.
@Deprecated
public static Set<CtType> getImport(CtType type) {
if (!AmplificationHelper.importByClass.containsKey(type)) {
ImportScanner importScanner = new ImportScannerImpl();
try {
importScanner.computeImports(type);
Set<CtType> set = importScanner.getAllImports().stream().map(CtImport::getReference).filter(Objects::nonNull).filter(ctElement -> ctElement instanceof CtType).map(ctElement -> (CtType) ctElement).collect(Collectors.toSet());
AmplificationHelper.importByClass.put(type, set);
} catch (Exception e) {
AmplificationHelper.importByClass.put(type, new HashSet<>(0));
}
}
return AmplificationHelper.importByClass.get(type);
}
use of spoon.reflect.declaration.CtImport in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method isImported.
private boolean isImported(CtExecutableReference executableReference) {
CtImport executableImport = executableReference.getFactory().createImport(executableReference);
if (this.imports.contains(executableImport)) {
return true;
} else {
if (executableReference.getDeclaringType() == null) {
return false;
}
CtTypeReference staticTypeMemberReference = executableReference.getFactory().Type().createWildcardStaticTypeMemberReference(executableReference.getDeclaringType());
CtImport staticClassImport = executableReference.getFactory().createImport(staticTypeMemberReference);
return this.imports.contains(staticClassImport);
}
}
use of spoon.reflect.declaration.CtImport in project spoon by INRIA.
the class ElementPrinterHelper method writeImports.
/**
* writes the imports in a specific order (eg all static imports together
*/
public void writeImports(Collection<CtImport> imports) {
Set<String> setImports = new HashSet<>();
Set<String> setStaticImports = new HashSet<>();
for (CtImport ctImport : imports) {
String importTypeStr;
switch(ctImport.getImportKind()) {
case TYPE:
CtTypeReference typeRef = (CtTypeReference) ctImport.getReference();
importTypeStr = typeRef.getQualifiedName();
if (!isJavaLangClasses(importTypeStr)) {
setImports.add(importTypeStr);
}
break;
case ALL_TYPES:
CtPackageReference packageRef = (CtPackageReference) ctImport.getReference();
importTypeStr = packageRef.getQualifiedName() + ".*";
if (!isJavaLangClasses(importTypeStr)) {
setImports.add(importTypeStr);
}
break;
case METHOD:
CtExecutableReference execRef = (CtExecutableReference) ctImport.getReference();
if (execRef.getDeclaringType() != null) {
setStaticImports.add(this.removeInnerTypeSeparator(execRef.getDeclaringType().getQualifiedName()) + "." + execRef.getSimpleName());
}
break;
case FIELD:
CtFieldReference fieldRef = (CtFieldReference) ctImport.getReference();
setStaticImports.add(this.removeInnerTypeSeparator(fieldRef.getDeclaringType().getQualifiedName()) + "." + fieldRef.getSimpleName());
break;
case ALL_STATIC_MEMBERS:
CtTypeReference typeStarRef = (CtTypeReference) ctImport.getReference();
importTypeStr = typeStarRef.getQualifiedName();
if (!isJavaLangClasses(importTypeStr)) {
setStaticImports.add(importTypeStr);
}
break;
}
}
List<String> sortedImports = new ArrayList<>(setImports);
Collections.sort(sortedImports);
boolean isFirst = true;
for (String importLine : sortedImports) {
if (isFirst) {
printer.writeln();
printer.writeln();
isFirst = false;
}
printer.writeKeyword("import").writeSpace();
writeQualifiedName(importLine).writeSeparator(";").writeln();
}
if (setStaticImports.size() > 0) {
if (isFirst) {
printer.writeln();
}
printer.writeln();
List<String> sortedStaticImports = new ArrayList<>(setStaticImports);
Collections.sort(sortedStaticImports);
for (String importLine : sortedStaticImports) {
printer.writeKeyword("import").writeSpace().writeKeyword("static").writeSpace();
writeQualifiedName(importLine).writeSeparator(";").writeln();
}
}
}
use of spoon.reflect.declaration.CtImport in project spoon by INRIA.
the class DefaultCoreFactory method createImport.
public CtImport createImport() {
CtImport e = new CtImportImpl();
e.setFactory(getMainFactory());
return e;
}
use of spoon.reflect.declaration.CtImport in project spoon by INRIA.
the class CtElementImpl method toString.
@Override
public String toString() {
DefaultJavaPrettyPrinter printer = new DefaultJavaPrettyPrinter(getFactory().getEnvironment());
String errorMessage = "";
try {
// we do not want to compute imports of a CtImport as it may change the print of a reference
if (!(this instanceof CtImport)) {
printer.computeImports(this);
}
printer.scan(this);
} catch (ParentNotInitializedException ignore) {
LOGGER.error(ERROR_MESSAGE_TO_STRING, ignore);
errorMessage = ERROR_MESSAGE_TO_STRING;
}
// removing them from the toString() representation
return printer.toString().replaceFirst("^\\s+", "") + errorMessage;
}
Aggregations