use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class CtClassImpl method getConstructor.
@Override
public CtConstructor<T> getConstructor(CtTypeReference<?>... parameterTypes) {
for (CtTypeMember typeMember : getTypeMembers()) {
if (!(typeMember instanceof CtConstructor)) {
continue;
}
CtConstructor<T> c = (CtConstructor<T>) typeMember;
boolean cont = c.getParameters().size() == parameterTypes.length;
for (int i = 0; cont && (i < c.getParameters().size()) && (i < parameterTypes.length); i++) {
if (!parameterTypes[i].getQualifiedName().equals(c.getParameters().get(i).getType().getQualifiedName())) {
cont = false;
}
}
if (cont) {
return c;
}
}
return null;
}
use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class SubstitutionVisitor method substitute.
/**
* Substitutes all template parameters of element and returns substituted element.
*
* @param element to be substituted model
* @return substituted model
*/
public <E extends CtElement> List<E> substitute(E element) {
final Map<CtElement, String> elementToGeneratedByComment = addGeneratedBy ? new IdentityHashMap<CtElement, String>() : null;
if (addGeneratedBy) {
/*
* collect 'generated by' comments for each type member of the substituted element, before the substitution is done,
* so we know the origin names of the members.
*/
final CtInheritanceScanner internalScanner = new CtInheritanceScanner() {
public void scanCtTypeMember(CtTypeMember typeMeber) {
elementToGeneratedByComment.put(typeMeber, getGeneratedByComment(typeMeber));
}
};
new CtScanner() {
@Override
public void scan(CtElement p_element) {
internalScanner.scan(p_element);
super.scan(p_element);
}
}.scan(element);
}
List<E> result = createContext().substitute(element);
if (addGeneratedBy) {
// add generated by comments after substitution, otherwise they would be substituted in comments too.
applyGeneratedByComments(elementToGeneratedByComment);
}
return result;
}
use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class Substitution method checkTemplateContracts.
private static <T> void checkTemplateContracts(CtClass<T> c) {
for (CtField f : c.getFields()) {
Parameter templateParamAnnotation = f.getAnnotation(Parameter.class);
if (templateParamAnnotation != null && !templateParamAnnotation.value().equals("")) {
String proxyName = templateParamAnnotation.value();
// contract: if value, then the field type must be String or CtTypeReference
String fieldTypeQName = f.getType().getQualifiedName();
if (fieldTypeQName.equals(String.class.getName())) {
// contract: the name of the template parameter must correspond to the name of the field
// as found, by Pavel, this is not good contract because it prevents easy refactoring of templates
// we remove it but keep th commented code in case somebody would come up with this bad idae
// if (!f.getSimpleName().equals("_" + f.getAnnotation(Parameter.class).value())) {
// throw new TemplateException("the field name of a proxy template parameter must be called _" + f.getSimpleName());
// }
// contract: if a proxy parameter is declared and named "x" (@Parameter("x")), then a type member named "x" must exist.
boolean found = false;
for (CtTypeMember member : c.getTypeMembers()) {
if (member.getSimpleName().equals(proxyName)) {
found = true;
}
}
if (!found) {
throw new TemplateException("if a proxy parameter is declared and named \"" + proxyName + "\", then a type member named \"\" + proxyName + \"\" must exist.");
}
} else if (fieldTypeQName.equals(CtTypeReference.class.getName())) {
// OK it is CtTypeReference
} else {
throw new TemplateException("proxy template parameter must be typed as String or CtTypeReference, but it is " + fieldTypeQName);
}
}
}
}
use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class Substitution method insertAll.
/**
* Inserts all the methods, fields, constructors, initialization blocks (if
* target is a class), inner types, and super interfaces (except
* {@link Template}) from a given template by substituting all the template
* parameters by their values. Members annotated with
* {@link spoon.template.Local} or {@link Parameter} are not inserted.
*
* @param targetType
* the target type
* @param template
* the source template
*/
public static <T extends Template<?>> void insertAll(CtType<?> targetType, T template) {
CtClass<T> templateClass = getTemplateCtClass(targetType, template);
// insert all the interfaces
insertAllSuperInterfaces(targetType, template);
// insert all the methods
insertAllMethods(targetType, template);
// insert all the constructors and all the initialization blocks (only for classes)
insertAllConstructors(targetType, template);
for (CtTypeMember typeMember : templateClass.getTypeMembers()) {
if (typeMember instanceof CtField) {
// insert all the fields
insertGeneratedField(targetType, template, (CtField<?>) typeMember);
} else if (typeMember instanceof CtType) {
// insert all the inner types
insertGeneratedNestedType(targetType, template, (CtType) typeMember);
}
}
}
use of spoon.reflect.declaration.CtTypeMember in project spoon by INRIA.
the class ElementPrinterHelper method writeElementList.
public void writeElementList(List<CtTypeMember> elements) {
for (CtTypeMember element : elements) {
if (element instanceof CtConstructor && element.isImplicit()) {
continue;
}
printer.writeln();
prettyPrinter.scan(element);
if (!env.isPreserveLineNumbers()) {
printer.writeln();
}
}
}
Aggregations