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

Fixing rename for permitted subclasses. #7977

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions java/java.source.base/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
<apidef name="javasource_base">Java Source API</apidef>
</apidefs>
<changes>
<change id="TreeMaker.ClassPermits">
<api name="javasource_base" />
<summary>Adding TreeMaker.Class overload that takes permitted subclasses</summary>
<version major="1" minor="2.74"/>
<date day="28" month="11" year="2024"/>
<author login="jlahoda"/>
<compatibility addition="yes" binary="compatible" source="compatible"/>
<description>
Adding TreeMaker.Class overload that allows to specify the permitted subclasses.
</description>
<class name="TreeMaker" package="org.netbeans.api.java.source"/>
</change>
<change id="TreeMaker.RawText">
<api name="javasource_base" />
<summary>Adding TreeMaker.RawText</summary>
Expand Down
2 changes: 1 addition & 1 deletion java/java.source.base/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ javadoc.name=Java Source Base
javadoc.title=Java Source Base
javadoc.arch=${basedir}/arch.xml
javadoc.apichanges=${basedir}/apichanges.xml
spec.version.base=2.72.0
spec.version.base=2.74.0
test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar
test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
${o.n.core.dir}/lib/boot.jar:\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,36 @@ public ClassTree Class(ModifiersTree modifiers,
Tree extendsClause,
List<? extends Tree> implementsClauses,
List<? extends Tree> memberDecls) {
return delegate.Class(modifiers, simpleName, typeParameters, extendsClause, implementsClauses, memberDecls);
return Class(modifiers, simpleName, typeParameters, extendsClause, implementsClauses, List.of(), memberDecls);
}

/**
* Creates a new ClassTree.
*
* @param modifiers the modifiers declaration
* @param simpleName the name of the class without its package, such
* as "String" for the class "java.lang.String".
* @param typeParameters the list of type parameters, or an empty list.
* @param extendsClause the name of the class this class extends, or null.
* @param implementsClauses the list of the interfaces this class
* implements, or an empty list.
* @param permitsClauses the list of the subtype this class
* permits, or an empty list.
* @param memberDecls the list of fields defined by this class, or an
* empty list.
* @see com.sun.source.tree.ClassTree
* @since 2.74
*/
public ClassTree Class(ModifiersTree modifiers,
CharSequence simpleName,
List<? extends TypeParameterTree> typeParameters,
Tree extendsClause,
List<? extends Tree> implementsClauses,
List<? extends Tree> permitsClauses,
List<? extends Tree> memberDecls) {
return delegate.Class(modifiers, simpleName, typeParameters, extendsClause, implementsClauses, permitsClauses, memberDecls);
}

/**
* Creates a new ClassTree representing interface.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public ClassTree Class(ModifiersTree modifiers,
List<? extends TypeParameterTree> typeParameters,
Tree extendsClause,
List<? extends Tree> implementsClauses,
List<? extends Tree> permitsClauses,
List<? extends Tree> memberDecls)
{
ListBuffer<JCTypeParameter> typarams = new ListBuffer<JCTypeParameter>();
Expand All @@ -287,6 +288,9 @@ public ClassTree Class(ModifiersTree modifiers,
ListBuffer<JCExpression> impls = new ListBuffer<JCExpression>();
for (Tree t : implementsClauses)
impls.append((JCExpression)t);
ListBuffer<JCExpression> permits = new ListBuffer<JCExpression>();
for (Tree t : permitsClauses)
permits.append((JCExpression)t);
ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
for (Tree t : memberDecls)
defs.append((JCTree)t);
Expand All @@ -295,6 +299,7 @@ public ClassTree Class(ModifiersTree modifiers,
typarams.toList(),
(JCExpression)extendsClause,
impls.toList(),
permits.toList(),
defs.toList());

}
Expand Down Expand Up @@ -1105,6 +1110,7 @@ private ClassTree modifyClassMember(ClassTree clazz, int index, Tree member, Ope
clazz.getTypeParameters(),
clazz.getExtendsClause(),
(List<ExpressionTree>) clazz.getImplementsClause(),
clazz.getPermitsClause(),
c(clazz.getMembers(), index, member, op)
);
return copy;
Expand Down Expand Up @@ -1133,6 +1139,7 @@ private ClassTree modifyClassTypeParameter(ClassTree clazz, int index, TypeParam
c(clazz.getTypeParameters(), index, typeParameter, op),
clazz.getExtendsClause(),
(List<ExpressionTree>) clazz.getImplementsClause(),
clazz.getPermitsClause(),
clazz.getMembers()
);
return copy;
Expand Down Expand Up @@ -1161,6 +1168,7 @@ private ClassTree modifyClassImplementsClause(ClassTree clazz, int index, Tree i
clazz.getTypeParameters(),
clazz.getExtendsClause(),
c((List<ExpressionTree>) clazz.getImplementsClause(), index, implementsClause, op), // todo: cast!
clazz.getPermitsClause(),
clazz.getMembers()
);
return copy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@ protected int diffClassDef(JCClassDecl oldT, JCClassDecl newT, int[] bounds) {
localPointer = endPos(oldT.extending);
break;
}
{
// TODO (#pf): there is some space for optimization. If the new list
// is also empty, we can skip this computation.
if (oldT.implementing.isEmpty()) {
Expand Down Expand Up @@ -1098,6 +1099,39 @@ protected int diffClassDef(JCClassDecl oldT, JCClassDecl newT, int[] bounds) {
if (!newT.implementing.isEmpty())
copyTo(localPointer, insertHint);
localPointer = diffList2(oldT.implementing, newT.implementing, insertHint, estimator);
}

{
// TODO (#pf): there is some space for optimization. If the new list
// is also empty, we can skip this computation.
if (oldT.permitting.isEmpty()) {
// if there is not any permits part, we need to adjust position
// from different place. Look at the examples in all if branches.
// | represents current adjustment and ! where we want to point to
if (oldT.implementing.nonEmpty()) {
// public class Yerba<E>| implements Runnable! { ...
insertHint = endPos(oldT.implementing);
} else if (oldT.extending != null)
// public class Yerba<E>| extends Object! { ...
insertHint = endPos(oldT.extending);
else {
// currently no need to adjust anything here:
// public class Yerba<E>|! { ...
}
} else {
// we already have any permits, adjust position to first
// public class Yerba<E>| permits !Mate { ...
// Note: in case of all permits classes are removed,
// diffing mechanism will solve the permits keyword.
insertHint = oldT.permitting.iterator().next().getStartPosition();
}
PositionEstimator estimator =
EstimatorFactory.permits(oldT.getPermitsClause(), newT.getPermitsClause(), diffContext);
if (!newT.permitting.isEmpty())
copyTo(localPointer, insertHint);
localPointer = diffList2(oldT.permitting, newT.permitting, insertHint, estimator);
}

insertHint = endPos(oldT) - 1;

if (filteredOldTDefs.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ static PositionEstimator extendz(List<? extends Tree> oldL,
return new PositionEstimator.ExtendsEstimator(oldL, newL, diffContext);
}

static PositionEstimator permits(List<? extends Tree> oldL,
List<? extends Tree> newL,
DiffContext diffContext)
{
return new PositionEstimator.PermitsEstimator(oldL, newL, diffContext);
}

static PositionEstimator statements(List<? extends Tree> oldL,
List<? extends Tree> newL,
DiffContext diffContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import javax.swing.text.StyledDocument;
import org.netbeans.api.editor.guards.GuardedSection;
import org.netbeans.api.editor.guards.GuardedSectionManager;
Expand Down Expand Up @@ -168,6 +169,18 @@ static class ExtendsEstimator extends BaseEstimator {
}
}

static class PermitsEstimator extends BaseEstimator {
private static final java.lang.String CONSTANT = "permits";
PermitsEstimator(List<? extends Tree> oldL,
List<? extends Tree> newL,
DiffContext diffContext)
{
super(token -> token.id() != JavaTokenId.IDENTIFIER &&
!CONSTANT.contentEquals(token.text()),
CONSTANT, oldL, newL, diffContext);
}
}

static class ThrowsEstimator extends BaseEstimator {
ThrowsEstimator(List<? extends ExpressionTree> oldL,
List<? extends ExpressionTree> newL,
Expand All @@ -187,7 +200,7 @@ static class CasePatternEstimator extends BaseEstimator {

@Override
public String head() {
return precToken.fixedText() + " ";
return prefixTokenText + " ";
}

}
Expand All @@ -202,7 +215,7 @@ static class StringTemaplateEstimator extends BaseEstimator {

@Override
public String head() {
return precToken.fixedText();
return prefixTokenText;
}

@Override
Expand Down Expand Up @@ -814,19 +827,31 @@ public int[] sectionRemovalBounds(StringBuilder replacement) {

private abstract static class BaseEstimator extends PositionEstimator {

JavaTokenId precToken;
Predicate<Token<JavaTokenId>> prefixTokenAcceptor;
String prefixTokenText;
private ArrayList<String> separatorList;

private BaseEstimator(JavaTokenId precToken,
List<? extends Tree> oldL,
List<? extends Tree> newL,
DiffContext diffContext)
{
this(token -> token.id() != precToken, precToken.fixedText(),
oldL, newL, diffContext);
}

private BaseEstimator(Predicate<Token<JavaTokenId>> prefixTokenAcceptor,
String prefixTokenText,
List<? extends Tree> oldL,
List<? extends Tree> newL,
DiffContext diffContext)
{
super(oldL, newL, diffContext);
this.precToken = precToken;
this.prefixTokenAcceptor = prefixTokenAcceptor;
this.prefixTokenText = prefixTokenText;
}

public String head() { return " " + precToken.fixedText() + " "; }
public String head() { return " " + prefixTokenText + " "; }
public String sep() { return ", "; }

@SuppressWarnings("empty-statement")
Expand All @@ -848,7 +873,7 @@ public void initialize() {
int beforer = -1;
if (first) {
// go back to throws keywrd.
while (seq.movePrevious() && seq.token().id() != precToken) ;
while (seq.movePrevious() && prefixTokenAcceptor.test(seq.token())) ;
int throwsIndex = seq.index();
beforer = throwsIndex+1;
// go back to closing )
Expand All @@ -868,7 +893,8 @@ else if (seq.token().id() == LINE_COMMENT)
separatedText = '\n' + separatedText;
separatorList.add(separatedText);
int separator = seq.index();
int afterSeparator = separator + 1; // bug
while (seq.moveNext() && seq.token().id() == WHITESPACE);
int afterSeparator = seq.index();
if (afterPrevious == separator) {
afterPrevious = -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -686,15 +686,18 @@ protected final ClassTree rewriteChildren(ClassTree tree) {
Tree extending = translateClassRef(tree.getExtendsClause());
List<? extends ExpressionTree> implementing =
translateClassRef((List<? extends ExpressionTree>)tree.getImplementsClause());
List<? extends ExpressionTree> permits =
translateClassRef((List<? extends ExpressionTree>)tree.getPermitsClause());
importAnalysis.enterVisibleThroughClasses(tree);
List<? extends Tree> defs = translate(tree.getMembers());
boolean typeChanged = !typarams.equals(tree.getTypeParameters()) ||
extending != tree.getExtendsClause() ||
!implementing.equals(tree.getImplementsClause());
!implementing.equals(tree.getImplementsClause()) ||
!permits.equals(tree.getPermitsClause());
if (typeChanged || mods != tree.getModifiers() ||
!defs.equals(tree.getMembers())) {
ClassTree n = make.Class(mods, tree.getSimpleName(), typarams,
extending, implementing, defs);
extending, implementing, permits, defs);
if (!typeChanged) {
model.setElement(n, model.getElement(tree));
model.setType(n, model.getType(tree));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ public Tree visitCatch(CatchTree tree, Void p) {
@Override
public Tree visitClass(ClassTree tree, Void p) {
ClassTree n = make.Class(tree.getModifiers(), tree.getSimpleName(), tree.getTypeParameters(),
tree.getExtendsClause(), tree.getImplementsClause(), tree.getMembers());
tree.getExtendsClause(), tree.getImplementsClause(),
tree.getPermitsClause(), tree.getMembers());
model.setElement(n, model.getElement(tree));
model.setType(n, model.getType(tree));
comments.copyComments(tree, n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ public Void visitClass (@NonNull final ClassTree node, @NonNull final Map<Pair<B
state = errorInDecl ? State.OTHER : State.IMPLEMENTS;
scan(node.getImplementsClause(), p);
state = State.OTHER;
scan(node.getPermitsClause(), p);
scan(node.getMembers(), p);
activeClass.pop();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class MethodTest1 {
return;
}

private void fifthMethod(String d) throws IllegalAccessError, IllegalArgumentException, java.lang.IllegalMonitorStateException {
private void fifthMethod(String d) throws IllegalAccessError, IllegalArgumentException, java.lang.IllegalMonitorStateException {
// three exceptions thrown
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class MethodTest1 {
return;
}

private void fifthMethod(String d) throws IllegalAccessError, IllegalArgumentException, java.lang.IllegalMonitorStateException {
private void fifthMethod(String d) throws IllegalAccessError, IllegalArgumentException, java.lang.IllegalMonitorStateException {
// three exceptions thrown
}

Expand Down
Loading
Loading