Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ToolFactory.createScanner() with source compliance set if possible #205

Merged
merged 1 commit into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.eclipse.core.runtime.Assert;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.compiler.IScanner;
Expand Down Expand Up @@ -71,7 +73,14 @@ public RefactoringScanner(String name, String qualifier) {
public void scan(ICompilationUnit cu) throws JavaModelException {
char[] chars= cu.getBuffer().getCharacters();
fMatches= new HashSet<>();
fScanner= ToolFactory.createScanner(true, true, false, true);
IJavaProject javaProject= cu.getJavaProject();
if (javaProject != null) {
String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
fScanner = ToolFactory.createScanner(true, true, true, sourceLevel, complianceLevel);
} else {
fScanner= ToolFactory.createScanner(true, true, false, true);
}
fScanner.setSource(chars);

// IImportContainer importContainer= cu.getImportContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.compiler.IScanner;
Expand All @@ -54,6 +56,8 @@
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.TypeReferenceMatch;

import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringScopeFactory;
Expand All @@ -69,9 +73,6 @@

import org.eclipse.jdt.internal.ui.JavaPlugin;

import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;

public class MoveCuUpdateCreator {

private final String fNewPackage;
Expand Down Expand Up @@ -318,7 +319,14 @@ private final static class Collector extends CollectingSearchRequestor {
public Collector(IPackageFragment source, ReferencesInBinaryContext binaryRefs) {
super(binaryRefs);
fSource= source;
fScanner= ToolFactory.createScanner(false, false, false, false);
IJavaProject javaProject= source.getJavaProject();
if (javaProject != null) {
String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
fScanner = ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel);
} else {
fScanner= ToolFactory.createScanner(false, false, false, false);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.NamingConventions;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.compiler.IScanner;
Expand Down Expand Up @@ -692,20 +693,28 @@ protected String getFileComment(ICompilationUnit parentCU, String lineDelimiter)
}

protected String getTypeComment(ICompilationUnit parentCU, String lineDelimiter) throws CoreException {
if (StubUtility.doAddComments(parentCU.getJavaProject())) {
IJavaProject javaProject= parentCU.getJavaProject();
if (StubUtility.doAddComments(javaProject)) {
StringBuilder typeName= new StringBuilder();
typeName.append(getClassName());
String[] typeParamNames= new String[0];
String comment= CodeGeneration.getTypeComment(parentCU, typeName.toString(), typeParamNames, lineDelimiter);
if (comment != null && isValidComment(comment)) {
if (comment != null && isValidComment(comment, javaProject)) {
return comment;
}
}
return null;
}

private boolean isValidComment(String template) {
IScanner scanner= ToolFactory.createScanner(true, false, false, false);
private boolean isValidComment(String template, IJavaProject javaProject) {
IScanner scanner;
if (javaProject != null) {
String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
scanner = ToolFactory.createScanner(true, false, false, sourceLevel, complianceLevel);
} else {
scanner= ToolFactory.createScanner(true, false, false, false);
}
scanner.setSource(template.toCharArray());
try {
int next= scanner.getNextToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,14 @@ private IEditorPart openCu(ICompilationUnit cu) {
}

private void parseCUs(IJavaProject javaProject, String text) {
IScanner scanner= ToolFactory.createScanner(false, false, false, false);
IScanner scanner;
if (javaProject != null) {
String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
scanner = ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel);
} else {
scanner= ToolFactory.createScanner(false, false, false, false);
}
scanner.setSource(text.toCharArray());

ArrayList<ParsedCu> cus= new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IInitializer;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IOrdinaryClassFile;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.SourceRange;
import org.eclipse.jdt.core.ToolFactory;
Expand Down Expand Up @@ -230,7 +232,15 @@ private static int getOffset(IMember iMember) throws JavaModelException {

private static int firstOpeningBraceOffset(IInitializer iInitializer) throws JavaModelException {
try {
IScanner scanner= ToolFactory.createScanner(false, false, false, false);
IScanner scanner;
IJavaProject project = iInitializer.getJavaProject();
if (project != null) {
String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
scanner = ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel);
} else {
scanner= ToolFactory.createScanner(false, false, false, false);
}
scanner.setSource(iInitializer.getSource().toCharArray());
int token= scanner.getNextToken();
while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLBRACE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.eclipse.jface.text.Position;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.compiler.IScanner;
import org.eclipse.jdt.core.compiler.ITerminalSymbols;
Expand Down Expand Up @@ -68,7 +70,15 @@ protected void addEdits(IDocument document, TextEdit rootEdit) throws CoreExcept
}

private Position getUpdatedPosition(IDocument document) throws BadLocationException {
IScanner scanner= ToolFactory.createScanner(true, false, false, false);
IScanner scanner;
IJavaProject project = this.getCompilationUnit().getJavaProject();
if (project != null) {
String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
scanner = ToolFactory.createScanner(true, false, false, sourceLevel, complianceLevel);
} else {
scanner= ToolFactory.createScanner(true, false, false, false);
}
scanner.setSource(document.get().toCharArray());

int token= getSurroundingComment(scanner);
Expand Down