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

Change ConvertForLoopOperation to avoid getting type from size() method #1618

Merged
merged 2 commits into from
Sep 5, 2024
Merged
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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2020 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 @@ -85,7 +85,7 @@ private static final class InvalidBodyError extends Error {
private VariableDeclarationFragment fElementDeclaration;
private boolean fMakeFinal;
private boolean fIsCollection;
private IMethodBinding fSizeMethodBinding;
private ITypeBinding fSizeMethodTypeBinding;
private IMethodBinding fGetMethodBinding;
private MethodInvocation fSizeMethodAccess;
private boolean fCheckLoopVarUsed;
Expand Down Expand Up @@ -347,9 +347,19 @@ private boolean validateLengthQuery(Expression lengthQuery) {
}
ITypeBinding classBinding= methodBinding.getDeclaringClass();

if (methodCall.getExpression() == null) {
return false;
}

ITypeBinding expressionBinding= methodCall.getExpression().resolveTypeBinding();

if (expressionBinding == null) {
return false;
}

if (isCollection(classBinding)) {
fIsCollection= true;
fSizeMethodBinding= methodBinding;
fSizeMethodTypeBinding= expressionBinding;
fSizeMethodAccess= methodCall;
return true;
}
Expand Down Expand Up @@ -446,7 +456,7 @@ private boolean validateUpdaters(ForStatement statement) {
return false;
}

/*
/**
* returns false iff
* <ul>
* <li><code>indexBinding</code> is used for anything else then accessing
Expand Down Expand Up @@ -538,7 +548,7 @@ protected boolean visitNode(ASTNode node) {
|| !GET_QUERY.equals(method.getName().getFullyQualifiedName())
|| parms.length != 1
|| !"int".equals(parms[0].getName()) //$NON-NLS-1$
|| !areTypeBindingEqual(fSizeMethodBinding.getDeclaringClass(), methodBinding.getDeclaringClass())
|| !areTypeBindingEqual(fSizeMethodTypeBinding, methodBinding.getDeclaringClass())
|| fSizeMethodAccess.getExpression() == null
|| !fSizeMethodAccess.getExpression().subtreeMatch(new ASTMatcher(), method.getExpression()))
throw new InvalidBodyError();
Expand All @@ -561,7 +571,7 @@ protected boolean visitNode(ASTNode node) {
if (binding == null) {
throw new InvalidBodyError();
}
if (areTypeBindingEqual(fSizeMethodBinding.getDeclaringClass(), binding.getDeclaringClass())) {
if (areTypeBindingEqual(fSizeMethodTypeBinding, binding.getDeclaringClass())) {
String methodName= method.getName().getFullyQualifiedName();
if (!SIZE_QUERY.equals(methodName) &&
!GET_QUERY.equals(methodName) &&
Expand Down Expand Up @@ -621,7 +631,7 @@ public boolean visit(MethodInvocation node) {
ITypeBinding[] args= nodeBinding.getParameterTypes();
if (GET_QUERY.equals(nodeBinding.getName()) && args.length == 1 &&
"int".equals(args[0].getName()) && //$NON-NLS-1$
areTypeBindingEqual(nodeBinding.getDeclaringClass(), fSizeMethodBinding.getDeclaringClass())) {
areTypeBindingEqual(nodeBinding.getDeclaringClass(), fSizeMethodTypeBinding)) {
IBinding index= getBinding((Expression)node.arguments().get(0));
if (fIndexBinding.equals(index)) {
if (node.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
Expand Down Expand Up @@ -844,7 +854,7 @@ private String[] getVariableNameProposalsCollection(MethodInvocation sizeMethodA
String baseName= modifyBaseName(name);
String[] elementSuggestions= StubUtility.getLocalNameSuggestions(project, baseName, 0, variableNames);

ITypeBinding[] typeArgs= fSizeMethodBinding.getDeclaringClass().getTypeArguments();
ITypeBinding[] typeArgs= fSizeMethodTypeBinding.getTypeArguments();

String type;
if (typeArgs == null || typeArgs.length == 0) {
Expand Down Expand Up @@ -919,8 +929,9 @@ private SingleVariableDeclaration createParameterDeclarationCollection(String pa
pg.addPosition(rewrite.track(name), true);
result.setName(name);

IMethodBinding sizeTypeBinding= ((MethodInvocation)sizeAccess).resolveMethodBinding();
ITypeBinding[] sizeTypeArguments= sizeTypeBinding.getDeclaringClass().getTypeArguments();

ITypeBinding sizeTypeBinding= ((MethodInvocation)sizeAccess).getExpression().resolveTypeBinding();
ITypeBinding[] sizeTypeArguments= sizeTypeBinding.getTypeArguments();

ITypeBinding elementType;
if (sizeTypeArguments == null || sizeTypeArguments.length == 0) {
Expand Down
Loading