Skip to content

Commit

Permalink
No need for 1.5 JVM checks in o.e.j.core.manipulation/refactoring
Browse files Browse the repository at this point in the history
Every project is 1.8+ now as ECJ doesn't support pre 1.8 anymore.
  • Loading branch information
akurtakov committed Dec 26, 2024
1 parent a27b470 commit 6669c21
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
Expand Down Expand Up @@ -59,7 +58,6 @@
import org.eclipse.jdt.internal.corext.dom.Bindings;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTesterCore;
import org.eclipse.jdt.internal.corext.refactoring.sef.SelfEncapsulateFieldRefactoring;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;

import org.eclipse.jdt.ui.text.java.IInvocationContext;
Expand Down Expand Up @@ -339,15 +337,13 @@ private static boolean isBoolean(ProposalParameter context) {
private static Expression getAssignedValue(ProposalParameter context) {
ASTNode parent= context.accessNode.getParent();
ASTRewrite astRewrite= context.astRewrite;
IJavaProject javaProject= context.compilationUnit.getJavaProject();
IMethodBinding getter= findGetter(context);
Expression getterExpression= null;
if (getter != null) {
getterExpression= astRewrite.getAST().newSimpleName("placeholder"); //$NON-NLS-1$
}
ITypeBinding type= context.variableBinding.getType();
boolean is50OrHigher= JavaModelUtil.is50OrHigher(javaProject);
Expression result= GetterSetterUtil.getAssignedValue(parent, astRewrite, getterExpression, type, is50OrHigher);
Expression result= GetterSetterUtil.getAssignedValue(parent, astRewrite, getterExpression, type);
if (result != null && getterExpression != null && getterExpression.getParent() != null) {
getterExpression.getParent().setStructuralProperty(getterExpression.getLocationInParent(), createMethodInvocation(context, getter, null));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2018 IBM Corporation and others.
* Copyright (c) 2000, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -246,10 +246,9 @@ public static String getGetterStub(IField field, String getterName, boolean addC
* @param astRewrite the astRewrite to use
* @param getterExpression the expression to insert for read accesses or <code>null</code> if such an expression does not exist
* @param variableType the type of the variable that the result will be assigned to
* @param is50OrHigher <code>true</code> if a 5.0 or higher environment can be used
* @return an expression that can be assigned to the type variableType with node being replaced by a equivalent expression using the getter
*/
public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType, boolean is50OrHigher) {
public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, Expression getterExpression, ITypeBinding variableType) {
InfixExpression.Operator op= null;
AST ast= astRewrite.getAST();
if (isNotInBlock(node))
Expand All @@ -260,7 +259,7 @@ public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, E
Expression copiedRightOp= (Expression) astRewrite.createCopyTarget(rightHandSide);
if (assignment.getOperator() == Operator.ASSIGN) {
ITypeBinding rightHandSideType= rightHandSide.resolveTypeBinding();
copiedRightOp= createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType, is50OrHigher);
copiedRightOp= createNarrowCastIfNessecary(copiedRightOp, rightHandSideType, ast, variableType);
return copiedRightOp;
}
if (getterExpression != null) {
Expand All @@ -274,7 +273,7 @@ public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, E
copiedRightOp= p;
}
infix.setRightOperand(copiedRightOp);
return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
return createNarrowCastIfNessecary(infix, infixType, ast, variableType);
}
} else if (node.getNodeType() == ASTNode.POSTFIX_EXPRESSION) {
PostfixExpression po= (PostfixExpression) node;
Expand All @@ -290,7 +289,7 @@ public static Expression getAssignedValue(ASTNode node, ASTRewrite astRewrite, E
op= InfixExpression.Operator.MINUS;
}
if (op != null && getterExpression != null) {
return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType, is50OrHigher);
return createInfixInvocationFromPostPrefixExpression(op, getterExpression, ast, variableType);
}
return null;
}
Expand All @@ -307,15 +306,15 @@ private static boolean isNotInBlock(ASTNode parent) {
return isStatement || (!isBlock && !isControlStatemenBody);
}

private static Expression createInfixInvocationFromPostPrefixExpression(InfixExpression.Operator operator, Expression getterExpression, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
private static Expression createInfixInvocationFromPostPrefixExpression(InfixExpression.Operator operator, Expression getterExpression, AST ast, ITypeBinding variableType) {
InfixExpression infix= ast.newInfixExpression();
infix.setLeftOperand(getterExpression);
infix.setOperator(operator);
NumberLiteral number= ast.newNumberLiteral();
number.setToken("1"); //$NON-NLS-1$
infix.setRightOperand(number);
ITypeBinding infixType= infix.resolveTypeBinding();
return createNarrowCastIfNessecary(infix, infixType, ast, variableType, is50OrHigher);
return createNarrowCastIfNessecary(infix, infixType, ast, variableType);
}

/**
Expand All @@ -325,21 +324,18 @@ private static Expression createInfixInvocationFromPostPrefixExpression(InfixExp
* @param expressionType the type of the right hand-side. Can be null
* @param ast the AST
* @param variableType the Type of the variable the expression will be assigned to
* @param is50OrHigher if <code>true</code> java 5.0 code will be assumed
* @return the casted expression if necessary
*/
private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType, boolean is50OrHigher) {
private static Expression createNarrowCastIfNessecary(Expression expression, ITypeBinding expressionType, AST ast, ITypeBinding variableType) {
PrimitiveType castTo= null;
if (variableType.isEqualTo(expressionType))
return expression; //no cast for same type
if (is50OrHigher) {
if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) //$NON-NLS-1$
castTo= ast.newPrimitiveType(PrimitiveType.CHAR);
if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) //$NON-NLS-1$
castTo= ast.newPrimitiveType(PrimitiveType.BYTE);
if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) //$NON-NLS-1$
castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
}
if (ast.resolveWellKnownType("java.lang.Character").isEqualTo(variableType)) //$NON-NLS-1$
castTo= ast.newPrimitiveType(PrimitiveType.CHAR);
if (ast.resolveWellKnownType("java.lang.Byte").isEqualTo(variableType)) //$NON-NLS-1$
castTo= ast.newPrimitiveType(PrimitiveType.BYTE);
if (ast.resolveWellKnownType("java.lang.Short").isEqualTo(variableType)) //$NON-NLS-1$
castTo= ast.newPrimitiveType(PrimitiveType.SHORT);
if (ast.resolveWellKnownType("char").isEqualTo(variableType)) //$NON-NLS-1$
castTo= ast.newPrimitiveType(PrimitiveType.CHAR);
if (ast.resolveWellKnownType("byte").isEqualTo(variableType)) //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
import org.eclipse.jdt.internal.corext.fix.UnusedSuppressWarningsFixCore;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;

import org.eclipse.jdt.ui.cleanup.CleanUpOptions;
Expand All @@ -68,7 +67,7 @@ public abstract class SuppressWarningsBaseSubProcessor<T> {
static final String ADD_SUPPRESSWARNINGS_ID= "org.eclipse.jdt.ui.correction.addSuppressWarnings"; //$NON-NLS-1$

public static final boolean hasSuppressWarningsProposal(IJavaProject javaProject, int problemId) {
if (CorrectionEngine.getWarningToken(problemId) != null && JavaModelUtil.is50OrHigher(javaProject)) {
if (CorrectionEngine.getWarningToken(problemId) != null) {
String optionId= JavaCore.getOptionForConfigurableSeverity(problemId);
if (optionId != null) {
String optionValue= javaProject.getOption(optionId, true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2018 IBM Corporation and others.
* Copyright (c) 2007, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -618,12 +618,11 @@ private RefactoringStatus replaceReferences(ParameterObjectFactory pof, SearchRe

private Expression getAssignedValue(ParameterObjectFactory pof, String parameterName, IJavaProject javaProject, RefactoringStatus status, ASTRewrite rewrite, ParameterInfo pi, boolean useSuper, ITypeBinding typeBinding, Expression qualifier, ASTNode replaceNode, ITypeRoot typeRoot) {
AST ast= rewrite.getAST();
boolean is50OrHigher= JavaModelUtil.is50OrHigher(javaProject);
Expression assignedValue= handleSimpleNameAssignment(replaceNode, pof, parameterName, ast, javaProject, useSuper);
if (assignedValue == null) {
NullLiteral marker= qualifier == null ? null : ast.newNullLiteral();
Expression fieldReadAccess= pof.createFieldReadAccess(pi, parameterName, ast, javaProject, useSuper, marker);
assignedValue= GetterSetterUtil.getAssignedValue(replaceNode, rewrite, fieldReadAccess, typeBinding, is50OrHigher);
assignedValue= GetterSetterUtil.getAssignedValue(replaceNode, rewrite, fieldReadAccess, typeBinding);
boolean markerReplaced= replaceMarker(rewrite, qualifier, assignedValue, marker);
if (markerReplaced && qualifier != null) {
switch (qualifier.getNodeType()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2023 IBM Corporation and others.
* Copyright (c) 2006, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -17,7 +17,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -98,7 +97,6 @@
import org.eclipse.jdt.internal.corext.refactoring.util.JavaStatusContext;
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
import org.eclipse.jdt.internal.corext.refactoring.util.TextEditBasedChangeManager;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.JdtFlags;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.corext.util.SearchUtils;
Expand Down Expand Up @@ -174,37 +172,8 @@ protected static boolean areAllFragmentsDeleted(final FieldDeclaration declarati
return declarationNodes.containsAll(declaration.fragments());
}

protected static RefactoringStatus checkProjectCompliance(CompilationUnitRewrite sourceRewriter, IType destination, IMember[] members) {
protected static RefactoringStatus checkProjectCompliance() {
RefactoringStatus status= new RefactoringStatus();
if (!JavaModelUtil.is50OrHigher(destination.getJavaProject())) {
for (IMember member : members) {
try {
BodyDeclaration decl= ASTNodeSearchUtil.getBodyDeclarationNode(member, sourceRewriter.getRoot());
if (decl != null) {
for (final Iterator<IExtendedModifier> iterator= decl.modifiers().iterator(); iterator.hasNext();) {
boolean reported= false;
final IExtendedModifier modifier= iterator.next();
if (!reported && modifier.isAnnotation()) {
status.merge(RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.PullUpRefactoring_incompatible_langauge_constructs, new String[]{JavaElementLabelsCore.getTextLabel(member, JavaElementLabelsCore.ALL_FULLY_QUALIFIED), JavaElementLabelsCore.getTextLabel(destination, JavaElementLabelsCore.ALL_DEFAULT)}), JavaStatusContext.create(member)));
reported= true;
}
}
}
} catch (JavaModelException exception) {
JavaManipulationPlugin.log(exception);
}
if (member instanceof IMethod) {
final IMethod method= (IMethod) member;
try {
if (Flags.isVarargs(method.getFlags())) {
status.merge(RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.PullUpRefactoring_incompatible_language_constructs1, new String[]{JavaElementLabelsCore.getTextLabel(member, JavaElementLabelsCore.ALL_FULLY_QUALIFIED), JavaElementLabelsCore.getTextLabel(destination, JavaElementLabelsCore.ALL_DEFAULT)}), JavaStatusContext.create(member)));
}
} catch (JavaModelException exception) {
JavaManipulationPlugin.log(exception);
}
}
}
}
return status;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ public RefactoringStatus checkFinalConditions(final IProgressMonitor monitor, fi
if (result.hasFatalError())
return result;
fCompilationUnitRewrites= new HashMap<>(3);
result.merge(checkProjectCompliance(getCompilationUnitRewrite(fCompilationUnitRewrites, getDeclaringType().getCompilationUnit()), getDestinationType(), fMembersToMove));
result.merge(checkProjectCompliance());
fChangeManager= createChangeManager(subMonitor.newChild(1), result);

Checks.addModifiedFilesToChecker(ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits()), context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2023 IBM Corporation and others.
* Copyright (c) 2006, 2024 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -125,7 +125,6 @@
import org.eclipse.jdt.internal.corext.refactoring.util.JavaStatusContext;
import org.eclipse.jdt.internal.corext.refactoring.util.ResourceUtil;
import org.eclipse.jdt.internal.corext.refactoring.util.TextEditBasedChangeManager;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.JdtFlags;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.corext.util.SearchUtils;
Expand Down Expand Up @@ -859,7 +858,7 @@ private void copyMembers(Collection<MemberVisibilityAdjustor> adjustors, Map<IMe
adjustor.adjustVisibility(subMon.newChild(1));
adjustments.remove(member);
adjustors.add(adjustor);
status.merge(checkProjectCompliance(getCompilationUnitRewrite(rewrites, getDeclaringType().getCompilationUnit()), type, new IMember[] {infos[offset].getMember()}));
status.merge(checkProjectCompliance());
if (infos[offset].isFieldInfo()) {
final VariableDeclarationFragment oldField= ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) infos[offset].getMember(), sourceRewriter.getRoot());
if (oldField != null) {
Expand Down Expand Up @@ -1131,8 +1130,7 @@ private MethodDeclaration createNewMethodDeclarationNode(MemberActionInfo info,
copyExtraDimensions(oldMethod, newMethod);
if (info.copyJavadocToCopiesInSubclasses())
copyJavadocNode(rewrite, oldMethod, newMethod);
final IJavaProject project= rewriter.getCu().getJavaProject();
if (info.isNewMethodToBeDeclaredAbstract() && JavaModelUtil.is50OrHigher(project) && JavaPreferencesSettings.getCodeGenerationSettings(rewriter.getCu()).overrideAnnotation) {
if (info.isNewMethodToBeDeclaredAbstract() && JavaPreferencesSettings.getCodeGenerationSettings(rewriter.getCu()).overrideAnnotation) {
final MarkerAnnotation annotation= ast.newMarkerAnnotation();
annotation.setTypeName(ast.newSimpleName("Override")); //$NON-NLS-1$
newMethod.modifiers().add(annotation);
Expand Down

0 comments on commit 6669c21

Please sign in to comment.