Java Code Examples for org.eclipse.jdt.internal.compiler.util.Util#getFileCharContent()
The following examples show how to use
org.eclipse.jdt.internal.compiler.util.Util#getFileCharContent() .
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: ExtractAnnotations.java From javaide with GNU General Public License v3.0 | 6 votes |
private boolean addSources(List<ICompilationUnit> sourceUnits, File file) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); if (files != null) { for (File sub : files) { addSources(sourceUnits, sub); } } } else if (file.getPath().endsWith(DOT_JAVA) && file.isFile()) { char[] contents = Util.getFileCharContent(file, encoding); ICompilationUnit unit = new CompilationUnit(contents, file.getPath(), encoding); return sourceUnits.add(unit); } return false; }
Example 2
Source File: ExtractAnnotationsDriver.java From javaide with GNU General Public License v3.0 | 5 votes |
@NonNull private static Pair<Collection<CompilationUnitDeclaration>, INameEnvironment> parseSources( @NonNull List<File> sourcePaths, @NonNull List<String> classpath, @NonNull String encoding, long languageLevel) throws IOException { List<ICompilationUnit> sourceUnits = Lists.newArrayListWithExpectedSize(100); for (File source : gatherJavaSources(sourcePaths)) { char[] contents = Util.getFileCharContent(source, encoding); ICompilationUnit unit = new CompilationUnit(contents, source.getPath(), encoding); sourceUnits.add(unit); } Map<ICompilationUnit, CompilationUnitDeclaration> outputMap = Maps.newHashMapWithExpectedSize( sourceUnits.size()); CompilerOptions options = EcjParser.createCompilerOptions(); options.docCommentSupport = true; // So I can find @hide // Note: We can *not* set options.ignoreMethodBodies=true because it disables // type attribution! options.sourceLevel = languageLevel; options.complianceLevel = options.sourceLevel; // We don't generate code, but just in case the parser consults this flag // and makes sure that it's not greater than the source level: options.targetJDK = options.sourceLevel; options.originalComplianceLevel = options.sourceLevel; options.originalSourceLevel = options.sourceLevel; options.inlineJsrBytecode = true; // >= 1.5 INameEnvironment environment = EcjParser.parse(options, sourceUnits, classpath, outputMap, null); Collection<CompilationUnitDeclaration> parsedUnits = outputMap.values(); return Pair.of(parsedUnits, environment); }
Example 3
Source File: BasicCompilationUnit.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public char[] getContents() { if (this.contents != null) return this.contents; // answer the cached source // otherwise retrieve it try { return Util.getFileCharContent(new File(new String(this.fileName)), this.encoding); } catch (IOException e) { // could not read file: returns an empty array } return CharOperation.NO_CHAR; }
Example 4
Source File: CompilationUnit.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public char[] getContents() { if (this.contents != null) return this.contents; // answer the cached source // otherwise retrieve it try { return Util.getFileCharContent(new File(new String(this.fileName)), this.encoding); } catch (IOException e) { this.contents = CharOperation.NO_CHAR; // assume no source if asked again throw new AbortCompilationUnit(null, e, this.encoding); } }
Example 5
Source File: JDTCompilerAdapter.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * check the compiler arguments. * Extract from files specified using @, lines marked with ADAPTER_PREFIX * These lines specify information that needs to be interpreted by us. * @param args compiler arguments to process */ private void checkCompilerArgs(String[] args) { for (int i = 0; i < args.length; i++) { if (args[i].charAt(0) == '@') { try { char[] content = Util.getFileCharContent(new File(args[i].substring(1)), null); int offset = 0; int prefixLength = ADAPTER_PREFIX.length; while ((offset = CharOperation.indexOf(ADAPTER_PREFIX, content, true, offset)) > -1) { int start = offset + prefixLength; int end = CharOperation.indexOf('\n', content, start); if (end == -1) end = content.length; while (CharOperation.isWhitespace(content[end])) { end--; } // end is inclusive, but in the API end is exclusive if (CharOperation.equals(ADAPTER_ENCODING, content, start, start + ADAPTER_ENCODING.length)) { CharOperation.replace(content, SEPARATOR_CHARS, File.separatorChar, start, end + 1); // file or folder level custom encoding start += ADAPTER_ENCODING.length; int encodeStart = CharOperation.lastIndexOf('[', content, start, end); if (start < encodeStart && encodeStart < end) { boolean isFile = CharOperation.equals(SuffixConstants.SUFFIX_java, content, encodeStart - 5, encodeStart, false); String str = String.valueOf(content, start, encodeStart - start); String enc = String.valueOf(content, encodeStart, end - encodeStart + 1); if (isFile) { if (this.fileEncodings == null) this.fileEncodings = new HashMap(); //use File to translate the string into a path with the correct File.seperator this.fileEncodings.put(str, enc); } else { if (this.dirEncodings == null) this.dirEncodings = new HashMap(); this.dirEncodings.put(str, enc); } } } else if (CharOperation.equals(ADAPTER_ACCESS, content, start, start + ADAPTER_ACCESS.length)) { // access rules for the classpath start += ADAPTER_ACCESS.length; int accessStart = CharOperation.indexOf('[', content, start, end); CharOperation.replace(content, SEPARATOR_CHARS, File.separatorChar, start, accessStart); if (start < accessStart && accessStart < end) { String path = String.valueOf(content, start, accessStart - start); String access = String.valueOf(content, accessStart, end - accessStart + 1); if (this.accessRules == null) this.accessRules = new ArrayList(); this.accessRules.add(path); this.accessRules.add(access); } } offset = end; } } catch (IOException e) { //ignore } } } }
Example 6
Source File: CompilationUnitResolver.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public static void parse( String[] sourceUnits, String[] encodings, FileASTRequestor astRequestor, int apiLevel, Map options, int flags, IProgressMonitor monitor) { try { CompilerOptions compilerOptions = new CompilerOptions(options); compilerOptions.ignoreMethodBodies = (flags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0; Parser parser = new CommentRecorderParser( new ProblemReporter( DefaultErrorHandlingPolicies.proceedWithAllProblems(), compilerOptions, new DefaultProblemFactory()), false); int unitLength = sourceUnits.length; if (monitor != null) monitor.beginTask("", unitLength); //$NON-NLS-1$ for (int i = 0; i < unitLength; i++) { char[] contents = null; String encoding = encodings != null ? encodings[i] : null; try { contents = Util.getFileCharContent(new File(sourceUnits[i]), encoding); } catch(IOException e) { // go to the next unit continue; } if (contents == null) { // go to the next unit continue; } org.eclipse.jdt.internal.compiler.batch.CompilationUnit compilationUnit = new org.eclipse.jdt.internal.compiler.batch.CompilationUnit(contents, sourceUnits[i], encoding); org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = compilationUnit; CompilationResult compilationResult = new CompilationResult(sourceUnit, 0, 0, compilerOptions.maxProblemsPerUnit); CompilationUnitDeclaration compilationUnitDeclaration = parser.dietParse(sourceUnit, compilationResult); if (compilationUnitDeclaration.ignoreMethodBodies) { compilationUnitDeclaration.ignoreFurtherInvestigation = true; // if initial diet parse did not work, no need to dig into method bodies. continue; } //fill the methods bodies in order for the code to be generated //real parse of the method.... org.eclipse.jdt.internal.compiler.ast.TypeDeclaration[] types = compilationUnitDeclaration.types; if (types != null) { for (int j = 0, typeLength = types.length; j < typeLength; j++) { types[j].parseMethods(parser, compilationUnitDeclaration); } } // convert AST CompilationUnit node = convert(compilationUnitDeclaration, parser.scanner.getSource(), apiLevel, options, false/*don't resolve binding*/, null/*no owner needed*/, null/*no binding table needed*/, flags /* flags */, monitor, true); node.setTypeRoot(null); // accept AST astRequestor.acceptAST(sourceUnits[i], node); if (monitor != null) monitor.worked(1); } } finally { if (monitor != null) monitor.done(); } }
Example 7
Source File: Main.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private String errorReportSource(CategorizedProblem problem, char[] unitSource, int bits) { //extra from the source the innacurate token //and "highlight" it using some underneath ^^^^^ //put some context around too. //this code assumes that the font used in the console is fixed size //sanity ..... int startPosition = problem.getSourceStart(); int endPosition = problem.getSourceEnd(); if (unitSource == null) { if (problem.getOriginatingFileName() != null) { try { unitSource = Util.getFileCharContent(new File(new String(problem.getOriginatingFileName())), null); } catch (IOException e) { // ignore; } } } int length; if ((startPosition > endPosition) || ((startPosition < 0) && (endPosition < 0)) || (unitSource == null) || (length = unitSource.length) == 0) return Messages.problem_noSourceInformation; StringBuffer errorBuffer = new StringBuffer(); if ((bits & Main.Logger.EMACS) == 0) { errorBuffer.append(' ').append(Messages.bind(Messages.problem_atLine, String.valueOf(problem.getSourceLineNumber()))); errorBuffer.append(Util.LINE_SEPARATOR); } errorBuffer.append('\t'); char c; final char SPACE = '\u0020'; final char MARK = '^'; final char TAB = '\t'; //the next code tries to underline the token..... //it assumes (for a good display) that token source does not //contain any \r \n. This is false on statements ! //(the code still works but the display is not optimal !) // expand to line limits int begin; int end; for (begin = startPosition >= length ? length - 1 : startPosition; begin > 0; begin--) { if ((c = unitSource[begin - 1]) == '\n' || c == '\r') break; } for (end = endPosition >= length ? length - 1 : endPosition ; end+1 < length; end++) { if ((c = unitSource[end + 1]) == '\r' || c == '\n') break; } // trim left and right spaces/tabs while ((c = unitSource[begin]) == ' ' || c == '\t') begin++; //while ((c = unitSource[end]) == ' ' || c == '\t') end--; TODO (philippe) should also trim right, but all tests are to be updated // copy source errorBuffer.append(unitSource, begin, end-begin+1); errorBuffer.append(Util.LINE_SEPARATOR).append("\t"); //$NON-NLS-1$ // compute underline for (int i = begin; i <startPosition; i++) { errorBuffer.append((unitSource[i] == TAB) ? TAB : SPACE); } for (int i = startPosition; i <= (endPosition >= length ? length - 1 : endPosition); i++) { errorBuffer.append(MARK); } return errorBuffer.toString(); }
Example 8
Source File: Main.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private void extractContext(CategorizedProblem problem, char[] unitSource) { //sanity ..... int startPosition = problem.getSourceStart(); int endPosition = problem.getSourceEnd(); if (unitSource == null) { if (problem.getOriginatingFileName() != null) { try { unitSource = Util.getFileCharContent(new File(new String(problem.getOriginatingFileName())), null); } catch(IOException e) { // ignore } } } int length; if ((startPosition > endPosition) || ((startPosition < 0) && (endPosition < 0)) || (unitSource == null) || ((length = unitSource.length) <= 0) || (endPosition > length)) { this.parameters.put(Logger.VALUE, Messages.problem_noSourceInformation); this.parameters.put(Logger.SOURCE_START, "-1"); //$NON-NLS-1$ this.parameters.put(Logger.SOURCE_END, "-1"); //$NON-NLS-1$ printTag(Logger.SOURCE_CONTEXT, this.parameters, true, true); return; } char c; //the next code tries to underline the token..... //it assumes (for a good display) that token source does not //contain any \r \n. This is false on statements ! //(the code still works but the display is not optimal !) // expand to line limits int begin, end; for (begin = startPosition >= length ? length - 1 : startPosition; begin > 0; begin--) { if ((c = unitSource[begin - 1]) == '\n' || c == '\r') break; } for (end = endPosition >= length ? length - 1 : endPosition ; end+1 < length; end++) { if ((c = unitSource[end + 1]) == '\r' || c == '\n') break; } // trim left and right spaces/tabs while ((c = unitSource[begin]) == ' ' || c == '\t') begin++; while ((c = unitSource[end]) == ' ' || c == '\t') end--; // copy source StringBuffer buffer = new StringBuffer(); buffer.append(unitSource, begin, end - begin + 1); this.parameters.put(Logger.VALUE, String.valueOf(buffer)); this.parameters.put(Logger.SOURCE_START, Integer.toString(startPosition - begin)); this.parameters.put(Logger.SOURCE_END, Integer.toString(endPosition - begin)); printTag(Logger.SOURCE_CONTEXT, this.parameters, true, true); }