@Test
  public void testPrintAMethodWithGeneric() throws Exception {
    final Launcher launcher = new Launcher();
    final Factory factory = launcher.getFactory();
    factory.getEnvironment().setAutoImports(true);
    final SpoonCompiler compiler = launcher.createCompiler();
    compiler.addInputSource(new File("./src/test/java/spoon/test/prettyprinter/testclasses/"));
    compiler.build();

    final CtClass<?> aClass = (CtClass<?>) factory.Type().get(AClass.class);
    final String expected =
        "public List<? extends ArrayList> aMethodWithGeneric() {"
            + System.lineSeparator()
            + "    return new ArrayList<>();"
            + System.lineSeparator()
            + "}";
    assertEquals(expected, aClass.getMethodsByName("aMethodWithGeneric").get(0).toString());

    final CtConstructorCall<?> constructorCall =
        aClass.getElements(new TypeFilter<CtConstructorCall<?>>(CtConstructorCall.class)).get(0);
    final CtTypeReference<?> ctTypeReference =
        constructorCall.getType().getActualTypeArguments().get(0);
    assertTrue(ctTypeReference instanceof CtImplicitTypeReference);
    assertEquals("Object", ctTypeReference.getSimpleName());
  }
Esempio n. 2
0
  /**
   * Get the CtClass object linked to a Class object
   *
   * @param object
   * @return
   */
  public static CtClass<?> getClassElement(
      String[] sourcefolder, String cp, final String classFullName) {
    Factory factory = SpoonHelpers.obtainFactory();
    SpoonCompiler compiler = new JDTBasedSpoonCompiler(factory);

    for (String srcitem : sourcefolder) {
      compiler.addInputSource(new File(srcitem));
    }

    // Updating classpath
    if (cp != null) compiler.setSourceClasspath(cp);

    // Build (in memory)
    compiler.build();

    List<CtClass<?>> elements =
        Query.getElements(
            factory,
            new Filter<CtClass<?>>() {
              @Override
              public boolean matches(CtClass<?> arg0) {
                return arg0.getQualifiedName().equals(classFullName);
              }
            });

    if (elements.size() == 1) return elements.get(0);
    else return null;
  }
Esempio n. 3
0
  @Before
  public void setUp() throws Exception {
    final File testDirectory = new File("./src/test/java/spoon/test/interfaces/testclasses/");

    final Launcher launcher = new Launcher();

    this.factory = launcher.createFactory();
    factory.getEnvironment().setComplianceLevel(8);
    SpoonCompiler compiler = launcher.createCompiler(this.factory);

    compiler.addInputSource(testDirectory);
    compiler.build();
  }