Skip to content

Commit

Permalink
remove more casts flagged as unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-herrmann committed Jun 18, 2024
1 parent b073d4a commit 6a170ae
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.eclipse.jdt.core.dom.IAnnotationBinding;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

import com.sun.mirror.declaration.AnnotationMirror;
import com.sun.mirror.declaration.Modifier;
Expand Down Expand Up @@ -148,7 +147,7 @@ private IAnnotationBinding[] getAnnotationInstancesFromAST()
extendsMods = ((SingleVariableDeclaration)_astNode).modifiers();
break;
case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
final ASTNode parent = ((VariableDeclarationFragment)_astNode).getParent();
final ASTNode parent = _astNode.getParent();
if( parent instanceof BodyDeclaration )
extendsMods = ((BodyDeclaration)parent).modifiers();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ private IAnnotationBinding[] getAnnotationInstances()
switch( binding.getKind() )
{
case IBinding.TYPE:
instances = ((ITypeBinding)binding).getAnnotations();
instances = binding.getAnnotations();
break;
case IBinding.METHOD:
instances = ((IMethodBinding)binding).getAnnotations();
instances = binding.getAnnotations();
break;
case IBinding.VARIABLE:
instances = ((IVariableBinding)binding).getAnnotations();
instances = binding.getAnnotations();
break;
case IBinding.PACKAGE:
// TODO: support package annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private TestCodeUtil() {
}

public static boolean isTestCode(ICompilationUnit cu) {
IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) ((IJavaElement) cu)
IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) cu
.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
if (packageFragmentRoot != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9346,7 +9346,7 @@ public void test0272() throws JavaModelException {
assertEquals("Not an expression statement", ASTNode.EXPRESSION_STATEMENT, statement2.getNodeType());
Expression expression2 = ((ExpressionStatement) statement2).getExpression();
assertEquals("Not a method invocation", ASTNode.METHOD_INVOCATION, expression2.getNodeType());
ITypeBinding typeBinding = ((MethodInvocation) expression2).resolveTypeBinding();
ITypeBinding typeBinding = expression2.resolveTypeBinding();
assertTrue("Not a capture", typeBinding.isCapture());
assertNull("No binary type", typeBinding.getBinaryName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9334,7 +9334,7 @@ public void test0272() throws JavaModelException {
assertEquals("Not an expression statement", ASTNode.EXPRESSION_STATEMENT, statement2.getNodeType());
Expression expression2 = ((ExpressionStatement) statement2).getExpression();
assertEquals("Not a method invocation", ASTNode.METHOD_INVOCATION, expression2.getNodeType());
ITypeBinding typeBinding = ((MethodInvocation) expression2).resolveTypeBinding();
ITypeBinding typeBinding = expression2.resolveTypeBinding();
assertTrue("Not a capture", typeBinding.isCapture());
assertNull("No binary type", typeBinding.getBinaryName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9381,7 +9381,7 @@ public void test0272() throws JavaModelException {
assertEquals("Not an expression statement", ASTNode.EXPRESSION_STATEMENT, statement2.getNodeType());
Expression expression2 = ((ExpressionStatement) statement2).getExpression();
assertEquals("Not a method invocation", ASTNode.METHOD_INVOCATION, expression2.getNodeType());
ITypeBinding typeBinding = ((MethodInvocation) expression2).resolveTypeBinding();
ITypeBinding typeBinding = expression2.resolveTypeBinding();
assertTrue("Not a capture", typeBinding.isCapture());
assertNull("No binary type", typeBinding.getBinaryName());
}
Expand Down Expand Up @@ -11372,7 +11372,7 @@ public void test0350() throws JavaModelException {
true,
true);
ExpressionStatement statement = (ExpressionStatement) getASTNode(unit, 0, 1, 0);
ITypeBinding binding = ((MethodInvocation) statement.getExpression()).resolveTypeBinding();
ITypeBinding binding = statement.getExpression().resolveTypeBinding();
assertTrue("Should be seen as a wildcard (really an intersection type)", binding.isWildcardType());
assertNull("should be null", binding.getGenericTypeOfWildcardType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ public void test0007() throws JavaModelException {
MethodDeclaration method = (MethodDeclaration) node;
Type receiver = method.getReceiverType();
assertEquals("Not a simple type", ASTNode.SIMPLE_TYPE, receiver.getNodeType());
assertEquals("Incorrect receiver signature", "@Marker @Marker2 X", ((SimpleType) receiver).toString());
assertEquals("Incorrect receiver signature", "@Marker @Marker2 X", receiver.toString());
assertEquals("Incorrect annotations on receiver", 2, ((SimpleType) receiver).annotations().size());
assertNull("Incorrect receiver qualfier", method.getReceiverQualifier());
}
Expand Down Expand Up @@ -1126,7 +1126,7 @@ public void test0008() throws JavaModelException {
MethodDeclaration method = innerType.getMethods()[0];
Type receiver = method.getReceiverType();
assertEquals("Not a simple type", ASTNode.SIMPLE_TYPE, receiver.getNodeType());
assertEquals("Incorrect receiver signature", "@Marker @Marker2 X", ((SimpleType) receiver).toString());
assertEquals("Incorrect receiver signature", "@Marker @Marker2 X", receiver.toString());
assertEquals("Incorrect annotations on receiver", 2, ((SimpleType) receiver).annotations().size());
assertNotNull("Incorrect receiver qualfier", method.getReceiverQualifier());
assertEquals("Incorrect receiver qualfier", "X", method.getReceiverQualifier().getFullyQualifiedName());
Expand Down Expand Up @@ -2097,12 +2097,12 @@ public void testBug403132() throws JavaModelException {
MethodDeclaration method = (MethodDeclaration) node;
Type receiver = method.getReceiverType();
assertEquals("Not a qualified type", ASTNode.QUALIFIED_TYPE, receiver.getNodeType());
assertEquals("Incorrect receiver", "@A X.@B Y", ((QualifiedType) receiver).toString());
assertEquals("Incorrect receiver", "@A X.@B Y", receiver.toString());
assertEquals("Incorrect method signature", "public Z(@A X.@B Y Y.this,String str){\n}\n", method.toString());

method = (MethodDeclaration) type.bodyDeclarations().get(1);
receiver = method.getReceiverType();
assertEquals("Incorrect receiver", "@A X.@B Y.@C Z", ((QualifiedType) receiver).toString());
assertEquals("Incorrect receiver", "@A X.@B Y.@C Z", receiver.toString());
assertEquals("Incorrect method signature", "public void foo(@A X.@B Y.@C Z this,String str){\n}\n", method.toString());
}
public void testParameterizedReceiverType() throws JavaModelException {
Expand Down Expand Up @@ -3857,8 +3857,8 @@ public void test416559() throws JavaModelException {
VariableDeclaration variableDeclaration = (VariableDeclaration) lambdaExpression.parameters().get(0);
IVariableBinding variableBinding = variableDeclaration.resolveBinding();
IMethodBinding methodBinding = lambdaExpression.resolveMethodBinding();
String methodKey = ((IBinding) methodBinding).getKey();
String variableKey = ((IBinding) variableBinding).getKey();
String methodKey = methodBinding.getKey();
String variableKey = variableBinding.getKey();
assertTrue(variableKey.regionMatches(0, methodKey, 0, methodKey.length()));

fragments = field[1].fragments();
Expand All @@ -3868,7 +3868,7 @@ public void test416559() throws JavaModelException {
variableDeclaration = (VariableDeclaration) lambdaExpression.parameters().get(0);
variableBinding = variableDeclaration.resolveBinding();

assertNotSame(variableKey.intern(), ((IBinding) variableBinding).getKey().intern());
assertNotSame(variableKey.intern(), variableBinding.getKey().intern());
}
/*
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=420458
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ public void testBug530803_1() throws Exception {
// common check for both parts:
Consumer<IBinding> validateBinding = (IBinding binding) -> {
assertTrue("Not ModuleBinding", binding instanceof IModuleBinding);
IAnnotationBinding[] annotations = ((IModuleBinding) binding).getAnnotations();
IAnnotationBinding[] annotations = binding.getAnnotations();
assertEquals("Number of annotations", 1, annotations.length);
assertEquals("Annotation type", "Deprecated", annotations[0].getAnnotationType().getName());
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ public void testCreateBindings24() throws CoreException {
assertBindingsEqual(
"Lpack/package-info;",
bindings);
IAnnotationBinding[] annotations = ((ITypeBinding) bindings[0]).getAnnotations();
IAnnotationBinding[] annotations = bindings[0].getAnnotations();
assertBindingsEqual(
"@Ljava/lang/Deprecated;",
annotations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ public void testBug153133() throws JavaModelException {
// Need to get type members constructors
for (int i = 0; i < length; i++) {
assertTrue(members[i] instanceof IMember);
if (((IMember)members[i]).getElementType() == IJavaElement.TYPE) {
if (members[i].getElementType() == IJavaElement.TYPE) {
IType typeMember = (IType) members[i];
String typeName = typeMember.getElementName();
IMethod[] methods = typeMember.getMethods();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public void testCopyFieldsMultiStatus() throws CoreException {
typeDest.getJavaModel().copy(fieldsSource, dests, null, null, false, null);
} catch (JavaModelException jme) {
assertTrue("Should be multistatus", jme.getStatus().isMultiStatus());
assertTrue("Should be an invalid destination", ((IJavaModelStatus)jme.getStatus().getChildren()[0]).getCode()== IJavaModelStatusConstants.INVALID_DESTINATION);
assertTrue("Should be an invalid destination", jme.getStatus().getChildren()[0].getCode()== IJavaModelStatusConstants.INVALID_DESTINATION);
e = true;
}
assertTrue("Should have been an exception", e);
Expand Down Expand Up @@ -457,7 +457,7 @@ public void testCopyFieldsMultiStatusInDifferentProject() throws CoreException {
typeDest.getJavaModel().copy(fieldsSource, dests, null, null, false, null);
} catch (JavaModelException jme) {
assertTrue("Should be multistatus", jme.getStatus().isMultiStatus());
assertTrue("Should be an invalid destination", ((IJavaModelStatus)jme.getStatus().getChildren()[0]).getCode()== IJavaModelStatusConstants.INVALID_DESTINATION);
assertTrue("Should be an invalid destination", jme.getStatus().getChildren()[0].getCode()== IJavaModelStatusConstants.INVALID_DESTINATION);
e = true;
}
assertTrue("Should have been an exception", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void testCreateCompilationUnits() throws CoreException {
assertTrue("wrong object B created", objectB instanceof ICompilationUnit);
assertTrue("compilation unit B does not exist", objectB.exists());

assertEquals("should share project", ((ICompilationUnit)objectA).getJavaProject(), ((ICompilationUnit)objectB).getJavaProject());
assertEquals("should share project", objectA.getJavaProject(), objectB.getJavaProject());
} finally {
this.deleteProject("P");
}
Expand All @@ -103,7 +103,7 @@ public void testCreateCompilationUnitsNotOnClasspath() throws CoreException {
assertTrue("wrong object B created", objectB instanceof ICompilationUnit);
assertTrue("compilation unit B should not exist", !objectB.exists());

assertEquals("should share project", ((ICompilationUnit)objectA).getJavaProject(), ((ICompilationUnit)objectB).getJavaProject());
assertEquals("should share project", objectA.getJavaProject(), objectB.getJavaProject());

IJavaElement objectC = JavaCore.create(fileC);
assertTrue("tooling object C not created", objectC != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.model;

import junit.framework.Test;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IAnnotation;
import org.eclipse.jdt.core.ICompilationUnit;
Expand All @@ -23,11 +21,12 @@
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageDeclaration;
import org.eclipse.jdt.core.ISourceReference;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaModelException;

import junit.framework.Test;

public class GetSourceTests extends ModifyingResourceTests {

ICompilationUnit cu;
Expand Down Expand Up @@ -309,7 +308,7 @@ public void testImport() throws JavaModelException {
public void testLocalVariable1() throws JavaModelException {
ILocalVariable var = getLocalVariable("/P/p/X.java", "var1 = 2;", "var1");

String actualSource = ((ISourceReference)var).getSource();
String actualSource = var.getSource();
String expectedSource = "final int var1 = 2;";
assertSourceEquals("Unexpected source'", expectedSource, actualSource);
}
Expand All @@ -320,7 +319,7 @@ public void testLocalVariable1() throws JavaModelException {
public void testLocalVariable2() throws JavaModelException {
ILocalVariable var = getLocalVariable("/P/p/X.java", "var2;", "var2");

String actualSource = ((ISourceReference)var).getSource();
String actualSource = var.getSource();
String expectedSource = "Object var2;";
assertSourceEquals("Unexpected source'", expectedSource, actualSource);
}
Expand All @@ -331,7 +330,7 @@ public void testLocalVariable2() throws JavaModelException {
public void testLocalVariable3() throws JavaModelException {
ILocalVariable var = getLocalVariable("/P/p/X.java", "i = 0;", "i");

String actualSource = ((ISourceReference)var).getSource();
String actualSource = var.getSource();
String expectedSource = "int i = 0"; // semi-colon is not part of the local declaration in a for statement
assertSourceEquals("Unexpected source'", expectedSource, actualSource);
}
Expand All @@ -342,7 +341,7 @@ public void testLocalVariable3() throws JavaModelException {
public void testLocalVariable4() throws JavaModelException {
ILocalVariable var = getLocalVariable("/P/p/X.java", "s) {", "s");

String actualSource = ((ISourceReference)var).getSource();
String actualSource = var.getSource();
String expectedSource = "String s";
assertSourceEquals("Unexpected source'", expectedSource, actualSource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -984,23 +984,23 @@ public void testFindModule1() throws CoreException, IOException {
assertNotNull("module", modTest);
assertEquals("module name", "test", modTest.getElementName());
IJavaElement root = parentChain(modTest, new int[] { COMPILATION_UNIT, PACKAGE_FRAGMENT, PACKAGE_FRAGMENT_ROOT });
String rootPath = ((IPackageFragmentRoot) root).getPath().toString();
String rootPath = root.getPath().toString();
assertEquals("package fragment root path", "/Test/src", rootPath);

// search source module in project dependency:
IModuleDescription modZero = javaProject.findModule("mod.zero", null);
assertNotNull("module", modZero);
assertEquals("module name", "mod.zero", modZero.getElementName());
root = parentChain(modZero, new int[] { COMPILATION_UNIT, PACKAGE_FRAGMENT, PACKAGE_FRAGMENT_ROOT });
rootPath = ((IPackageFragmentRoot) root).getPath().toString();
rootPath = root.getPath().toString();
assertEquals("package fragment root path", "/mod.zero/src", rootPath);

// search binary module in jar dependency:
IModuleDescription modOne = javaProject.findModule("mod.one", null);
assertNotNull("module", modOne);
assertEquals("module name", "mod.one", modOne.getElementName());
root = parentChain(modOne, new int[] { CLASS_FILE, PACKAGE_FRAGMENT, PACKAGE_FRAGMENT_ROOT });
rootPath = ((IPackageFragmentRoot) root).getPath().toString();
rootPath = root.getPath().toString();
assertEquals("package fragment root path", "/Test/mod.one.jar", rootPath);

IModuleDescription notSuchModule = javaProject.findModule("does.not.exist", null);
Expand Down
Loading

0 comments on commit 6a170ae

Please sign in to comment.