コード例 #1
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;
  }
コード例 #2
0
 @Test
 public void printerCanPrintInvocationWithoutException() throws Exception {
   String packageName = "spoon.test.subclass.prettyprinter";
   String className = "DefaultPrettyPrinterExample";
   String qualifiedName = packageName + "." + className;
   SpoonCompiler comp = new Launcher().createCompiler();
   List<SpoonResource> fileToBeSpooned =
       SpoonResourceHelper.resources(
           "./src/test/resources/printer-test/" + qualifiedName.replace('.', '/') + ".java");
   assertEquals(1, fileToBeSpooned.size());
   comp.addInputSources(fileToBeSpooned);
   List<SpoonResource> classpath =
       SpoonResourceHelper.resources(
           "./src/test/resources/printer-test/DefaultPrettyPrinterDependency.jar");
   assertEquals(1, classpath.size());
   comp.setSourceClasspath(classpath.get(0).getPath());
   comp.build();
   Factory factory = comp.getFactory();
   CtType<?> theClass = factory.Type().get(qualifiedName);
   List<CtInvocation<?>> elements =
       Query.getElements(theClass, new TypeFilter<CtInvocation<?>>(CtInvocation.class));
   assertEquals(3, elements.size());
   CtInvocation<?> mathAbsInvocation = elements.get(1);
   assertEquals("java.lang.Math.abs(message.length())", mathAbsInvocation.toString());
 }
コード例 #3
0
  @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());
  }
コード例 #4
0
 public <T extends CtType<?>> T getLoops() throws Exception {
   SpoonCompiler comp = new Launcher().createCompiler();
   comp.addInputSources(
       SpoonResourceHelper.resources(
           "../Processors/src/main/java/com/tibo/processors/util/UselessIfs.java"));
   comp.build();
   return comp.getFactory().Package().get("com.tibo.processors.util").getType("UselessIfs");
 }
コード例 #5
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();
  }
コード例 #6
0
  @Test
  public void superInvocationWithEnclosingInstance() throws Exception {

    /**
     * To extend a nested class an enclosing instance must be provided to call the super
     * constructor.
     */
    String sourcePath = "./src/test/resources/spoon/test/prettyprinter/NestedSuperCall.java";
    List<SpoonResource> files = SpoonResourceHelper.resources(sourcePath);
    assertEquals(1, files.size());

    SpoonCompiler comp = new Launcher().createCompiler();
    comp.addInputSources(files);
    comp.build();

    Factory factory = comp.getFactory();
    CtType<?> theClass = factory.Type().get("spoon.test.prettyprinter.NestedSuperCall");

    assertTrue(theClass.toString().contains("nc.super(\"a\")"));
  }