Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.fixes.AppliedFix;
import com.google.errorprone.fixes.ErrorProneEndPosTable;
import com.google.errorprone.fixes.Fix;
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.ImportTree;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JCDiagnostic;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
Expand Down Expand Up @@ -68,7 +68,7 @@ public class JavacErrorDescriptionListener implements DescriptionListener {

private JavacErrorDescriptionListener(
Log log,
EndPosTable endPositions,
ErrorProneEndPosTable endPositions,
JavaFileObject sourceFile,
Context context,
boolean dontUseErrors) {
Expand Down Expand Up @@ -169,12 +169,20 @@ private static String messageForFixes(Description description, List<AppliedFix>
static Factory provider(Context context) {
return (log, compilation) ->
new JavacErrorDescriptionListener(
log, compilation.endPositions, compilation.getSourceFile(), context, false);
log,
ErrorProneEndPosTable.create(compilation),
compilation.getSourceFile(),
context,
false);
}

static Factory providerForRefactoring(Context context) {
return (log, compilation) ->
new JavacErrorDescriptionListener(
log, compilation.endPositions, compilation.getSourceFile(), context, true);
log,
ErrorProneEndPosTable.create(compilation),
compilation.getSourceFile(),
context,
true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.errorprone.DescriptionListener;
import com.google.errorprone.fixes.ErrorProneEndPosTable;
import com.google.errorprone.fixes.Fix;
import com.google.errorprone.fixes.Replacement;
import com.google.errorprone.fixes.Replacements;
import com.google.errorprone.matchers.Description;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import java.net.URI;
import java.nio.file.Paths;
Expand All @@ -46,7 +46,7 @@ public final class DescriptionBasedDiff implements DescriptionListener, Diff {
private final JCCompilationUnit compilationUnit;
private final Set<String> importsToAdd;
private final Set<String> importsToRemove;
private final EndPosTable endPositions;
private final ErrorProneEndPosTable endPositions;
private final Replacements replacements = new Replacements();
private final ImportOrganizer importOrganizer;

Expand All @@ -73,7 +73,7 @@ private DescriptionBasedDiff(
this.ignoreOverlappingFixes = ignoreOverlappingFixes;
this.importsToAdd = new LinkedHashSet<>();
this.importsToRemove = new LinkedHashSet<>();
this.endPositions = compilationUnit.endPositions;
this.endPositions = ErrorProneEndPosTable.create(compilationUnit);
this.importOrganizer = importOrganizer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package com.google.errorprone.apply;

import static com.google.errorprone.fixes.ErrorProneEndPosTable.getEndPosition;

import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCExpression;
Expand Down Expand Up @@ -58,31 +59,29 @@ public static ImportStatements create(
return new ImportStatements(
(JCExpression) compilationUnit.getPackageName(),
compilationUnit.getImports(),
compilationUnit.endPositions,
compilationUnit,
importOrganizer);
}

ImportStatements(
JCExpression packageTree,
List<? extends JCTree> importTrees,
EndPosTable endPositions,
JCCompilationUnit unit,
ImportOrganizer importOrganizer) {

// find start, end positions for current list of imports (for replacement)
if (importTrees.isEmpty()) {
// start/end positions are just after the package expression
hasExistingImports = false;
startPos =
packageTree != null
? packageTree.getEndPosition(endPositions) + 2 // +2 for semicolon and newline
: 0;
// +2 for semicolon and newline
startPos = packageTree != null ? getEndPosition(packageTree, unit) + 2 : 0;
endPos = startPos;
} else {
// process list of imports and find start/end positions
hasExistingImports = true;
for (JCTree importTree : importTrees) {
int currStartPos = importTree.getStartPosition();
int currEndPos = importTree.getEndPosition(endPositions);
int currEndPos = getEndPosition(importTree, unit);

startPos = Math.min(startPos, currStartPos);
endPos = Math.max(endPos, currEndPos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;

/** Describes a tree position with adjustments to the start and end indices. */
public class AdjustedPosition implements DiagnosticPosition {
public class AdjustedPosition implements ErrorPronePosition {
protected final JCTree position;
protected final int startPositionAdjustment;
protected final int endPositionAdjustment;
Expand Down Expand Up @@ -51,4 +50,9 @@ public int getPreferredPosition() {
public int getEndPosition(EndPosTable endPositions) {
return position.getEndPosition(endPositions) + endPositionAdjustment;
}

@Override
public int getEndPosition(ErrorProneEndPosTable endPositions) {
return endPositions.getEndPosition(position) + endPositionAdjustment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Range;
import com.sun.tools.javac.tree.EndPosTable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -39,7 +38,7 @@ public record AppliedFix(String snippet, boolean isRemoveLine) {
* to the source, or a change only to imports.
*/
public static @Nullable AppliedFix apply(
CharSequence source, EndPosTable endPositions, Fix suggestedFix) {
CharSequence source, ErrorProneEndPosTable endPositions, Fix suggestedFix) {
// We apply the replacements in ascending order here. Descending is simpler, since applying a
// replacement can't change the index for future replacements, but it leads to quadratic
// copying behavior as we constantly shift the tail of the file around in our StringBuilder.
Expand Down Expand Up @@ -91,7 +90,8 @@ private static String snippet(
return firstEditedLine(replaced, shiftedReplacements.getFirst());
}

public static String applyReplacements(CharSequence source, EndPosTable endPositions, Fix fix) {
public static String applyReplacements(
CharSequence source, ErrorProneEndPosTable endPositions, Fix fix) {
return applyReplacements(source, fix.getReplacements(endPositions));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2025 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.fixes;

import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.JCDiagnostic;

/** A compatibility wrapper around {@link EndPosTable}. */
public interface ErrorProneEndPosTable {

static ErrorProneEndPosTable create(CompilationUnitTree unit) {
EndPosTable endPosTable = ((JCTree.JCCompilationUnit) unit).endPositions;
return pos -> pos.getEndPosition(endPosTable);
}

static ErrorProneEndPosTable create(EndPosTable endPosTable) {
return pos -> pos.getEndPosition(endPosTable);
}

default int getEndPosition(Tree tree) {
return getEndPosition((JCDiagnostic.DiagnosticPosition) tree);
}

default int getEndPosition(JCTree tree) {
return getEndPosition((JCDiagnostic.DiagnosticPosition) tree);
}

int getEndPosition(JCDiagnostic.DiagnosticPosition pos);

static int getEndPosition(Tree tree, CompilationUnitTree unit) {
return getEndPosition((JCDiagnostic.DiagnosticPosition) tree, unit);
}

static int getEndPosition(JCTree tree, CompilationUnitTree unit) {
return getEndPosition((JCDiagnostic.DiagnosticPosition) tree, unit);
}

static int getEndPosition(JCDiagnostic.DiagnosticPosition pos, CompilationUnitTree unit) {
return create(unit).getEndPosition(pos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2025 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.fixes;

import com.sun.source.tree.Tree;
import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;

/** A compatibility wrapper around {@link DiagnosticPosition}. */
public interface ErrorPronePosition extends DiagnosticPosition {
static ErrorPronePosition from(Tree node) {
DiagnosticPosition pos = (DiagnosticPosition) node;
return new ErrorPronePosition() {
@Override
public int getStartPosition() {
return pos.getStartPosition();
}

@Override
public int getPreferredPosition() {
return pos.getPreferredPosition();
}

@Override
public JCTree getTree() {
return pos.getTree();
}

@Override
public int getEndPosition(EndPosTable endPosTable) {
return pos.getEndPosition(endPosTable);
}

@Override
public int getEndPosition(ErrorProneEndPosTable endPosTable) {
return endPosTable.getEndPosition(pos);
}
};
}

int getEndPosition(ErrorProneEndPosTable endPosTable);
}
10 changes: 9 additions & 1 deletion check_api/src/main/java/com/google/errorprone/fixes/Fix.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ public interface Fix {

Replacements.CoalescePolicy getCoalescePolicy();

ImmutableSet<Replacement> getReplacements(EndPosTable endPositions);
ImmutableSet<Replacement> getReplacements(ErrorProneEndPosTable endPositions);

/**
* @deprecated use {@link #getReplacements(ErrorProneEndPosTable) instead}
*/
@Deprecated
default ImmutableSet<Replacement> getReplacements(EndPosTable endPositions) {
return getReplacements(ErrorProneEndPosTable.create(endPositions));
}

ImmutableSet<String> getImportsToAdd();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;

/** A {@link DiagnosticPosition} with a fixed position. */
public final class FixedPosition implements DiagnosticPosition {
public final class FixedPosition implements ErrorPronePosition {
private final JCTree tree;
private final int startPosition;

Expand All @@ -46,6 +46,11 @@ public int getPreferredPosition() {
return startPosition;
}

@Override
public int getEndPosition(ErrorProneEndPosTable endPosTable) {
return startPosition;
}

@Override
public int getEndPosition(EndPosTable endPosTable) {
return startPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

import com.sun.tools.javac.tree.EndPosTable;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;

/** Describes a position that only has a start and end index. */
public class IndexedPosition implements DiagnosticPosition {
public class IndexedPosition implements ErrorPronePosition {

final int startPos;
final int endPos;
Expand All @@ -50,6 +49,11 @@ public int getPreferredPosition() {
throw new UnsupportedOperationException();
}

@Override
public int getEndPosition(ErrorProneEndPosTable endPosTable) {
return endPos;
}

@Override
public int getEndPosition(EndPosTable endPosTable) {
return endPos;
Expand Down
Loading
Loading