use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class CtTypeImpl method addTypeMemberAt.
@Override
public <C extends CtType<T>> C addTypeMemberAt(int position, CtTypeMember member) {
if (member == null) {
return (C) this;
}
if (this.typeMembers == CtElementImpl.<CtTypeMember>emptyList()) {
this.typeMembers = new ArrayList<>();
}
if (!this.typeMembers.stream().anyMatch(m -> m == member)) {
member.setParent(this);
CtRole role;
if (member instanceof CtMethod) {
role = METHOD;
} else if (member instanceof CtConstructor) {
role = CONSTRUCTOR;
} else if (member instanceof CtField) {
role = FIELD;
} else if (member instanceof CtAnonymousExecutable) {
role = ANNONYMOUS_EXECUTABLE;
} else {
role = NESTED_TYPE;
}
getFactory().getEnvironment().getModelChangeListener().onListAdd(this, role, this.typeMembers, position, member);
if (position < typeMembers.size()) {
this.typeMembers.add(position, member);
} else {
this.typeMembers.add(member);
}
}
return (C) this;
}
use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class CtTypeImpl method addTypeMember.
/**
* Adds a type member.
* If it has a position, adds it at the right place according to the position (sourceStart).
* If it is implicit, adds it at the beginning.
* Otherwise, adds it at the end.
*/
@Override
public <C extends CtType<T>> C addTypeMember(CtTypeMember member) {
if (member == null) {
return (C) this;
}
Comparator c = new CtLineElementComparator();
if (member.isImplicit()) {
return addTypeMemberAt(0, member);
}
// by default, inserting at the end
int insertionPosition = typeMembers.size();
// we search for an insertion position only if this one has one position
if (member.getPosition() != null && member.getPosition().getSourceStart() >= 0) {
for (int i = typeMembers.size() - 1; i >= 0; i--) {
CtTypeMember m = this.typeMembers.get(i);
if (m.isImplicit() || (m.getPosition().getSourceStart() >= 0 && c.compare(member, m) > 0)) {
break;
}
insertionPosition--;
}
}
return addTypeMemberAt(insertionPosition, member);
}
use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class CtTypeImpl method getNestedType.
@Override
@SuppressWarnings("unchecked")
public <N extends CtType<?>> N getNestedType(final String name) {
class NestedTypeScanner extends EarlyTerminatingScanner<CtType<?>> {
private boolean checkType(CtType<?> type) {
if (type.getSimpleName().equals(name) && CtTypeImpl.this.equals(type.getDeclaringType())) {
setResult(type);
terminate();
return true;
}
return false;
}
@Override
public <U> void visitCtClass(spoon.reflect.declaration.CtClass<U> ctClass) {
if (!checkType(ctClass)) {
final List<CtTypeMember> typeMembers = new ArrayList<>();
for (CtTypeMember typeMember : ctClass.getTypeMembers()) {
if (typeMember instanceof CtType || typeMember instanceof CtConstructor || typeMember instanceof CtMethod) {
typeMembers.add(typeMember);
}
}
scan(typeMembers);
}
}
@Override
public <U> void visitCtInterface(spoon.reflect.declaration.CtInterface<U> intrface) {
if (!checkType(intrface)) {
final List<CtTypeMember> typeMembers = new ArrayList<>();
for (CtTypeMember typeMember : intrface.getTypeMembers()) {
if (typeMember instanceof CtType || typeMember instanceof CtMethod) {
typeMembers.add(typeMember);
}
}
scan(typeMembers);
}
}
@Override
public <U extends java.lang.Enum<?>> void visitCtEnum(spoon.reflect.declaration.CtEnum<U> ctEnum) {
if (!checkType(ctEnum)) {
final List<CtTypeMember> typeMembers = new ArrayList<>();
for (CtTypeMember typeMember : ctEnum.getTypeMembers()) {
if (typeMember instanceof CtType || typeMember instanceof CtConstructor || typeMember instanceof CtMethod) {
typeMembers.add(typeMember);
}
}
scan(typeMembers);
}
}
@Override
public <A extends Annotation> void visitCtAnnotationType(CtAnnotationType<A> annotationType) {
if (!checkType(annotationType)) {
scan(annotationType.getNestedTypes());
}
}
}
NestedTypeScanner scanner = new NestedTypeScanner();
scanner.scan(this);
return (N) scanner.getResult();
}
use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class CtExecutableReferenceImpl method isOverriding.
@Override
public boolean isOverriding(CtExecutableReference<?> executable) {
CtExecutable<?> exec = executable.getExecutableDeclaration();
CtExecutable<?> thisExec = getExecutableDeclaration();
if (exec == null || thisExec == null) {
// the declaration of this executable is not in spoon model
// use light detection algorithm, which ignores generic types
final boolean isSame = getSimpleName().equals(executable.getSimpleName()) && getParameters().equals(executable.getParameters()) && getActualTypeArguments().equals(executable.getActualTypeArguments());
if (!isSame) {
return false;
}
if (!getDeclaringType().isSubtypeOf(executable.getDeclaringType())) {
return false;
}
return true;
}
if (exec instanceof CtMethod<?> && thisExec instanceof CtMethod<?>) {
return new ClassTypingContext(((CtTypeMember) thisExec).getDeclaringType()).isOverriding((CtMethod<?>) thisExec, (CtMethod<?>) exec);
}
// it is not a method. So we can return true only if it is reference to the this executable
return exec == getDeclaration();
}
use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class TypeReferenceScanner method visitCtInterface.
@Override
public <T> void visitCtInterface(CtInterface<T> intrface) {
addReference(intrface.getReference());
for (CtTypeMember typeMember : intrface.getTypeMembers()) {
if (typeMember instanceof CtType) {
addReference(((CtType) typeMember).getReference());
}
}
super.visitCtInterface(intrface);
}
Aggregations