for (Object o : initialClass.filterChildren(new TypeFilter<>(CtCodeSnippet.class)).list()) { CtElement el = (CtElement) o; elements2before.put(el.getPath(), el); Factory f = initialClass.getFactory(); backup.addAll(initialClass.getModifiers()); initialClass.removeModifier(ModifierKind.PUBLIC); initialClass.delete(); build(f, "package " + initialClass.getPackage().getQualifiedName() + ";" + initialClass.toString()); } finally { initialClass.setModifiers(backup); CtType<?> newClass = f.Type().get(initialClass.getQualifiedName()); newClass.replace(initialClass);
CacheBasedConflictFinder(CtType<?> p_type) { type = p_type; typeRef = type.getReference(); }
@SuppressWarnings("unchecked") @Override public <T, U> U getValue(T element) { return ((U) ((Object) (castTarget(element).getMethods()))); }
@Override public boolean matches(CtType<?> mayBeSubType) { if (superTypeQualifiedName != null && superTypeQualifiedName.equals(mayBeSubType.getQualifiedName())) { //we should not accept superType return false; } return mayBeSubType.isSubtypeOf(superType); } }
/** returns true if the given name is a nested type name */ public boolean hasNestedTypeConflict(String name) { if (cachedNestedTypeNames == null) { Collection<CtType<?>> allTypes = type.getNestedTypes(); cachedNestedTypeNames = new HashSet<>(allTypes.size()); for (CtType<?> t : allTypes) { cachedNestedTypeNames.add(t.getSimpleName()); } } return cachedNestedTypeNames.contains(name); }
/** * Returns clone of the patternType. * The clone is done only once. Later calls returns cached clone. * @return */ private CtType<?> getClonedPatternType() { if (clonedPatternType == null) { clonedPatternType = patternType.clone(); if (patternType.isParentInitialized()) { //set parent package, to keep origin qualified name of the Template. It is needed for correct substitution of Template name by target type reference clonedPatternType.setParent(patternType.getParent()); } setElements(Collections.singletonList(clonedPatternType)); } return clonedPatternType; }
@Override public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) { if (amplifiedTestToBeKept.isEmpty()) { return amplifiedTestToBeKept; } CtType<?> clone = this.currentClassTestToBeAmplified.clone(); clone.setParent(this.currentClassTestToBeAmplified.getParent()); this.currentClassTestToBeAmplified.getMethods().stream() .filter(TestFramework.get()::isTest) .forEach(clone::removeMethod); amplifiedTestToBeKept.forEach(clone::addMethod); DSpotUtils.printCtTypeToGivenDirectory(clone, new File(getPathToCopiedFiles())); final Map<String, Map<String, List<Integer>>> lineCoveragePerTestMethods = CloverExecutor.execute(this.configuration, getPathToCopiedFiles(), clone.getQualifiedName()); final List<CtMethod<?>> selectedTests = this.selectTests(clone, lineCoveragePerTestMethods); this.selectedAmplifiedTest.addAll(selectedTests); return selectedTests; }
public static String ctTypeToFullQualifiedName(CtType<?> testClass) { if (testClass.getModifiers().contains(ModifierKind.ABSTRACT)) { CtTypeReference<?> referenceOfSuperClass = testClass.getReference(); return testClass.getFactory().Class().getAll().stream() .filter(ctType -> referenceOfSuperClass.equals(ctType.getSuperclass())) .map(CtType::getQualifiedName).collect(Collectors.joining(",")); } else { return testClass.getQualifiedName(); } }
private boolean isMainType(CtTypeMember tm, CtType<?> mainType) { if (tm instanceof CtType) { return mainType.getQualifiedName().equals(((CtType) tm).getQualifiedName()); } return false; }
private boolean printAndCompile(InputConfiguration configuration, CtType<?> clone, CtMethod<?> amplifiedTestToBeMinimized) { clone.setParent(this.testClass.getParent()); this.testClass.getMethods().stream() .filter(TestFramework.get()::isTest) .forEach(clone::removeMethod); clone.addMethod(amplifiedTestToBeMinimized); DSpotUtils.printCtTypeToGivenDirectory(clone, new File(DSpotCompiler.getPathToAmplifiedTestSrc())); return DSpotCompiler.compile(configuration, DSpotCompiler.getPathToAmplifiedTestSrc(), this.configuration.getFullClassPathWithExtraDependencies(), new File(this.configuration.getAbsolutePathToTestClasses())); } }
CtType<?> clone = type.clone(); StringBuilder tentativeTypeName = new StringBuilder(type.getSimpleName() + "Copy"); while (type.getFactory().Type().get(type.getPackage().getQualifiedName() + "." + tentativeTypeName) != null) { tentativeTypeName.append("X"); clone.setSimpleName(cloneTypeName); type.getPackage().addType(clone);
/** * Changes name of a type element. * * @param type * Type in the AST. * @param name * New name of the element. */ public static void changeTypeName(final CtType<?> type, String name) { final String typeQFN = type.getQualifiedName(); final List<CtTypeReference<?>> references = Query.getElements(type.getFactory(), new TypeFilter<CtTypeReference<?>>(CtTypeReference.class) { @Override public boolean matches(CtTypeReference<?> reference) { String refFQN = reference.getQualifiedName(); return typeQFN.equals(refFQN); } }); type.setSimpleName(name); for (CtTypeReference<?> reference : references) { reference.setSimpleName(name); } }
static String getLocalTypeRefBySimpleName(CtType<?> templateType, String typeSimpleName) { CtType<?> type = templateType.getNestedType(typeSimpleName); if (type != null) { return type.getQualifiedName(); } type = templateType.getPackage().getType(typeSimpleName); if (type != null) { return type.getQualifiedName(); } Set<String> typeQNames = new HashSet<>(); templateType .filterChildren((CtTypeReference<?> ref) -> typeSimpleName.equals(ref.getSimpleName())) .forEach((CtTypeReference<?> ref) -> typeQNames.add(ref.getQualifiedName())); if (typeQNames.size() > 1) { throw new SpoonException("The type parameter " + typeSimpleName + " is ambiguous. It matches multiple types: " + typeQNames); } if (typeQNames.size() == 1) { return typeQNames.iterator().next(); } return null; }
public static boolean isAssertionClass(CtType klass) { return klass != null && ("org.junit.Assert".equals(klass.getQualifiedName()) || "junit.framework.Assert".equals(klass.getQualifiedName())) && klass.getSuperclass() != null && isAssertionClass(klass.getSuperclass().getDeclaration()); }
public CompilationUnit getOrCreate(CtType type) { if (type == null) { return null; } if (type.getPosition().getCompilationUnit() != null) { return type.getPosition().getCompilationUnit(); } if (type.isTopLevel()) { CtModule module; if (type.getPackage() != null && factory.getEnvironment().getComplianceLevel() > 8) { module = type.getPackage().getParent(CtModule.class); } else { module = null; } File file = this.factory.getEnvironment().getOutputDestinationHandler().getOutputPath(module, type.getPackage(), type).toFile(); try { String path = file.getCanonicalPath(); CompilationUnit result = this.getOrCreate(path); result.addDeclaredType(type); type.setPosition(this.factory.createPartialSourcePosition(result)); return result; } catch (IOException e) { throw new SpoonException("Cannot get path for file: " + file.getAbsolutePath(), e); } } else { return getOrCreate(type.getTopLevelType()); } }
@Override public <T> void visitCtTypeReference(CtTypeReference<T> reference) { assertNotNull(reference); if (CtTypeReference.NULL_TYPE_NAME.equals(reference.getSimpleName()) || "?".equals(reference.getSimpleName())) { super.visitCtTypeReference(reference); return; } final CtType<T> typeDeclaration = reference.getTypeDeclaration(); assertNotNull(reference.toString() + " cannot be found in ", typeDeclaration); assertEquals(reference.getSimpleName(), typeDeclaration.getSimpleName()); assertEquals(reference.getQualifiedName(), typeDeclaration.getQualifiedName()); if (reference.getDeclaration() == null) { assertTrue("typeDeclaration must be shadow", typeDeclaration.isShadow()); } super.visitCtTypeReference(reference); }
public String getGeneratedByComment(CtElement ele) { SourcePosition pos = ele.getPosition(); if (pos != null && pos.isValidPosition()) { CompilationUnit cu = pos.getCompilationUnit(); if (cu != null) { CtType<?> mainType = cu.getMainType(); if (mainType != null) { StringBuilder result = new StringBuilder(); result.append("Generated by "); result.append(mainType.getQualifiedName()); appendInnerTypedElements(result, mainType, ele); result.append('('); result.append(mainType.getSimpleName()); result.append(".java:"); result.append(pos.getLine()); result.append(')'); return result.toString(); } } } return null; } private void appendInnerTypedElements(StringBuilder result, CtType<?> mainType, CtElement ele) {
if (!t.equals(targetType.getFactory().Type().createReference(Template.class))) { CtTypeReference<?> t1 = t; t1 = (CtTypeReference<?>) o; } else if (o instanceof Class) { t1 = targetType.getFactory().Type().createReference((Class<?>) o); } else if (o instanceof String) { t1 = targetType.getFactory().Type().createReference((String) o); if (!t1.equals(targetType.getReference())) { Class<?> c = null; try { targetType.addSuperInterface(t1); targetType.addSuperInterface(t1);
/** * Creates a method by copying an existing method. * * @param <T> * the type of the method * @param target * the target type where the new method has to be inserted to * @param source * the source method to be copied * @param redirectReferences * tells if all the references to the owning type of the source * method should be redirected to the target type (true is * recommended for most uses) * @return the newly created method */ public <T> CtMethod<T> create(CtType<?> target, CtMethod<T> source, boolean redirectReferences) { CtMethod<T> newMethod = source.clone(); if (redirectReferences && (source.getDeclaringType() != null)) { Substitution.redirectTypeReferences(newMethod, source.getDeclaringType().getReference(), target.getReference()); } target.addMethod(newMethod); return newMethod; }
@Deprecated private Set<CtMethod<?>> getAllMethods(CtType<?> type) { try { return type.getAllMethods(); } catch (StackOverflowError e) { LOGGER.error("Overflow!"); final Set<CtMethod<?>> methods = new HashSet<>(type.getMethods()); CtTypeReference<?> currentSuperClass = type.getSuperclass(); while (currentSuperClass != null) { methods.addAll(currentSuperClass.getTypeDeclaration().getMethods()); currentSuperClass = currentSuperClass.getSuperclass(); } return methods; } }