当前位置: 首页>>代码示例>>Java>>正文


Java JavaFileObject类代码示例

本文整理汇总了Java中javax.tools.JavaFileObject的典型用法代码示例。如果您正苦于以下问题:Java JavaFileObject类的具体用法?Java JavaFileObject怎么用?Java JavaFileObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


JavaFileObject类属于javax.tools包,在下文中一共展示了JavaFileObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: failsIfHasReturnType

import javax.tools.JavaFileObject; //导入依赖的package包/类
@Test public void failsIfHasReturnType() {
  JavaFileObject source = JavaFileObjects.forSourceString("test.Test", ""
      + "package test;\n"
      + "import butterknife.OnClick;\n"
      + "public class Test {\n"
      + "  @OnClick(1)\n"
      + "  public String doStuff() {\n"
      + "  }\n"
      + "}"
  );

  assertAbout(javaSource()).that(source)
      .processedWith(new ButterKnifeProcessor())
      .failsToCompile()
      .withErrorContaining("@OnClick methods must have a 'void' return type. (test.Test.doStuff)")
      .in(source).onLine(5);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:18,代码来源:OnClickTest.java

示例2: testFileManager

import javax.tools.JavaFileObject; //导入依赖的package包/类
/**
 * Verify that an alternate file manager can be specified:
 * in this case, a TestFileManager.
 */
@Test
public void testFileManager() throws Exception {
    JavaFileObject srcFile = createSimpleJavaFileObject();
    DocumentationTool tool = ToolProvider.getSystemDocumentationTool();
    StandardJavaFileManager fm = new TestFileManager();
    File outDir = getOutDir();
    fm.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(outDir));
    Iterable<? extends JavaFileObject> files = Arrays.asList(srcFile);
    DocumentationTask t = tool.getTask(null, fm, null, null, Arrays.asList("-verbose"), files);
    if (t.call()) {
        System.err.println("task succeeded");
        checkFiles(outDir, standardExpectFiles);
    } else {
        throw new Exception("task failed");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:GetTask_FileManagerTest.java

示例3: testNUnrelatedORToOneAP_creates_GeneratedFiles_WhenTwoFileToProcess

import javax.tools.JavaFileObject; //导入依赖的package包/类
@Test
public void testNUnrelatedORToOneAP_creates_GeneratedFiles_WhenTwoFileToProcess() {
  //GIVEN
  JavaFileObject source0 = forSourceString("test.Test0", "" //
      + "package test;\n" //
      + "import org.gradle.incap.Annotation1;\n" //
      + "@Annotation1\n" //
      + "public class Test0 {\n" //
      + "}\n" //
      + "@Annotation1\n" //
      + "class Test1 {\n" //
      + "}");
  JavaFileObject expected = forSourceString("NUnrelatedORToOneAP_OR_Gen0", "" //
      + "\n" //
      + "public class NUnrelatedORToOneAP_OR_Gen0 {\n" //
      + "}");

  //WHEN
  //THEN
  Truth.assertAbout(JavaSourceSubjectFactory.javaSource()).that(source0) //
      .withCompilerOptions("-Xlint:-processing","-Aincap.mapping.folder=.", "-Aincap.incremental=true") //
      .processedWith(new NUnrelatedORToOneAP()) //
      .compilesWithoutError().and().generatesSources(expected);
}
 
开发者ID:gradle,项目名称:incap,代码行数:25,代码来源:NUnrelatedORToOneAPTest.java

示例4: writeClass

import javax.tools.JavaFileObject; //导入依赖的package包/类
/**
 * Emit a class file for a given class.
 *
 * @param c The class from which a class file is generated.
 */
public JavaFileObject writeClass(ClassSymbol c)
        throws IOException, PoolOverflow, StringOverflow {
    JavaFileObject outFile
            = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
            c.flatname.toString(),
            JavaFileObject.Kind.CLASS,
            c.sourcefile);
    OutputStream out = outFile.openOutputStream();
    try {
        writeClassFile(out, c);
        if (verbose)
            log.printVerbose("wrote.file", outFile);
        out.close();
        out = null;
    } finally {
        if (out != null) {
            // if we are propogating an exception, delete the file
            out.close();
            outFile.delete();
            outFile = null;
        }
    }
    return outFile; // may be null if write failed
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:30,代码来源:ClassWriter.java

示例5: getJavaFileForOutput

import javax.tools.JavaFileObject; //导入依赖的package包/类
@Override
public JavaFileObject getJavaFileForOutput(
		final Location location,
		final String className,
		final Kind kind,
		final FileObject sibling)
		throws IOException {
	
	final URI uri = createUri(location, className, kind);
	
	if (!files.containsKey(uri)) {
		files.put(uri, new InMemoryJavaFileObject(uri));
	}
	
	return files.get(uri);
}
 
开发者ID:MatthewTamlin,项目名称:Avatar,代码行数:17,代码来源:InMemoryJavaFileManager.java

示例6: test

import javax.tools.JavaFileObject; //导入依赖的package包/类
@Override
public void test(TestCase testCase, Map<String, ? extends JavaFileObject> classes)
        throws IOException, ConstantPoolException, Descriptor.InvalidDescriptor {
    for (Map.Entry<String, ? extends JavaFileObject> entry : classes.entrySet()) {
        ClassFile classFile = readClassFile(classes.get(entry.getKey()));
        Set<String> foundMethods = new HashSet<>();
        String className = classFile.getName();
        TestCase.TestClassInfo testClassInfo = testCase.classes.get(className);
        for (Method method : classFile.methods) {
            String methodName = method.getName(classFile.constant_pool) +
                    method.descriptor.getParameterTypes(classFile.constant_pool);
            if (methodName.startsWith("<init>")) {
                methodName = methodName.replace("<init>", className);
            }
            foundMethods.add(methodName);
            echo("Testing method : " + methodName);

            TestCase.TestMethodInfo testMethod = testClassInfo.getTestMethodInfo(methodName);
            if (testMethod == null) {
                continue;
            }
            testAttributes(testMethod, classFile, method);
        }
        checkContains(foundMethods, testClassInfo.methods.keySet(), "Methods in " + className);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:RuntimeParameterAnnotationsTestBase.java

示例7: process

import javax.tools.JavaFileObject; //导入依赖的package包/类
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Map<String, Integer> map = new HashMap<>();
    Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(ContentView.class);
    for (Element element : elements) {
        TypeElement type = (TypeElement) element;
        ContentView annotation = type.getAnnotation(ContentView.class);
        String name = type.getQualifiedName().toString();
        map.put(name, annotation.value());
    }

    try {
        JavaFileObject sourceFile = filer.createSourceFile(API_PATH);
        Writer writer = sourceFile.openWriter();
        writer.write(generateCode(map));
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return true;
}
 
开发者ID:DrkCore,项目名称:ContentViewAnnotation,代码行数:24,代码来源:ContentViewProcessor.java

示例8: testConfigurationMayNotHaveNonPublicConstructors

import javax.tools.JavaFileObject; //导入依赖的package包/类
@Test
public void testConfigurationMayNotHaveNonPublicConstructors() throws IOException {
  JavaFileObject definitionClass = JavaFileObjects.forSourceLines(
      "test.TestClass",
      "package test;",
      "",
      "import org.springframework.context.annotation.Bean;",
      "import org.springframework.context.annotation.Configuration;",
      "",
      "@com.salesforce.aptspring.Verified",
      "@Configuration",
      "public class TestClass {",
      "",
      " private TestClass() {",
      " }",
      "",
      "}");

  assertAbout(javaSources())
          .that(Arrays.asList(definitionClass))
          .processedWith(new VerifiedSpringConfiguration())
          .failsToCompile()
          .withErrorContaining("@Configuration should not have any non-public constructors.")
          .in(definitionClass)
          .onLine(10);
}
 
开发者ID:salesforce,项目名称:AptSpring,代码行数:27,代码来源:ParserTests.java

示例9: findSingleModule

import javax.tools.JavaFileObject; //导入依赖的package包/类
public ModuleSymbol findSingleModule() {
    try {
        JavaFileObject src_fo = getModuleInfoFromLocation(StandardLocation.SOURCE_PATH, Kind.SOURCE);
        JavaFileObject class_fo = getModuleInfoFromLocation(StandardLocation.CLASS_OUTPUT, Kind.CLASS);
        JavaFileObject fo = (src_fo == null) ? class_fo
                : (class_fo == null) ? src_fo
                        : classFinder.preferredFileObject(src_fo, class_fo);

        ModuleSymbol msym;
        if (fo == null) {
            msym = syms.unnamedModule;
        } else {
            msym = readModule(fo);
        }

        if (msym.patchLocation == null) {
            msym.classLocation = StandardLocation.CLASS_OUTPUT;
        } else {
            msym.patchOutputLocation = StandardLocation.CLASS_OUTPUT;
        }
        return msym;

    } catch (IOException e) {
        throw new Error(e); // FIXME
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:ModuleFinder.java

示例10: testNRelatedToOneAP_creates_GeneratedFiles_WhenSingleFileToProcess

import javax.tools.JavaFileObject; //导入依赖的package包/类
@Test
public void testNRelatedToOneAP_creates_GeneratedFiles_WhenSingleFileToProcess() {
  //GIVEN
  JavaFileObject source = forSourceString("test.Test", "" //
      + "package test;\n" //
      + "import org.gradle.incap.Annotation1;\n" //
      + "@Annotation1\n" //
      + "public class Test {\n" //
      + "}");

  //WHEN
  //THEN
  //we can't test that no files were generated, but we would need it.
  Truth.assertAbout(JavaSourceSubjectFactory.javaSource()).that(source) //
      .withCompilerOptions("-Xlint:-processing","-Aincap.mapping.folder=.") //
      .processedWith(new NRelatedToOneAP()) //
      .compilesWithoutError();
}
 
开发者ID:gradle,项目名称:incap,代码行数:19,代码来源:NRelatedToOneAPTest.java

示例11: testAnnotatedClass_asBoundInGenericParameter

import javax.tools.JavaFileObject; //导入依赖的package包/类
@Test
public void testAnnotatedClass_asBoundInGenericParameter() {
  List<Diagnostic<? extends JavaFileObject>> diagnostics = compiler.compile(
      BETA, ANNOTATED_CLASS,
      JavaFileObjects.forSourceLines("example.Test",
          "package example;",
          "",
          "import com.google.common.foo.AnnotatedClass;",
          "",
          "import java.util.List;",
          "",
          "public class Test {",
          "  public static void foo(List<? super AnnotatedClass> list) {", // error
          "  }",
          "}")
  );

  compiler.assertErrorsOnLines("example/Test.java", diagnostics, 8);
}
 
开发者ID:google,项目名称:guava-beta-checker,代码行数:20,代码来源:BetaCheckerTest.java

示例12: printSource

import javax.tools.JavaFileObject; //导入依赖的package包/类
/**
 * Emit plain Java source for a class.
 *
 * @param env  The attribution environment of the outermost class
 *             containing this class.
 * @param cdef The class definition to be printed.
 */
JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
    JavaFileObject outFile
            = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
            cdef.sym.flatname.toString(),
            JavaFileObject.Kind.SOURCE,
            null);
    if (inputFiles.contains(outFile)) {
        log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile);
        return null;
    } else {
        BufferedWriter out = new BufferedWriter(outFile.openWriter());
        try {
            new Pretty(out, true).printUnit(env.toplevel, cdef);
            if (verbose)
                log.printVerbose("wrote.file", outFile);
        } finally {
            out.close();
        }
        return outFile;
    }
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:29,代码来源:JavaCompiler.java

示例13: run

import javax.tools.JavaFileObject; //导入依赖的package包/类
public void run() throws IOException {
    File srcDir = new File(System.getProperty("test.src"));
    File thisFile = new File(srcDir, getClass().getName() + ".java");

    JavaCompiler c = ToolProvider.getSystemJavaCompiler();
    try (StandardJavaFileManager fm = c.getStandardFileManager(null, null, null)) {

        List<String> opts = Arrays.asList("-proc:only", "-doe");
        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile);
        JavacTask t = (JavacTask) c.getTask(null, fm, null, opts, null, files);
        t.setProcessors(Collections.singleton(this));
        boolean ok = t.call();
        if (!ok)
            throw new Error("compilation failed");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:TestGetScope.java

示例14: getResourceName

import javax.tools.JavaFileObject; //导入依赖的package包/类
@CheckForNull
private static String getResourceName (@NullAllowed final CompilationUnitTree cu) {
    if (cu instanceof JCTree.JCCompilationUnit) {
        JavaFileObject jfo = ((JCTree.JCCompilationUnit)cu).sourcefile;
        if (jfo != null) {
            URI uri = jfo.toUri();
            if (uri != null && uri.isAbsolute()) {
                try {
                    FileObject fo = URLMapper.findFileObject(uri.toURL());
                    if (fo != null) {
                        ClassPath cp = ClassPath.getClassPath(fo,ClassPath.SOURCE);
                        if (cp != null) {
                            return cp.getResourceName(fo,'.',false);
                        }
                    }
                } catch (MalformedURLException e) {
                    Exceptions.printStackTrace(e);
                }
            }
        }
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:SourceAnalyzerFactory.java

示例15: getFiles

import javax.tools.JavaFileObject; //导入依赖的package包/类
@NonNull
@Override
public Iterable<JavaFileObject> getFiles(
        @NonNull final String folderName,
        @NullAllowed final ClassPath.Entry entry,
        @NullAllowed final Set<JavaFileObject.Kind> kinds,
        @NullAllowed final JavaFileFilterImplementation filter,
        final boolean recursive) throws IOException {
    final Collection<Iterable<JavaFileObject>> collector = new ArrayList<>();
    for (Archive delegate : getDelegates(this)) {
        final Iterable<JavaFileObject> it = delegate.getFiles(folderName, entry, kinds, filter, recursive);
        if (!isEmpty(it)) {
            collector.add(it);
        }
    }
    return Iterators.chained(collector);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:18,代码来源:ProxyArchive.java


注:本文中的javax.tools.JavaFileObject类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。