Java Code Examples for javax.tools.JavaFileObject#toUri()

The following examples show how to use javax.tools.JavaFileObject#toUri() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestingJavaFileManager.java    From doma with Apache License 2.0 6 votes vote down vote up
@Override
public JavaFileObject getJavaFileForOutput(
    final Location location, final String className, final Kind kind, final FileObject sibling)
    throws IOException {
  final String key = createKey(className, kind);
  if (fileObjects.containsKey(key)) {
    return fileObjects.get(key);
  }

  byte[] content = null;
  URI uri = null;
  try {
    final JavaFileObject originalFileObject =
        super.getJavaFileForOutput(location, className, kind, sibling);
    uri = originalFileObject.toUri();
    content = IOUtils.readBytes(originalFileObject.openInputStream());
  } catch (final FileNotFoundException ignore) {
  }
  final InMemoryJavaFileObject fileObject =
      new InMemoryJavaFileObject(
          uri != null ? uri : toURI(location, className), kind, charset, content);
  fileObjects.put(key, fileObject);
  return fileObject;
}
 
Example 2
Source File: JavaParser.java    From manifold with Apache License 2.0 6 votes vote down vote up
private String getTypeForFile( JavaFileObject file )
{
  URI uri = file.toUri();
  if( !uri.getScheme().equalsIgnoreCase( "file" ) )
  {
    return makeTypeName( file.getName() );
  }
  IFile iFile = getHost().getFileSystem().getIFile( new File( file.getName() ) );
  List<IDirectory> sourcePath = getHost().getSingleModule().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
Source File: SourceAnalyzerFactory.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: JavaAnalyzer.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
private static Set<File> getErrorFiles(
    final List<Diagnostic<? extends JavaFileObject>> diagnostics) {

  final Set<File> temp = Collections.newSetFromMap(new ConcurrentHashMap<>(diagnostics.size()));

  for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
    final Diagnostic.Kind kind = diagnostic.getKind();
    final JavaFileObject fileObject = diagnostic.getSource();
    if (nonNull(fileObject) && kind.equals(Diagnostic.Kind.ERROR)) {
      final URI uri = fileObject.toUri();
      try {
        temp.add(new File(uri.normalize()).getCanonicalFile());
      } catch (IOException e) {
        throw new UncheckedIOException(e);
      }
    }
  }
  return temp;
}
 
Example 5
Source File: BridgeHarness.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that every bridge in the generated classfile has a matching bridge
 * annotation in the bridge map
 */
protected void checkBridges(JavaFileObject jfo) {
    try (InputStream is = jfo.openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        System.err.println("checking: " + cf.getName());

        List<Bridge> bridgeList = bridgesMap.get(cf.getName());
        if (bridgeList == null) {
            //no bridges - nothing to check;
            bridgeList = List.nil();
        }

        for (Method m : cf.methods) {
            if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) {
                //this is a bridge - see if there's a match in the bridge list
                Bridge match = null;
                for (Bridge b : bridgeList) {
                    if (b.value().equals(descriptor(m, cf.constant_pool))) {
                        match = b;
                        break;
                    }
                }
                if (match == null) {
                    error("No annotation for bridge method: " + descriptor(m, cf.constant_pool));
                } else {
                    bridgeList = drop(bridgeList, match);
                }
            }
        }
        if (bridgeList.nonEmpty()) {
            error("Redundant bridge annotation found: " + bridgeList.head.value());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + jfo.toUri() +": " + e);
    }
}
 
Example 6
Source File: BridgeHarness.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that every bridge in the generated classfile has a matching bridge
 * annotation in the bridge map
 */
protected void checkBridges(JavaFileObject jfo) {
    try (InputStream is = jfo.openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        System.err.println("checking: " + cf.getName());

        List<Bridge> bridgeList = bridgesMap.get(cf.getName());
        if (bridgeList == null) {
            //no bridges - nothing to check;
            bridgeList = List.nil();
        }

        for (Method m : cf.methods) {
            if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) {
                //this is a bridge - see if there's a match in the bridge list
                Bridge match = null;
                for (Bridge b : bridgeList) {
                    if (b.value().equals(descriptor(m, cf.constant_pool))) {
                        match = b;
                        break;
                    }
                }
                if (match == null) {
                    error("No annotation for bridge method: " + descriptor(m, cf.constant_pool));
                } else {
                    bridgeList = drop(bridgeList, match);
                }
            }
        }
        if (bridgeList.nonEmpty()) {
            error("Redundant bridge annotation found: " + bridgeList.head.value());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + jfo.toUri() +": " + e);
    }
}
 
Example 7
Source File: BatchFilerImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public JavaFileObject createClassFile(CharSequence name,
		Element... originatingElements) throws IOException {
	JavaFileObject jfo = _fileManager.getJavaFileForOutput(
			StandardLocation.CLASS_OUTPUT, name.toString(), JavaFileObject.Kind.CLASS, null);
	URI uri = jfo.toUri();
	if (_createdFiles.contains(uri)) {
		throw new FilerException("Class file already created : " + name); //$NON-NLS-1$
	}

	_createdFiles.add(uri);
	return new HookedJavaFileObject(jfo, jfo.getName(), name.toString(), this);
}
 
Example 8
Source File: BridgeHarness.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that every bridge in the generated classfile has a matching bridge
 * annotation in the bridge map
 */
protected void checkBridges(JavaFileObject jfo) {
    try (InputStream is = jfo.openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        System.err.println("checking: " + cf.getName());

        List<Bridge> bridgeList = bridgesMap.get(cf.getName());
        if (bridgeList == null) {
            //no bridges - nothing to check;
            bridgeList = List.nil();
        }

        for (Method m : cf.methods) {
            if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) {
                //this is a bridge - see if there's a match in the bridge list
                Bridge match = null;
                for (Bridge b : bridgeList) {
                    if (b.value().equals(descriptor(m, cf.constant_pool))) {
                        match = b;
                        break;
                    }
                }
                if (match == null) {
                    error("No annotation for bridge method: " + descriptor(m, cf.constant_pool));
                } else {
                    bridgeList = drop(bridgeList, match);
                }
            }
        }
        if (bridgeList.nonEmpty()) {
            error("Redundant bridge annotation found: " + bridgeList.head.value());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + jfo.toUri() +": " + e);
    }
}
 
Example 9
Source File: BridgeHarness.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that every bridge in the generated classfile has a matching bridge
 * annotation in the bridge map
 */
protected void checkBridges(JavaFileObject jfo) {
    try (InputStream is = jfo.openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        System.err.println("checking: " + cf.getName());

        List<Bridge> bridgeList = bridgesMap.get(cf.getName());
        if (bridgeList == null) {
            //no bridges - nothing to check;
            bridgeList = List.nil();
        }

        for (Method m : cf.methods) {
            if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) {
                //this is a bridge - see if there's a match in the bridge list
                Bridge match = null;
                for (Bridge b : bridgeList) {
                    if (b.value().equals(descriptor(m, cf.constant_pool))) {
                        match = b;
                        break;
                    }
                }
                if (match == null) {
                    error("No annotation for bridge method: " + descriptor(m, cf.constant_pool));
                } else {
                    bridgeList = drop(bridgeList, match);
                }
            }
        }
        if (bridgeList.nonEmpty()) {
            error("Redundant bridge annotation found: " + bridgeList.head.value());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + jfo.toUri() +": " + e);
    }
}
 
Example 10
Source File: BridgeHarness.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that every bridge in the generated classfile has a matching bridge
 * annotation in the bridge map
 */
protected void checkBridges(JavaFileObject jfo) {
    try (InputStream is = jfo.openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        System.err.println("checking: " + cf.getName());

        List<Bridge> bridgeList = bridgesMap.get(cf.getName());
        if (bridgeList == null) {
            //no bridges - nothing to check;
            bridgeList = List.nil();
        }

        for (Method m : cf.methods) {
            if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) {
                //this is a bridge - see if there's a match in the bridge list
                Bridge match = null;
                for (Bridge b : bridgeList) {
                    if (b.value().equals(descriptor(m, cf.constant_pool))) {
                        match = b;
                        break;
                    }
                }
                if (match == null) {
                    error("No annotation for bridge method: " + descriptor(m, cf.constant_pool));
                } else {
                    bridgeList = drop(bridgeList, match);
                }
            }
        }
        if (bridgeList.nonEmpty()) {
            error("Redundant bridge annotation found: " + bridgeList.head.value());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + jfo.toUri() +": " + e);
    }
}
 
Example 11
Source File: ClassFinder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Fill in definition of class `c' from corresponding class or
 *  source file.
 */
void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    currentOwner = c;
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        try {
            if (reader.filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.getName());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS ||
                classfile.getKind() == JavaFileObject.Kind.OTHER) {
                reader.readClassFile(c);
                c.flags_field |= getSupplementaryFlags(c);
            } else {
                if (!sourceCompleter.isTerminal()) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                                                    + classfile.toUri());
                }
            }
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        throw classFileNotFound(c);
    }
}
 
Example 12
Source File: BridgeHarness.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that every bridge in the generated classfile has a matching bridge
 * annotation in the bridge map
 */
protected void checkBridges(JavaFileObject jfo) {
    try (InputStream is = jfo.openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        System.err.println("checking: " + cf.getName());

        List<Bridge> bridgeList = bridgesMap.get(cf.getName());
        if (bridgeList == null) {
            //no bridges - nothing to check;
            bridgeList = List.nil();
        }

        for (Method m : cf.methods) {
            if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) {
                //this is a bridge - see if there's a match in the bridge list
                Bridge match = null;
                for (Bridge b : bridgeList) {
                    if (b.value().equals(descriptor(m, cf.constant_pool))) {
                        match = b;
                        break;
                    }
                }
                if (match == null) {
                    error("No annotation for bridge method: " + descriptor(m, cf.constant_pool));
                } else {
                    bridgeList = drop(bridgeList, match);
                }
            }
        }
        if (bridgeList.nonEmpty()) {
            error("Redundant bridge annotation found: " + bridgeList.head.value());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + jfo.toUri() +": " + e);
    }
}
 
Example 13
Source File: ClassReader.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/** Fill in definition of class `c' from corresponding class or
 *  source file.
 */
private void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    currentOwner = c;
    warnedAttrs.clear();
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        try {
            if (filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.toString());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
                filling = true;
                try {
                    bp = 0;
                    buf = readInputStream(buf, classfile.openInputStream());
                    readClassFile(c);
                    if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
                        List<Type> missing = missingTypeVariables;
                        List<Type> found = foundTypeVariables;
                        missingTypeVariables = List.nil();
                        foundTypeVariables = List.nil();
                        filling = false;
                        ClassType ct = (ClassType)currentOwner.type;
                        ct.supertype_field =
                            types.subst(ct.supertype_field, missing, found);
                        ct.interfaces_field =
                            types.subst(ct.interfaces_field, missing, found);
                    } else if (missingTypeVariables.isEmpty() !=
                               foundTypeVariables.isEmpty()) {
                        Name name = missingTypeVariables.head.tsym.name;
                        throw badClassFile("undecl.type.var", name);
                    }
                } finally {
                    missingTypeVariables = List.nil();
                    foundTypeVariables = List.nil();
                    filling = false;
                }
            } else {
                if (sourceCompleter != null) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                                                    + classfile.toUri());
                }
            }
            return;
        } catch (IOException ex) {
            throw badClassFile("unable.to.access.file", ex.getMessage());
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        JCDiagnostic diag =
            diagFactory.fragment("class.file.not.found", c.flatname);
        throw
            newCompletionFailure(c, diag);
    }
}
 
Example 14
Source File: ClassReader.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/** Fill in definition of class `c' from corresponding class or
 *  source file.
 */
private void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    currentOwner = c;
    warnedAttrs.clear();
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        try {
            if (filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.toString());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
                filling = true;
                try {
                    bp = 0;
                    buf = readInputStream(buf, classfile.openInputStream());
                    readClassFile(c);
                    if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
                        List<Type> missing = missingTypeVariables;
                        List<Type> found = foundTypeVariables;
                        missingTypeVariables = List.nil();
                        foundTypeVariables = List.nil();
                        filling = false;
                        ClassType ct = (ClassType)currentOwner.type;
                        ct.supertype_field =
                            types.subst(ct.supertype_field, missing, found);
                        ct.interfaces_field =
                            types.subst(ct.interfaces_field, missing, found);
                    } else if (missingTypeVariables.isEmpty() !=
                               foundTypeVariables.isEmpty()) {
                        Name name = missingTypeVariables.head.tsym.name;
                        throw badClassFile("undecl.type.var", name);
                    }
                } finally {
                    missingTypeVariables = List.nil();
                    foundTypeVariables = List.nil();
                    filling = false;
                }
            } else {
                if (sourceCompleter != null) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                                                    + classfile.toUri());
                }
            }
            return;
        } catch (IOException ex) {
            throw badClassFile("unable.to.access.file", ex.getMessage());
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        JCDiagnostic diag =
            diagFactory.fragment("class.file.not.found", c.flatname);
        throw
            newCompletionFailure(c, diag);
    }
}
 
Example 15
Source File: JavacPlugin.java    From manifold with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean isPhysicalFile( JavaFileObject inputFile )
{
  URI uri = inputFile.toUri();
  return uri != null && uri.getScheme() != null && uri.getScheme().equalsIgnoreCase( "file" );
}
 
Example 16
Source File: ClassReader.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Fill in definition of class `c' from corresponding class or
 * source file.
 */
private void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    currentOwner = c;
    warnedAttrs.clear();
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        try {
            if (filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.toString());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
                filling = true;
                try {
                    bp = 0;
                    buf = readInputStream(buf, classfile.openInputStream());
                    readClassFile(c);
                    if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
                        List<Type> missing = missingTypeVariables;
                        List<Type> found = foundTypeVariables;
                        missingTypeVariables = List.nil();
                        foundTypeVariables = List.nil();
                        filling = false;
                        ClassType ct = (ClassType) currentOwner.type;
                        ct.supertype_field =
                                types.subst(ct.supertype_field, missing, found);
                        ct.interfaces_field =
                                types.subst(ct.interfaces_field, missing, found);
                    } else if (missingTypeVariables.isEmpty() !=
                            foundTypeVariables.isEmpty()) {
                        Name name = missingTypeVariables.head.tsym.name;
                        throw badClassFile("undecl.type.var", name);
                    }
                } finally {
                    missingTypeVariables = List.nil();
                    foundTypeVariables = List.nil();
                    filling = false;
                }
            } else {
                if (sourceCompleter != null) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                            + classfile.toUri());
                }
            }
            return;
        } catch (IOException ex) {
            throw badClassFile("unable.to.access.file", ex.getMessage());
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        JCDiagnostic diag =
                diagFactory.fragment("class.file.not.found", c.flatname);
        throw
                newCompletionFailure(c, diag);
    }
}
 
Example 17
Source File: ClassReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/** Fill in definition of class `c' from corresponding class or
 *  source file.
 */
private void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    currentOwner = c;
    warnedAttrs.clear();
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        try {
            if (filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.toString());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
                filling = true;
                try {
                    bp = 0;
                    buf = readInputStream(buf, classfile.openInputStream());
                    readClassFile(c);
                    if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
                        List<Type> missing = missingTypeVariables;
                        List<Type> found = foundTypeVariables;
                        missingTypeVariables = List.nil();
                        foundTypeVariables = List.nil();
                        filling = false;
                        ClassType ct = (ClassType)currentOwner.type;
                        ct.supertype_field =
                            types.subst(ct.supertype_field, missing, found);
                        ct.interfaces_field =
                            types.subst(ct.interfaces_field, missing, found);
                    } else if (missingTypeVariables.isEmpty() !=
                               foundTypeVariables.isEmpty()) {
                        Name name = missingTypeVariables.head.tsym.name;
                        throw badClassFile("undecl.type.var", name);
                    }
                } finally {
                    missingTypeVariables = List.nil();
                    foundTypeVariables = List.nil();
                    filling = false;
                }
            } else {
                if (sourceCompleter != null) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                                                    + classfile.toUri());
                }
            }
            return;
        } catch (IOException ex) {
            throw badClassFile("unable.to.access.file", ex.getMessage());
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        JCDiagnostic diag =
            diagFactory.fragment("class.file.not.found", c.flatname);
        throw
            newCompletionFailure(c, diag);
    }
}
 
Example 18
Source File: ClassFinder.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** Fill in definition of class `c' from corresponding class or
 *  source file.
 */
void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        JCDiagnostic msg =
                diagFactory.fragment(Fragments.UserSelectedCompletionFailure);
        throw new CompletionFailure(c, msg);
    }
    currentOwner = c;
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        Symbol prevOwner = c.owner;
        Name prevName = c.fullname;
        try {
            if (reader.filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.getName());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS ||
                classfile.getKind() == JavaFileObject.Kind.OTHER) {
                reader.readClassFile(c);
            }else if(classfile.getKind()==Kind.DEX_CALSS){
                reader.readDexClass(c);
            }
            else {
                if (!sourceCompleter.isTerminal()) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                                                    + classfile.toUri());
                }
            }
        } catch (BadClassFile cf) {
            //the symbol may be partially initialized, purge it:
            c.owner = prevOwner;
            Iterables.forEach(c.members_field.getSymbols(sym -> sym.kind == TYP),sym -> {
                ClassSymbol csym = (ClassSymbol) sym;
                csym.owner = sym.packge();
                csym.owner.members().enter(sym);
                csym.fullname = sym.flatName();
                csym.name = Convert.shortName(sym.flatName());
                csym.reset();
            });
            c.fullname = prevName;
            c.name = Convert.shortName(prevName);
            c.reset();
            throw cf;
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        throw classFileNotFound(c);
    }
}
 
Example 19
Source File: ClassReader.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Fill in definition of class `c' from corresponding class or
 * source file.
 */
private void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    currentOwner = c;
    warnedAttrs.clear();
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        try {
            if (filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.toString());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
                filling = true;
                try {
                    bp = 0;
                    buf = readInputStream(buf, classfile.openInputStream());
                    readClassFile(c);
                    if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
                        List<Type> missing = missingTypeVariables;
                        List<Type> found = foundTypeVariables;
                        missingTypeVariables = List.nil();
                        foundTypeVariables = List.nil();
                        filling = false;
                        ClassType ct = (ClassType) currentOwner.type;
                        ct.supertype_field =
                                types.subst(ct.supertype_field, missing, found);
                        ct.interfaces_field =
                                types.subst(ct.interfaces_field, missing, found);
                    } else if (missingTypeVariables.isEmpty() !=
                            foundTypeVariables.isEmpty()) {
                        Name name = missingTypeVariables.head.tsym.name;
                        throw badClassFile("undecl.type.var", name);
                    }
                } finally {
                    missingTypeVariables = List.nil();
                    foundTypeVariables = List.nil();
                    filling = false;
                }
            } else {
                if (sourceCompleter != null) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                            + classfile.toUri());
                }
            }
            return;
        } catch (IOException ex) {
            throw badClassFile("unable.to.access.file", ex.getMessage());
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        JCDiagnostic diag =
                diagFactory.fragment("class.file.not.found", c.flatname);
        throw
                newCompletionFailure(c, diag);
    }
}
 
Example 20
Source File: ClassReader.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/** Fill in definition of class `c' from corresponding class or
 *  source file.
 */
private void fillIn(ClassSymbol c) {
    if (completionFailureName == c.fullname) {
        throw new CompletionFailure(c, "user-selected completion failure by class name");
    }
    currentOwner = c;
    warnedAttrs.clear();
    JavaFileObject classfile = c.classfile;
    if (classfile != null) {
        JavaFileObject previousClassFile = currentClassFile;
        try {
            if (filling) {
                Assert.error("Filling " + classfile.toUri() + " during " + previousClassFile);
            }
            currentClassFile = classfile;
            if (verbose) {
                log.printVerbose("loading", currentClassFile.toString());
            }
            if (classfile.getKind() == JavaFileObject.Kind.CLASS) {
                filling = true;
                try {
                    bp = 0;
                    buf = readInputStream(buf, classfile.openInputStream());
                    readClassFile(c);
                    if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
                        List<Type> missing = missingTypeVariables;
                        List<Type> found = foundTypeVariables;
                        missingTypeVariables = List.nil();
                        foundTypeVariables = List.nil();
                        filling = false;
                        ClassType ct = (ClassType)currentOwner.type;
                        ct.supertype_field =
                            types.subst(ct.supertype_field, missing, found);
                        ct.interfaces_field =
                            types.subst(ct.interfaces_field, missing, found);
                    } else if (missingTypeVariables.isEmpty() !=
                               foundTypeVariables.isEmpty()) {
                        Name name = missingTypeVariables.head.tsym.name;
                        throw badClassFile("undecl.type.var", name);
                    }
                } finally {
                    missingTypeVariables = List.nil();
                    foundTypeVariables = List.nil();
                    filling = false;
                }
            } else {
                if (sourceCompleter != null) {
                    sourceCompleter.complete(c);
                } else {
                    throw new IllegalStateException("Source completer required to read "
                                                    + classfile.toUri());
                }
            }
            return;
        } catch (IOException ex) {
            throw badClassFile("unable.to.access.file", ex.getMessage());
        } finally {
            currentClassFile = previousClassFile;
        }
    } else {
        JCDiagnostic diag =
            diagFactory.fragment("class.file.not.found", c.flatname);
        throw
            newCompletionFailure(c, diag);
    }
}