public static String formatEclipseStyle(final Properties prop, final String content) { final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(prop); final IDocument document = new Document(content); try { final TextEdit textEdit = codeFormatter.format( CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, content, 0, content.length(), 0, null); if (textEdit != null) { textEdit.apply(document); } else { return content; } } catch (final BadLocationException e) { return content; } return ensureCorrectNewLines(document.get()); }
/** * Method to check with methods share common attributes, according to * CK definition. * @author Mariana Azevedo * @since 13/07/2014 * @param methods */ private void checkMethodsWithSharedAttributes(IMethod[] methods){ IScanner scanner = null; for (IMethod method : methods) { String methodName = method.getElementName(); try { scanner = ToolFactory.createScanner(false, false, false, false); scanner.setSource(method.getSource().toCharArray()); while(true){ int charactere = scanner.getNextToken(); if (charactere == ITerminalSymbols.TokenNameEOF) break; if (charactere == ITerminalSymbols.TokenNameIdentifier) { addMethods(new String(scanner.getCurrentTokenSource()), methodName); } } } catch (JavaModelException exception1) { logger.error(exception1); } catch (InvalidInputException exception2) { logger.error(exception2); } } }
public static Long calculateSerialVersionId(ITypeBinding typeBinding, final IProgressMonitor monitor) throws CoreException, IOException { try { IFile classfileResource = getClassfile(typeBinding); if (classfileResource == null) { return null; } InputStream contents = classfileResource.getContents(); try { IClassFileReader cfReader = ToolFactory.createDefaultClassFileReader(contents, IClassFileReader.ALL); if (cfReader != null) { return calculateSerialVersionId(cfReader); } } finally { contents.close(); } return null; } finally { if (monitor != null) { monitor.done(); } } }
/** * Removes comments and whitespace * * @param reference * the type reference * @return the reference only consisting of dots and java identifier * characters */ public static String normalizeReference(String reference) { IScanner scanner = ToolFactory.createScanner(false, false, false, false); scanner.setSource(reference.toCharArray()); StringBuffer sb = new StringBuffer(); try { int tokenType = scanner.getNextToken(); while (tokenType != ITerminalSymbols.TokenNameEOF) { sb.append(scanner.getRawTokenSource()); tokenType = scanner.getNextToken(); } } catch (InvalidInputException e) { Assert.isTrue(false, reference); } reference = sb.toString(); return reference; }
private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) { if (start == end) return true; Assert.isTrue(start <= end); String trimmedText = buffer.getText(start, end - start).trim(); if (0 == trimmedText.length()) { return true; } else { IScanner scanner = ToolFactory.createScanner(false, false, false, null); scanner.setSource(trimmedText.toCharArray()); try { return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF; } catch (InvalidInputException e) { return false; } } }
/** * Creates a TokenScanner * * @param typeRoot The type root to scan on * @throws CoreException thrown if the buffer cannot be accessed */ public TokenScanner(ITypeRoot typeRoot) throws CoreException { IJavaProject project = typeRoot.getJavaProject(); IBuffer buffer = typeRoot.getBuffer(); if (buffer == null) { throw new CoreException( createError(DOCUMENT_ERROR, "Element has no source", null)); // $NON-NLS-1$ } String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScanner = ToolFactory.createScanner( true, false, true, sourceLevel, complianceLevel); // line info required fScanner.setSource(buffer.getCharacters()); fDocument = null; // use scanner for line information fEndPosition = fScanner.getSource().length - 1; }
/** * Removes comments and whitespace * * @param reference the type reference * @return the reference only consisting of dots and java identifier characters */ public static String normalizeReference(String reference) { IScanner scanner = ToolFactory.createScanner(false, false, false, false); scanner.setSource(reference.toCharArray()); StringBuffer sb = new StringBuffer(); try { int tokenType = scanner.getNextToken(); while (tokenType != ITerminalSymbols.TokenNameEOF) { sb.append(scanner.getRawTokenSource()); tokenType = scanner.getNextToken(); } } catch (InvalidInputException e) { Assert.isTrue(false, reference); } reference = sb.toString(); return reference; }
private String formatJava(IType type) throws JavaModelException { String source = type.getCompilationUnit().getSource(); CodeFormatter formatter = ToolFactory.createCodeFormatter(type.getJavaProject().getOptions(true)); TextEdit formatEdit = formatter.format(CodeFormatterFlags.getFlagsForCompilationUnitFormat(), source, 0, source.length(), 0, lineDelimiter); if (formatEdit == null) { CorePluginLog.logError("Could not format source for " + type.getCompilationUnit().getElementName()); return source; } Document document = new Document(source); try { formatEdit.apply(document); source = document.get(); } catch (BadLocationException e) { CorePluginLog.logError(e); } source = Strings.trimLeadingTabsAndSpaces(source); return source; }
/** * Removes comments and whitespace * @param reference the type reference * @return the reference only consisting of dots and java identifier characters */ public static String normalizeReference(String reference) { IScanner scanner= ToolFactory.createScanner(false, false, false, false); scanner.setSource(reference.toCharArray()); StringBuffer sb= new StringBuffer(); try { int tokenType= scanner.getNextToken(); while (tokenType != ITerminalSymbols.TokenNameEOF) { sb.append(scanner.getRawTokenSource()); tokenType= scanner.getNextToken(); } } catch (InvalidInputException e) { Assert.isTrue(false, reference); } reference= sb.toString(); return reference; }
private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) { if (start == end) return true; Assert.isTrue(start <= end); String trimmedText= buffer.getText(start, end - start).trim(); if (0 == trimmedText.length()) { return true; } else { IScanner scanner= ToolFactory.createScanner(false, false, false, null); scanner.setSource(trimmedText.toCharArray()); try { return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF; } catch (InvalidInputException e) { return false; } } }
public static Long calculateSerialVersionId(ITypeBinding typeBinding, final IProgressMonitor monitor) throws CoreException, IOException { try { IFile classfileResource= getClassfile(typeBinding); if (classfileResource == null) return null; InputStream contents= classfileResource.getContents(); try { IClassFileReader cfReader= ToolFactory.createDefaultClassFileReader(contents, IClassFileReader.ALL); if (cfReader != null) { return calculateSerialVersionId(cfReader); } } finally { contents.close(); } return null; } finally { if (monitor != null) monitor.done(); } }
/** * @param source * must be not null * @param range * can be null * @return may return null, otherwise an initialized scanner which may * answer which source offset index belongs to which source line * @throws JavaModelException */ private static IScanner initScanner(IType source, ISourceRange range) throws JavaModelException { if (range == null) { return null; } char[] charContent = getContent(source); if (charContent == null) { return null; } IScanner scanner = ToolFactory.createScanner(false, false, false, true); scanner.setSource(charContent); int offset = range.getOffset(); try { while (scanner.getNextToken() != ITerminalSymbols.TokenNameEOF) { // do nothing, just wait for the end of stream if (offset <= scanner.getCurrentTokenEndPosition()) { break; } } } catch (InvalidInputException e) { FindbugsPlugin.getDefault().logException(e, "Could not init scanner for type: " + source); } return scanner; }
public static String disassemble(IClassFile classFile) { ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler(); String disassembledByteCode = null; try { disassembledByteCode = disassembler.disassemble(classFile.getBytes(), LF, ClassFileBytesDisassembler.WORKING_COPY); disassembledByteCode = MISSING_SOURCES_HEADER + LF + disassembledByteCode; } catch (Exception e) { // JavaLanguageServerPlugin.logError("Unable to disassemble " + // classFile.getHandleIdentifier()); e.printStackTrace(); } return disassembledByteCode; }
/** * Format a Unit Source Code * * @param testInterface * @param monitor * @throws CoreException */ @SuppressWarnings("unchecked") public static void formatUnitSourceCode(IFile file, IProgressMonitor monitor) throws CoreException { @SuppressWarnings("rawtypes") SubMonitor subMonitor = SubMonitor.convert(monitor, 100); ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file); subMonitor.split(50); ICompilationUnit workingCopy = unit.getWorkingCopy(monitor); Map options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7); options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, DefaultCodeFormatterConstants.createAlignmentValue(true, DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, DefaultCodeFormatterConstants.INDENT_ON_COLUMN)); final CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(options); ISourceRange range = unit.getSourceRange(); TextEdit formatEdit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, unit.getSource(), range.getOffset(), range.getLength(), 0, null); subMonitor.split(30); if (formatEdit != null /* && formatEdit.hasChildren()*/) { workingCopy.applyTextEdit(formatEdit, monitor); workingCopy.reconcile(ICompilationUnit.NO_AST, false, null, null); workingCopy.commitWorkingCopy(true, null); workingCopy.discardWorkingCopy(); } file.refreshLocal(IResource.DEPTH_INFINITE, subMonitor); subMonitor.split(20); }
/** * Creates a JavaCodeFormatter using the default formatter options and * optionally applying user provided options on top. * * @param overrideOptions user provided options to apply on top of defaults */ public JavaCodeFormatter(final Map<String, Object> overrideOptions) { Map formatterOptions = new HashMap<>(DEFAULT_FORMATTER_OPTIONS); if (overrideOptions != null) { formatterOptions.putAll(overrideOptions); } this.codeFormatter = ToolFactory.createCodeFormatter(formatterOptions, ToolFactory.M_FORMAT_EXISTING); }
private String getContent(byte[] bytes, IProgressMonitor monitor) throws CoreException { ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler(); String disassembledByteCode = null; try { disassembledByteCode = disassembler.disassemble(bytes, LF, ClassFileBytesDisassembler.WORKING_COPY); if (disassembledByteCode != null) { disassembledByteCode = DISASSEMBLED_HEADER + disassembledByteCode; } } catch (ClassFormatException e) { throw new CoreException(new Status(Status.ERROR, "", "Error disassembling", e)); } return disassembledByteCode; }
/** * Creates a TokenScanner * @param document The textbuffer to create the scanner on * @param project the current Java project */ public TokenScanner(IDocument document, IJavaProject project) { String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScanner= ToolFactory.createScanner(true, false, false, sourceLevel, complianceLevel); // no line info required fScanner.setSource(document.get().toCharArray()); fDocument= document; fEndPosition= fScanner.getSource().length - 1; }
/** * Creates a TokenScanner * @param typeRoot The type root to scan on * @throws CoreException thrown if the buffer cannot be accessed */ public TokenScanner(ITypeRoot typeRoot) throws CoreException { IJavaProject project= typeRoot.getJavaProject(); IBuffer buffer= typeRoot.getBuffer(); if (buffer == null) { throw new CoreException(createError(DOCUMENT_ERROR, "Element has no source", null)); //$NON-NLS-1$ } String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScanner= ToolFactory.createScanner(true, false, true, sourceLevel, complianceLevel); // line info required fScanner.setSource(buffer.getCharacters()); fDocument= null; // use scanner for line information fEndPosition= fScanner.getSource().length - 1; }
private boolean isValidComment(String template) { IScanner scanner = ToolFactory.createScanner(true, false, false, false); scanner.setSource(template.toCharArray()); try { int next = scanner.getNextToken(); while (TokenScanner.isComment(next)) { next = scanner.getNextToken(); } return next == ITerminalSymbols.TokenNameEOF; } catch (InvalidInputException e) { } return false; }
protected IScanner getScanner(ICompilationUnit unit) { IJavaProject project = unit.getJavaProject(); if (project.equals(fProjectCache)) { return fScannerCache; } fProjectCache = project; String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScannerCache = ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel); return fScannerCache; }
/** * Creates a TokenScanner * * @param document The textbuffer to create the scanner on * @param project the current Java project */ public TokenScanner(IDocument document, IJavaProject project) { String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScanner = ToolFactory.createScanner( true, false, false, sourceLevel, complianceLevel); // no line info required fScanner.setSource(document.get().toCharArray()); fDocument = document; fEndPosition = fScanner.getSource().length - 1; }
protected IScanner getScanner(ICompilationUnit unit) { IJavaProject project = unit.getJavaProject(); if (project.equals(fProjectCache)) return fScannerCache; fProjectCache = project; String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScannerCache = ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel); return fScannerCache; }
public void scan(ICompilationUnit cu) throws JavaModelException { char[] chars = cu.getBuffer().getCharacters(); fMatches = new HashSet<TextMatch>(); fScanner = ToolFactory.createScanner(true, true, false, true); fScanner.setSource(chars); // IImportContainer importContainer= cu.getImportContainer(); // if (importContainer.exists()) // fNoFlyZone= importContainer.getSourceRange(); // else // fNoFlyZone= null; doScan(); fScanner = null; }
/** * Scan the given text. * * <p><strong>NOTE:</strong> Use only for testing. * * @param text the text */ public void scan(String text) { char[] chars = text.toCharArray(); fMatches = new HashSet<TextMatch>(); fScanner = ToolFactory.createScanner(true, true, false, true); fScanner.setSource(chars); doScan(); fScanner = null; }
private boolean isValidComment(String template) { IScanner scanner = ToolFactory.createScanner(true, false, false, false); scanner.setSource(template.toCharArray()); try { int next = scanner.getNextToken(); while (TokenScanner.isComment(next)) { next = scanner.getNextToken(); } return next == ITerminalSymbols.TokenNameEOF; } catch (InvalidInputException e) { // If there are lexical errors, the comment is invalid } return false; }
@SuppressWarnings({"unchecked", "deprecation"}) public void setFormatterSettings(List<Setting> settings) { // // change the option to wrap each enum constant on a new line // options.put(DefaultCodeFormatterConstants.FORMATTER_ALIGNMENT_FOR_ENUM_CONSTANTS, // DefaultCodeFormatterConstants.createAlignmentValue(true, // DefaultCodeFormatterConstants.WRAP_ONE_PER_LINE, // DefaultCodeFormatterConstants.INDENT_ON_COLUMN)); // if (settings != null) { options = newHashMap(); for (Setting s : settings) { options.put(s.getId(), s.getValue()); } } else { options = DefaultCodeFormatterConstants.getEclipseDefaultSettings(); options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_8); options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8); options.put(JavaCore.FORMATTER_LINE_SPLIT, "160"); options.put(JavaCore.FORMATTER_TAB_CHAR, JavaCore.SPACE); options.put(JavaCore.FORMATTER_TAB_SIZE, "4"); } // instanciate the default code formatter with the given options codeFormatter = ToolFactory.createCodeFormatter(options); }
/** * Formats the specified compilation unit. * * @param unit the compilation unit to format * @param monitor the monitor for the operation * @throws JavaModelException */ public static void formatUnitSourceCode(ICompilationUnit unit, IProgressMonitor monitor) throws JavaModelException { CodeFormatter formatter = ToolFactory.createCodeFormatter(null); ISourceRange range = unit.getSourceRange(); TextEdit formatEdit = formatter.format( CodeFormatter.K_COMPILATION_UNIT, unit.getSource(), range.getOffset(), range.getLength(), 0, null); if (formatEdit != null && formatEdit.hasChildren()) { unit.applyTextEdit(formatEdit, monitor); } else { monitor.done(); } }
@Override public String formatCode(IType objectClass, String source) throws JavaModelException, BadLocationException { String lineDelim = getLineDelimiterUsed(objectClass); int indent = getUsedIndentation(objectClass) + 1; TextEdit textEdit = ToolFactory.createCodeFormatter(null).format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, source, 0, source.length(), indent, lineDelim); if (textEdit == null) { return source; } Document document = new Document(source); textEdit.apply(document); return document.get(); }
private boolean isValidComment(String template) { IScanner scanner= ToolFactory.createScanner(true, false, false, false); scanner.setSource(template.toCharArray()); try { int next= scanner.getNextToken(); while (TokenScanner.isComment(next)) { next= scanner.getNextToken(); } return next == ITerminalSymbols.TokenNameEOF; } catch (InvalidInputException e) { } return false; }
protected IScanner getScanner(ICompilationUnit unit) { IJavaProject project= unit.getJavaProject(); if (project.equals(fProjectCache)) return fScannerCache; fProjectCache= project; String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScannerCache= ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel); return fScannerCache; }
public void scan(ICompilationUnit cu) throws JavaModelException { char[] chars= cu.getBuffer().getCharacters(); fMatches= new HashSet<TextMatch>(); fScanner= ToolFactory.createScanner(true, true, false, true); fScanner.setSource(chars); // IImportContainer importContainer= cu.getImportContainer(); // if (importContainer.exists()) // fNoFlyZone= importContainer.getSourceRange(); // else // fNoFlyZone= null; doScan(); fScanner= null; }
/** * Scan the given text. * <p> * <strong>NOTE:</strong> Use only for testing. * </p> * * @param text the text */ public void scan(String text) { char[] chars= text.toCharArray(); fMatches= new HashSet<TextMatch>(); fScanner= ToolFactory.createScanner(true, true, false, true); fScanner.setSource(chars); doScan(); fScanner= null; }
public static NLSLine[] scan(ICompilationUnit cu) throws JavaModelException, BadLocationException, InvalidInputException { IJavaProject javaProject= cu.getJavaProject(); IScanner scanner= null; if (javaProject != null) { String complianceLevel= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true); String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true); scanner= ToolFactory.createScanner(true, true, true, sourceLevel, complianceLevel); } else { scanner= ToolFactory.createScanner(true, true, false, true); } return scan(scanner, cu.getBuffer().getCharacters()); }
/** * A visitor that maps a selection to a given ASTNode. The result node is * determined as follows: * <ul> * <li>first the visitor tries to find a node that is covered by <code>start</code> and * <code>length</code> where either <code>start</code> and <code>length</code> exactly * matches the node or where the text covered before and after the node only consists * of white spaces or comments.</li> * <li>if no such node exists than the node that encloses the range defined by * start and end is returned.</li> * <li>if the length is zero than also nodes are considered where the node's * start or end position matches <code>start</code>.</li> * <li>otherwise <code>null</code> is returned.</li> * </ul> * * @param root the root node from which the search starts * @param start the start offset * @param length the length * @param source the source of the compilation unit * * @return the result node * @throws JavaModelException if an error occurs in the Java model * * @since 3.0 */ public static ASTNode perform(ASTNode root, int start, int length, ITypeRoot source) throws JavaModelException { NodeFinder finder= new NodeFinder(start, length); root.accept(finder); ASTNode result= finder.getCoveredNode(); if (result == null) return null; Selection selection= Selection.createFromStartLength(start, length); if (selection.covers(result)) { IBuffer buffer= source.getBuffer(); if (buffer != null) { IScanner scanner= ToolFactory.createScanner(false, false, false, false); scanner.setSource(buffer.getText(start, length).toCharArray()); try { int token= scanner.getNextToken(); if (token != ITerminalSymbols.TokenNameEOF) { int tStart= scanner.getCurrentTokenStartPosition(); if (tStart == result.getStartPosition() - start) { scanner.resetTo(tStart + result.getLength(), length - 1); token= scanner.getNextToken(); if (token == ITerminalSymbols.TokenNameEOF) return result; } } } catch (InvalidInputException e) { } } } return finder.getCoveringNode(); }