private ITypeModel processTypeReference(CtTypeReference<?> typeReference) {
    String qualifiedName = typeReference.getQualifiedName();
    ITypeModel existingType = registry.getType(qualifiedName);
    if (existingType != null) {
      return new ProxyType(registry, qualifiedName);
    }

    CtClass<Object> ctClass = factory.Class().get(qualifiedName);
    if (ctClass != null) {
      return processType(ctClass);
    }

    CtType<Object> ctType = factory.Type().get(qualifiedName);
    if (ctType != null) {
      return processType(ctType);
    }

    TypeModel type = new TypeModel();
    type.setFullyQualifiedName(qualifiedName);
    registry.registerType(type);

    fillReference(type, typeReference);

    Collection<CtExecutableReference<?>> methods = typeReference.getDeclaredExecutables();
    for (CtExecutableReference<?> m : methods) {
      IMethodModel methodModel = processMethodReference(m);
      type.addMethod(methodModel);
    }
    Collection<CtFieldReference<?>> fields = typeReference.getDeclaredFields();
    for (CtFieldReference<?> m : fields) {
      IFieldModel methodModel = processFieldReference(m);
      type.addField(methodModel);
    }
    return new ProxyType(registry, qualifiedName);
  }
Exemplo n.º 2
0
  @Test
  public void testClone() throws Exception {
    final Factory factory = build(Adobada.class);
    final CtClass<Adobada> adobada = factory.Class().get(Adobada.class);
    final CtMethod<?> m2 = adobada.getMethod("m2");

    CtMethod<?> clone = m2.clone();
    clone.setVisibility(ModifierKind.PRIVATE);

    assertEquals(ModifierKind.PUBLIC, m2.getModifiers().iterator().next());
  }
Exemplo n.º 3
0
 @Before
 public void setUp() throws Exception {
   SpoonAPI launcher = new Launcher();
   launcher.run(
       new String[] {
         "-i", "./src/test/java/spoon/test/constructor/testclasses/",
         "-o", "./target/spooned/"
       });
   factory = launcher.getFactory();
   aClass = factory.Class().get(Tacos.class);
 }
Exemplo n.º 4
0
 @Test
 public void callParamConstructor() throws Exception {
   CtClass<Object> aClass = factory.Class().get(AClass.class);
   CtConstructor<Object> constructor = aClass.getConstructors().iterator().next();
   assertEquals(
       "{"
           + System.lineSeparator()
           + "    enclosingInstance.super();"
           + System.lineSeparator()
           + "}",
       constructor.getBody().toString());
 }
Exemplo n.º 5
0
  public CtStatement apply(CtType<?> targetType) {
    CtClass<?> c;
    Factory factory;

    // we first need a factory
    if (targetType != null) {
      // if it's template with reference replacement
      factory = targetType.getFactory();
    } else {
      // else we have at least one template parameter with a factory
      factory = getFactory();
    }

    c = factory.Class().get(this.getClass());
    if (c == null) {
      c = factory.Class().get(this.getClass());
    }
    // we substitute the first statement of method statement
    CtStatement result =
        factory.Core().clone(c.getMethod("statement").getBody().getStatements().get(0));
    new SubstitutionVisitor(factory, targetType, this).scan(result);
    return result;
  }
Exemplo n.º 6
0
 @Test
 public void testBasicAPIUsage() throws Exception {
   // this test shows a basic usage of the Launcher API without command line
   // and asserts there is no exception
   SpoonAPI spoon = new Launcher();
   spoon.addInputResource("src/test/resources/spoon/test/api");
   spoon.setSourceOutputDirectory("target/spooned");
   spoon.run();
   Factory factory = spoon.getFactory();
   for (CtPackage p : factory.Package().getAll()) {
     spoon.getEnvironment().debugMessage("package: " + p.getQualifiedName());
   }
   for (CtType<?> s : factory.Class().getAll()) {
     spoon.getEnvironment().debugMessage("class: " + s.getQualifiedName());
   }
 }
Exemplo n.º 7
0
 @Test
 public void testAddSameMethodsTwoTimes() throws Exception {
   final Factory factory = createFactory();
   final CtClass<Object> tacos = factory.Class().create("Tacos");
   final CtMethod<Void> method =
       factory.Method()
           .create(
               tacos,
               new HashSet<>(),
               factory.Type().voidType(),
               "m",
               new ArrayList<>(),
               new HashSet<>());
   try {
     tacos.addMethod(method.clone());
   } catch (ConcurrentModificationException e) {
     fail();
   }
 }