Search in sources :

Example 11 with CtTypeMember

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;
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtConstructor(spoon.reflect.declaration.CtConstructor)

Example 12 with CtTypeMember

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;
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtElement(spoon.reflect.declaration.CtElement) CtInheritanceScanner(spoon.reflect.visitor.CtInheritanceScanner) CtScanner(spoon.reflect.visitor.CtScanner)

Example 13 with CtTypeMember

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);
            }
        }
    }
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtField(spoon.reflect.declaration.CtField)

Example 14 with CtTypeMember

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);
        }
    }
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField)

Example 15 with CtTypeMember

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();
        }
    }
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtConstructor(spoon.reflect.declaration.CtConstructor)

Aggregations

CtTypeMember (spoon.reflect.declaration.CtTypeMember)22 CtType (spoon.reflect.declaration.CtType)9 CtField (spoon.reflect.declaration.CtField)6 CtConstructor (spoon.reflect.declaration.CtConstructor)5 CtMethod (spoon.reflect.declaration.CtMethod)5 Factory (spoon.reflect.factory.Factory)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Launcher (spoon.Launcher)3 CtBlock (spoon.reflect.code.CtBlock)3 CtElement (spoon.reflect.declaration.CtElement)3 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)3 ClassTypingContext (spoon.support.visitor.ClassTypingContext)3 File (java.io.File)2 Annotation (java.lang.annotation.Annotation)2 Comparator (java.util.Comparator)2 HashSet (java.util.HashSet)2 List (java.util.List)2 CtAnnotation (spoon.reflect.declaration.CtAnnotation)2 CtAnnotationType (spoon.reflect.declaration.CtAnnotationType)2