From f073a63fadee8609e31365441dbb52c364048112 Mon Sep 17 00:00:00 2001 From: coki1405 <74470430+coki1405@users.noreply.github.com> Date: Sat, 16 Nov 2024 09:14:36 +0100 Subject: [PATCH] IDE-250 Fix all PMD warnings (#5007) * IDE-250 Fix all PMD warnings * IDE-250 Move ServiceProviderTestService to separate file * IDE-250 Fix warnings * IDE-250 Suppress PMD warning for class TestService --- .../test/common/ServiceProviderTest.java | 6 ++-- .../catroid/test/utils/TestUtils.java | 3 +- .../content/actions/PhiroSensorAction.java | 4 +-- .../devices/mindstorms/ev3/EV3Command.java | 29 ++------------- .../catrobat/catroid/stage/StageListener.java | 13 ++++--- .../controller/ActorsAndObjectsManager.java | 14 ++++---- .../ActorAndObjectRecyclerViewFragment.java | 7 ---- .../recyclerview/dialog/TextInputDialog.java | 36 +------------------ .../PhysicsWorldConverterAngleTest.java | 12 +++---- ... PhysicsWorldConverterAngleTestUtils.java} | 6 ++-- 10 files changed, 33 insertions(+), 97 deletions(-) rename catroid/src/test/java/org/catrobat/catroid/test/utils/{TestConstants.java => PhysicsWorldConverterAngleTestUtils.java} (87%) diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/test/common/ServiceProviderTest.java b/catroid/src/androidTest/java/org/catrobat/catroid/test/common/ServiceProviderTest.java index 8522de691e0..769a92db0d1 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/test/common/ServiceProviderTest.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/test/common/ServiceProviderTest.java @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team + * Copyright (C) 2010-2024 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -22,7 +22,6 @@ */ package org.catrobat.catroid.test.common; -import org.catrobat.catroid.bluetooth.base.BluetoothDeviceService; import org.catrobat.catroid.common.CatroidService; import org.catrobat.catroid.common.ServiceProvider; import org.junit.Test; @@ -32,7 +31,6 @@ import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertNull; -import static junit.framework.Assert.assertTrue; @RunWith(AndroidJUnit4.class) public class ServiceProviderTest { @@ -42,7 +40,6 @@ public void testCommonServices() { CatroidService service = ServiceProvider.getService(CatroidService.BLUETOOTH_DEVICE_SERVICE); assertNotNull(service); - assertTrue(service instanceof BluetoothDeviceService); } @Test @@ -55,6 +52,7 @@ public void testRegisterAndGetService() { assertNotNull(service); } + @SuppressWarnings("PMD.TestClassWithoutTestCases") private static class TestService implements CatroidService { } } diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/test/utils/TestUtils.java b/catroid/src/androidTest/java/org/catrobat/catroid/test/utils/TestUtils.java index 603c1a3b5f4..1c7b2bac34b 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/test/utils/TestUtils.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/test/utils/TestUtils.java @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team + * Copyright (C) 2010-2024 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -50,6 +50,7 @@ import static org.catrobat.catroid.common.Constants.SOUND_DIRECTORY_NAME; +@SuppressWarnings("PMD.TestClassWithoutTestCases") public final class TestUtils { public static final String DEFAULT_TEST_PROJECT_NAME = "testProject"; diff --git a/catroid/src/main/java/org/catrobat/catroid/content/actions/PhiroSensorAction.java b/catroid/src/main/java/org/catrobat/catroid/content/actions/PhiroSensorAction.java index 2064e753265..ee7b7ee567c 100644 --- a/catroid/src/main/java/org/catrobat/catroid/content/actions/PhiroSensorAction.java +++ b/catroid/src/main/java/org/catrobat/catroid/content/actions/PhiroSensorAction.java @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team + * Copyright (C) 2010-2024 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -53,7 +53,7 @@ protected void begin() { return; } Double interpretation = ifCondition.interpretDouble(scope); - ifConditionValue = interpretation.intValue() <= DISTANCE_THRESHOLD_VALUE ? true : false; + ifConditionValue = interpretation.intValue() <= DISTANCE_THRESHOLD_VALUE; isInterpretedCorrectly = true; } catch (InterpretationException interpretationException) { isInterpretedCorrectly = false; diff --git a/catroid/src/main/java/org/catrobat/catroid/devices/mindstorms/ev3/EV3Command.java b/catroid/src/main/java/org/catrobat/catroid/devices/mindstorms/ev3/EV3Command.java index 25af0580cfe..86a7bbd49e5 100644 --- a/catroid/src/main/java/org/catrobat/catroid/devices/mindstorms/ev3/EV3Command.java +++ b/catroid/src/main/java/org/catrobat/catroid/devices/mindstorms/ev3/EV3Command.java @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team + * Copyright (C) 2010-2024 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -34,16 +34,7 @@ public class EV3Command implements MindstormsCommand { - private ByteArrayOutputStream commandData = new ByteArrayOutputStream(); - - public EV3Command(short commandCounter, EV3CommandType commandType, EV3CommandOpCode commandByte) { - - commandData.write((byte) (commandCounter & 0x00FF)); - commandData.write((byte) ((commandCounter & 0xFF00) >> 8)); - - commandData.write(commandType.getByte()); - commandData.write(commandByte.getByte()); - } + private final ByteArrayOutputStream commandData = new ByteArrayOutputStream(); public EV3Command(short commandCounter, EV3CommandType commandType, int globalVars, int localVars, EV3CommandOpCode commandByte) { @@ -143,20 +134,4 @@ public int getLength() { public byte[] getRawCommand() { return commandData.toByteArray(); } - - public String toHexString(EV3Command command) { - byte[] rawBytes = command.getRawCommand(); - StringBuilder commandHexString = new StringBuilder("0x"); - - if (rawBytes.length == 0) { - return "0"; - } - - for (int i = 0; i < rawBytes.length; i++) { - commandHexString.append(Integer.toHexString(rawBytes[i] & 0xFF)); - commandHexString.append("_"); - } - - return commandHexString.toString(); - } } diff --git a/catroid/src/main/java/org/catrobat/catroid/stage/StageListener.java b/catroid/src/main/java/org/catrobat/catroid/stage/StageListener.java index 381d277eaca..6df768a1f52 100644 --- a/catroid/src/main/java/org/catrobat/catroid/stage/StageListener.java +++ b/catroid/src/main/java/org/catrobat/catroid/stage/StageListener.java @@ -165,7 +165,6 @@ public void setMaxViewPort(Resolution maxViewPort) { private static final int Z_LAYER_PEN_ACTOR = 1; private static final int Z_LAYER_EMBROIDERY_ACTOR = 2; - private static final int Z_LAYER_PLOT_ACTOR = 3; private Map stageBackupMap = new HashMap<>(); @@ -527,11 +526,13 @@ public void render() { if (reloadProject) { stage.clear(); - if (penActor != null) + if (penActor != null) { penActor.dispose(); + } - if (plotActor != null) + if (plotActor != null) { plotActor.dispose(); + } embroideryPatternManager.clear(); @@ -714,11 +715,13 @@ public void dispose() { SoundManager.getInstance().clear(); PhysicsShapeBuilder.getInstance().reset(); embroideryPatternManager = null; - if (penActor != null) + if (penActor != null) { penActor.dispose(); + } - if(plotActor != null) + if(plotActor != null) { plotActor.dispose(); + } } public void finish() { diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/controller/ActorsAndObjectsManager.java b/catroid/src/main/java/org/catrobat/catroid/ui/controller/ActorsAndObjectsManager.java index ebe8566ad70..f571a174ce7 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/controller/ActorsAndObjectsManager.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/controller/ActorsAndObjectsManager.java @@ -26,14 +26,11 @@ import org.catrobat.catroid.R; import org.catrobat.catroid.common.FlavoredConstants; import org.catrobat.catroid.common.LookData; -import org.catrobat.catroid.content.Script; import org.catrobat.catroid.content.Sprite; import org.catrobat.catroid.content.StartScript; import org.catrobat.catroid.content.bricks.MoveNStepsBrick; import org.catrobat.catroid.content.bricks.PenDownBrick; import org.catrobat.catroid.content.bricks.PlaceAtBrick; -import org.catrobat.catroid.content.bricks.RepeatBrick; -import org.catrobat.catroid.content.bricks.RepeatUntilBrick; import org.catrobat.catroid.content.bricks.SetTransparencyBrick; import org.catrobat.catroid.content.bricks.TurnRightBrick; import org.catrobat.catroid.io.ResourceImporter; @@ -43,12 +40,14 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; + import static org.catrobat.catroid.common.Constants.DEFAULT_IMAGE_EXTENSION; import static org.catrobat.catroid.common.Constants.IMAGE_DIRECTORY_NAME; public final class ActorsAndObjectsManager { private static ActorsAndObjectsManager instance = null; + public static ActorsAndObjectsManager getInstance() { if (instance == null) { instance = new ActorsAndObjectsManager(); @@ -62,14 +61,15 @@ private ActorsAndObjectsManager() { private Sprite generateSprite(String name, int imgId, double scale) throws IOException { var sprite = new Sprite(name); var folder = new File(FlavoredConstants.DEFAULT_ROOT_DIRECTORY, IMAGE_DIRECTORY_NAME); - if(!folder.exists()) + if (!folder.exists()) { folder.mkdirs(); + } var path = ResourceImporter.createImageFileFromResourcesInDirectory(ProjectManager.getInstance().getApplicationContext().getResources(), imgId, folder, - "img" + DEFAULT_IMAGE_EXTENSION, - scale); + "img" + DEFAULT_IMAGE_EXTENSION, + scale); var lookData = new LookData(name, path); sprite.getLookList().add(lookData); @@ -80,7 +80,7 @@ public List getSprites() { var sprites = new ArrayList(); try { - if(SettingsFragment.isEmroiderySharedPreferenceEnabled(ProjectManager.getInstance().getApplicationContext())){ + if (SettingsFragment.isEmroiderySharedPreferenceEnabled(ProjectManager.getInstance().getApplicationContext())) { var frame = generateSprite("10x10", R.drawable.frame, 1.0); var script = new StartScript(); script.addBrick(new SetTransparencyBrick(100.0)); diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/backpack/ActorAndObjectRecyclerViewFragment.java b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/backpack/ActorAndObjectRecyclerViewFragment.java index 08595bf1e9c..edcf9dec00b 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/backpack/ActorAndObjectRecyclerViewFragment.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/backpack/ActorAndObjectRecyclerViewFragment.java @@ -25,7 +25,6 @@ import android.os.Bundle; import android.view.LayoutInflater; -import android.view.Menu; import android.view.View; import android.view.ViewGroup; @@ -100,11 +99,6 @@ public void onStop() { finishActionMode(); } - @Override - public void onPrepareOptionsMenu(Menu menu) { - super.onPrepareOptionsMenu(menu); - } - protected void finishActionMode() { adapter.clearSelection(); setShowProgressBar(false); @@ -113,7 +107,6 @@ protected void finishActionMode() { @Override public void onItemClick(final T item, MultiSelectionManager selectionManager) { unpackItems(new ArrayList<>(Collections.singletonList(item))); - } public void setShowProgressBar(boolean show) { diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/dialog/TextInputDialog.java b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/dialog/TextInputDialog.java index d262373a8ae..32cb1deb88a 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/dialog/TextInputDialog.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/dialog/TextInputDialog.java @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team + * Copyright (C) 2010-2024 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -26,7 +26,6 @@ import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; -import android.text.Editable; import android.widget.EditText; import com.google.android.material.textfield.TextInputLayout; @@ -134,39 +133,6 @@ public boolean isUnique(String newName) { } } - public abstract static class TextWatcher implements android.text.TextWatcher { - - private TextInputLayout inputLayout; - private AlertDialog alertDialog; - - private void setInputLayout(@NonNull TextInputLayout inputLayout) { - this.inputLayout = inputLayout; - } - - private void setAlertDialog(@NonNull AlertDialog alertDialog) { - this.alertDialog = alertDialog; - } - - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - String input = s.toString(); - String error = validateInput(input, alertDialog.getContext()); - inputLayout.setError(error); - alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(error == null); - } - - @Nullable - public abstract String validateInput(String input, Context context); - } - public interface OnClickListener { void onPositiveButtonClick(DialogInterface dialog, String textInput); diff --git a/catroid/src/test/java/org/catrobat/catroid/test/physics/PhysicsWorldConverterAngleTest.java b/catroid/src/test/java/org/catrobat/catroid/test/physics/PhysicsWorldConverterAngleTest.java index 1e7f11ea20b..6db90557b8a 100644 --- a/catroid/src/test/java/org/catrobat/catroid/test/physics/PhysicsWorldConverterAngleTest.java +++ b/catroid/src/test/java/org/catrobat/catroid/test/physics/PhysicsWorldConverterAngleTest.java @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team + * Copyright (C) 2010-2024 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -22,16 +22,16 @@ */ package org.catrobat.catroid.test.physics; -import junit.framework.Assert; - import org.catrobat.catroid.physics.PhysicsWorldConverter; -import org.catrobat.catroid.test.utils.TestConstants; +import org.catrobat.catroid.test.utils.PhysicsWorldConverterAngleTestUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.Arrays; +import static org.junit.Assert.assertEquals; + @RunWith(Parameterized.class) public class PhysicsWorldConverterAngleTest { @@ -55,11 +55,11 @@ public static Iterable data() { @Test public void testBox2dToNormalAngleConversion() { - Assert.assertEquals(deg, PhysicsWorldConverter.convertBox2dToNormalAngle(rad), TestConstants.DELTA); + assertEquals(deg, PhysicsWorldConverter.convertBox2dToNormalAngle(rad), PhysicsWorldConverterAngleTestUtils.DELTA); } @Test public void testNormalToBox2dAngleConversion() { - Assert.assertEquals(rad, PhysicsWorldConverter.convertNormalToBox2dAngle(deg), TestConstants.DELTA); + assertEquals(rad, PhysicsWorldConverter.convertNormalToBox2dAngle(deg), PhysicsWorldConverterAngleTestUtils.DELTA); } } diff --git a/catroid/src/test/java/org/catrobat/catroid/test/utils/TestConstants.java b/catroid/src/test/java/org/catrobat/catroid/test/utils/PhysicsWorldConverterAngleTestUtils.java similarity index 87% rename from catroid/src/test/java/org/catrobat/catroid/test/utils/TestConstants.java rename to catroid/src/test/java/org/catrobat/catroid/test/utils/PhysicsWorldConverterAngleTestUtils.java index d3eafb4ad21..7676daee41a 100644 --- a/catroid/src/test/java/org/catrobat/catroid/test/utils/TestConstants.java +++ b/catroid/src/test/java/org/catrobat/catroid/test/utils/PhysicsWorldConverterAngleTestUtils.java @@ -1,6 +1,6 @@ /* * Catroid: An on-device visual programming system for Android devices - * Copyright (C) 2010-2022 The Catrobat Team + * Copyright (C) 2010-2024 The Catrobat Team * () * * This program is free software: you can redistribute it and/or modify @@ -23,10 +23,10 @@ package org.catrobat.catroid.test.utils; -public final class TestConstants { +public final class PhysicsWorldConverterAngleTestUtils { public static final double DELTA = 0.00001; - private TestConstants() { + private PhysicsWorldConverterAngleTestUtils() { throw new AssertionError(); } }