Search in sources :

Example 11 with CtInterface

use of spoon.reflect.declaration.CtInterface in project spoon by INRIA.

the class SpoonMetaModel method initializeConcept.

/**
 * is called once for each {@link MetamodelConcept}, to initialize it.
 * @param type a class or inteface of the spoon model element
 * @param mmConcept to be initialize {@link MetamodelConcept}
 */
private void initializeConcept(CtType<?> type, MetamodelConcept mmConcept) {
    // it is not initialized yet. Do it now
    if (type instanceof CtInterface<?>) {
        CtInterface<?> iface = (CtInterface<?>) type;
        mmConcept.setModelClass(getImplementationOfInterface(iface));
        mmConcept.setModelInterface(iface);
    } else if (type instanceof CtClass<?>) {
        CtClass<?> clazz = (CtClass<?>) type;
        mmConcept.setModelClass(clazz);
        mmConcept.setModelInterface(getInterfaceOfImplementation(clazz));
    } else {
        throw new SpoonException("Unexpected spoon model type: " + type.getQualifiedName());
    }
    // add fields of class
    if (mmConcept.getModelClass() != null) {
        addFieldsOfType(mmConcept, mmConcept.getModelClass());
    }
    // add fields of interface
    if (mmConcept.getModelInterface() != null) {
        // add fields of interface too. They are not added by above call of addFieldsOfType, because the MetamodelConcept already exists in nameToConcept
        addFieldsOfType(mmConcept, mmConcept.getModelInterface());
    }
    // initialize all fields
    mmConcept.getRoleToProperty().forEach((role, mmField) -> {
        // if there are more methods for the same field then choose the one which best matches the field type
        mmField.sortByBestMatch();
        // finally initialize value type of this field
        mmField.setValueType(mmField.detectValueType());
    });
}
Also used : CtInterface(spoon.reflect.declaration.CtInterface) CtClass(spoon.reflect.declaration.CtClass) SpoonException(spoon.SpoonException)

Example 12 with CtInterface

use of spoon.reflect.declaration.CtInterface in project spoon by INRIA.

the class ProcessingTest method testCallProcessorWithMultipleTypes.

@Test
public void testCallProcessorWithMultipleTypes() {
    // contract: when calling a processor capable of treating CtClass and another capable of treating CtType, they are called on the right types
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/imports/testclasses");
    CtClassProcessor classProcessor = new CtClassProcessor();
    spoon.addProcessor(classProcessor);
    CtTypeProcessor typeProcessor = new CtTypeProcessor();
    spoon.addProcessor(typeProcessor);
    CtInterfaceProcessor interfaceProcessor = new CtInterfaceProcessor();
    spoon.addProcessor(interfaceProcessor);
    spoon.run();
    assertFalse(classProcessor.elements.isEmpty());
    for (CtType type : classProcessor.elements) {
        assertTrue("Type " + type.getSimpleName() + " is not a class", type instanceof CtClass);
    }
    assertFalse(classProcessor.elements.isEmpty());
    for (CtType type : interfaceProcessor.elements) {
        assertTrue("Type " + type.getSimpleName() + " is not an interface", type instanceof CtInterface);
    }
    assertFalse(typeProcessor.elements.isEmpty());
    for (CtType type : typeProcessor.elements) {
        if (type instanceof CtClass) {
            assertTrue(classProcessor.elements.contains(type));
            assertFalse(interfaceProcessor.elements.contains(type));
        } else if (type instanceof CtInterface) {
            assertFalse(classProcessor.elements.contains(type));
            assertTrue(interfaceProcessor.elements.contains(type));
        } else {
            assertFalse(classProcessor.elements.contains(type));
            assertFalse(interfaceProcessor.elements.contains(type));
        }
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtInterface(spoon.reflect.declaration.CtInterface) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtInterfaceProcessor(spoon.test.processing.testclasses.CtInterfaceProcessor) CtClassProcessor(spoon.test.processing.testclasses.CtClassProcessor) CtTypeProcessor(spoon.test.processing.testclasses.CtTypeProcessor) Test(org.junit.Test)

Aggregations

CtInterface (spoon.reflect.declaration.CtInterface)12 CtMethod (spoon.reflect.declaration.CtMethod)8 Test (org.junit.Test)7 CtClass (spoon.reflect.declaration.CtClass)7 Launcher (spoon.Launcher)6 CtType (spoon.reflect.declaration.CtType)4 ArrayList (java.util.ArrayList)3 SpoonException (spoon.SpoonException)3 CtElement (spoon.reflect.declaration.CtElement)3 Factory (spoon.reflect.factory.Factory)3 CtBlock (spoon.reflect.code.CtBlock)2 CtIf (spoon.reflect.code.CtIf)2 CtConstructor (spoon.reflect.declaration.CtConstructor)2 CtField (spoon.reflect.declaration.CtField)2 CtParameter (spoon.reflect.declaration.CtParameter)2 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)2 CtInheritanceScanner (spoon.reflect.visitor.CtInheritanceScanner)2 File (java.io.File)1 Collection (java.util.Collection)1 List (java.util.List)1