Java Code Examples for javax.tools.JavaFileObject.getName()

The following are Jave code examples for showing how to use getName() of the javax.tools.JavaFileObject class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: javaide   File: SpanUtil.java   View Source Code Vote up 6 votes
public static Spannable createSrcSpan(Resources resources, @NonNull Diagnostic diagnostic) {
    if (diagnostic.getSource() == null) {
        return new SpannableString("Unknown");
    }
    if (!(diagnostic.getSource() instanceof JavaFileObject)) {
        return new SpannableString(diagnostic.getSource().toString());
    }
    try {
        JavaFileObject source = (JavaFileObject) diagnostic.getSource();
        File file = new File(source.getName());
        String name = file.getName();
        String line = diagnostic.getLineNumber() + ":" + diagnostic.getColumnNumber();
        SpannableString span = new SpannableString(name + ":" + line);
        span.setSpan(new ForegroundColorSpan(resources.getColor(R.color.dark_color_diagnostic_file)),
                0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return span;
    } catch (Exception e) {

    }
    return new SpannableString(diagnostic.getSource().toString());
}
 
Example 2
Project: manifold   File: JavaParser.java   View Source Code Vote up 6 votes
private String getTypeForFile( JavaFileObject file )
{
  URI uri = file.toUri();
  if( !uri.getScheme().equalsIgnoreCase( "file" ) )
  {
    return makeTypeName( file.getName() );
  }
  IFile iFile = ManifoldHost.getFileSystem().getIFile( new File( file.getName() ) );
  List<IDirectory> sourcePath = ManifoldHost.getGlobalModule().getSourcePath();
  for( IDirectory dir : sourcePath )
  {
    if( iFile.isDescendantOf( dir ) )
    {
      return makeTypeName( iFile.getName().substring( dir.getName().length() ) );
    }
  }
  throw new IllegalStateException( "Could not infer type name from: " + file.getName() );
}
 
Example 3
Project: javaide   File: AbstractDiagnosticFormatter.java   View Source Code Vote up 5 votes
public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
    JavaFileObject fo = d.getSource();
    if (fo == null)
        throw new IllegalArgumentException(); // d should have source set
    if (fullname)
        return fo.getName();
    else if (fo instanceof BaseFileObject)
        return ((BaseFileObject) fo).getShortName();
    else
        return BaseFileObject.getSimpleName(fo);
}
 
Example 4
Project: otter-G   File: JavaFileManagerImpl.java   View Source Code Vote up 5 votes
@Override
public String inferBinaryName(Location loc, JavaFileObject file) {
    if (file instanceof JavaFileObjectImpl) {
        return file.getName();
    }

    return super.inferBinaryName(loc, file);
}
 
Example 5
Project: OpenJSharp   File: AbstractDiagnosticFormatter.java   View Source Code Vote up 5 votes
public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
    JavaFileObject fo = d.getSource();
    if (fo == null)
        throw new IllegalArgumentException(); // d should have source set
    if (fullname)
        return fo.getName();
    else if (fo instanceof BaseFileObject)
        return ((BaseFileObject) fo).getShortName();
    else
        return BaseFileObject.getSimpleName(fo);
}
 
Example 6
Project: jsf-sdk   File: TemplatePrecompiler.java   View Source Code Vote up 5 votes
@SuppressWarnings("serial")
private static void matchClassNames(List<String> ret, final String className)
        throws IOException {
    String packageName = className.substring(0, className.lastIndexOf('.'));
    String relativedName = className.substring(
            className.lastIndexOf('.') + 1, className.length());
    String patName = relativedName.replace("*", "(\\w+)");
    Pattern pat = Pattern.compile(patName);

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    JavaFileManager fm = compiler.getStandardFileManager(
            new DiagnosticCollector<JavaFileObject>(), null, null);
    HashSet<JavaFileObject.Kind> kind = new HashSet<JavaFileObject.Kind>() {
        {
            add(JavaFileObject.Kind.CLASS);
        }
    };

    for (JavaFileObject f : fm.list(StandardLocation.PLATFORM_CLASS_PATH, packageName, kind, false)) {
        String relatived0 = f.getName();
        String name0 = relatived0.substring(0, relatived0.length() - ".class".length());
        Matcher m = pat.matcher(name0);
        if (m.matches()) {
            String name = packageName + '.' + name0;
            if (!ret.contains(name)) {
                ret.add(name);
            }
        }
    }
}
 
Example 7
Project: openjdk-jdk10   File: AbstractDiagnosticFormatter.java   View Source Code Vote up 5 votes
public String formatSource(JCDiagnostic d, boolean fullname, Locale l) {
    JavaFileObject fo = d.getSource();
    if (fo == null)
        throw new IllegalArgumentException(); // d should have source set
    if (fullname)
        return fo.getName();
    else if (fo instanceof PathFileObject)
        return ((PathFileObject) fo).getShortName();
    else
        return PathFileObject.getSimpleName(fo);
}
 
Example 8
Project: openjdk-jdk10   File: T6411310.java   View Source Code Vote up 5 votes
void test(String... args) throws IOException {
    JavaFileObject file = fm.getJavaFileForInput(PLATFORM_CLASS_PATH,
                                                 "java.lang.Object",
                                                 CLASS);
    String fileName = file.getName();
    if (!fileName.matches(".*java/lang/Object.class\\)?")) {
        System.err.println(fileName);
        throw new AssertionError(file);
    }
}
 
Example 9
Project: openjdk-jdk10   File: ReflectionTest.java   View Source Code Vote up 5 votes
private static void printTestSrc(Iterable<? extends JavaFileObject> files) {
    for (JavaFileObject f : files) {
        System.out.println("Test file " + f.getName() + ":");
        try {
            System.out.println("" + f.getCharContent(true));
        } catch (IOException e) {
            throw new RuntimeException(
                    "Exception when printing test src contents for class " +
                            f.getName(), e);
        }
    }

}
 
Example 10
Project: manifold   File: ManifoldJavaFileManager.java   View Source Code Vote up 5 votes
private Set<String> makeNames( Iterable<JavaFileObject> list )
{
  HashSet<String> set = new HashSet<>();
  for( JavaFileObject file : list )
  {
    String name = file.getName();
    if( name.endsWith( ".java" ) )
    {
      set.add( name.substring( name.lastIndexOf( File.separatorChar ) + 1, name.lastIndexOf( '.' ) ) );
    }
  }
  return set;
}
 
Example 11
Project: EatDubbo   File: JdkCompiler.java   View Source Code Vote up 4 votes
@Override
public String inferBinaryName(Location loc, JavaFileObject file) {
    if (file instanceof JavaFileObjectImpl)
        return file.getName();
    return super.inferBinaryName(loc, file);
}
 
Example 12
Project: dubbo2   File: JdkCompiler.java   View Source Code Vote up 4 votes
@Override
public String inferBinaryName(Location loc, JavaFileObject file) {
    if (file instanceof JavaFileObjectImpl)
        return file.getName();
    return super.inferBinaryName(loc, file);
}
 
Example 13
Project: dubbox-hystrix   File: JdkCompiler.java   View Source Code Vote up 4 votes
@Override
public String inferBinaryName(Location loc, JavaFileObject file) {
    if (file instanceof JavaFileObjectImpl)
        return file.getName();
    return super.inferBinaryName(loc, file);
}
 
Example 14
Project: openjdk-jdk10   File: Modules.java   View Source Code Vote up 4 votes
String toString(JavaFileObject fo) {
    return (fo == null) ? "--" : fo.getName();
}
 
Example 15
Project: openjdk-jdk10   File: ExplodedImage.java   View Source Code Vote up 4 votes
void testCanCompileAgainstExplodedImage(String loc) throws IOException {
    System.err.println("testCanCompileAgainstExplodedImage(" + loc + ")");
    Path javaHome = prepareJavaHome();
    Path targetPath = javaHome.resolve(loc.replace("*", "/java.base").replace("/", sep));
    try (StandardJavaFileManager fm = javaCompiler.getStandardFileManager(null, null, null)) {
        for (String pack : REQUIRED_PACKAGES) {
            Iterable<JavaFileObject> content = fm.list(StandardLocation.PLATFORM_CLASS_PATH,
                                                       pack,
                                                       EnumSet.allOf(JavaFileObject.Kind.class),
                                                       false);

            for (JavaFileObject jfo : content) {
                String name = jfo.getName();
                int lastSlash = name.lastIndexOf('/');
                name = lastSlash >= 0 ? name.substring(lastSlash + 1) : name;
                Path target = targetPath.resolve(pack.replace(".", sep) + sep + name);
                Files.createDirectories(target.getParent());
                try (InputStream in = jfo.openInputStream()) {
                    Files.copy(in, target);
                }
            }
        }
    }

    System.setProperty("java.home", javaHome.toString());

    try (StandardJavaFileManager fm = javaCompiler.getStandardFileManager(null, null, null)) {
        DiagnosticListener<JavaFileObject> noErrors = d -> {
            if (d.getKind() == Diagnostic.Kind.ERROR)
                throw new IllegalStateException("Unexpected error: " + d);
        };
        ToolBox.JavaSource inputFile =
                new ToolBox.JavaSource("import java.util.List; class Test { List l; }");
        List<JavaFileObject> inputFiles = Arrays.asList(inputFile);
        boolean result =
                javaCompiler.getTask(null, fm, noErrors, null, null, inputFiles).call();
        if (!result) {
            throw new IllegalStateException("Could not compile correctly!");
        }
        JavacTask task =
                (JavacTask) javaCompiler.getTask(null, fm, noErrors, null, null, inputFiles);
        task.parse();
        TypeElement juList = task.getElements().getTypeElement("java.util.List");
        if (juList == null)
            throw new IllegalStateException("Cannot resolve java.util.List!");
        URI listSource = ((ClassSymbol) juList).classfile.toUri();
        if (!listSource.toString().startsWith(javaHome.toUri().toString()))
            throw new IllegalStateException(  "Did not load java.util.List from correct place, " +
                                              "actual location: " + listSource.toString() +
                                            "; expected prefix: " + javaHome.toUri());
    }

    System.err.println("finished.");
}
 
Example 16
Project: dubbocloud   File: JdkCompiler.java   View Source Code Vote up 4 votes
@Override
public String inferBinaryName(Location loc, JavaFileObject file) {
    if (file instanceof JavaFileObjectImpl)
        return file.getName();
    return super.inferBinaryName(loc, file);
}