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

Fix deprecated uses of newInstance() #1887

Merged
merged 2 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/main/java/picard/cmdline/PicardCommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.broadinstitute.barclay.argparser.CommandLineProgramProperties;
import org.broadinstitute.barclay.argparser.ExperimentalFeature;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -150,12 +151,11 @@ else if (!clProperties.omitFromCommandLine()) { /** We should check for missing
printCommandList(classes);
} else {
if (simpleNameToClass.containsKey(args[0])) {
final Class clazz = simpleNameToClass.get(args[0]);
final Class<?> clazz = simpleNameToClass.get(args[0]);
try {
return (CommandLineProgram)clazz.newInstance();
} catch (final InstantiationException e) {
throw new RuntimeException(e);
} catch (final IllegalAccessException e) {
return (CommandLineProgram)clazz.getDeclaredConstructor().newInstance();
} catch (final InstantiationException | IllegalAccessException
| InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
Expand Down Expand Up @@ -229,10 +229,9 @@ private static void printUsage(final Set<Class<?>> classes, final String command
CommandLineProgramGroup programGroup = programGroupClassToProgramGroupInstance.get(property.programGroup());
if (null == programGroup) {
try {
programGroup = property.programGroup().newInstance();
} catch (final InstantiationException e) {
throw new RuntimeException(e);
} catch (final IllegalAccessException e) {
programGroup = property.programGroup().getDeclaredConstructor().newInstance();
} catch (final InstantiationException | IllegalAccessException
| NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException(e);
}
programGroupClassToProgramGroupInstance.put(property.programGroup(), programGroup);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/picard/sam/MergeBamAlignment.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,22 +310,22 @@ enum PrimaryAlignmentStrategy implements CommandLineParser.ClpEnum{
"the alignment pair with the largest insert size. If all alignments would be chimeric, it picks the " +
"alignments for each end with the best MAPQ. ");

private final Class<PrimaryAlignmentSelectionStrategy> clazz;
private final Class<? extends PrimaryAlignmentSelectionStrategy> clazz;

private final String description;

public String getHelpDoc() {
return description;
}

PrimaryAlignmentStrategy(final Class<?> clazz, final String description) {
this.clazz = (Class<PrimaryAlignmentSelectionStrategy>) clazz;
PrimaryAlignmentStrategy(final Class<? extends PrimaryAlignmentSelectionStrategy> clazz, final String description) {
this.clazz = clazz;
this.description = description;
}

PrimaryAlignmentSelectionStrategy newInstance() {
try {
return clazz.newInstance();
return clazz.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new PicardException("Trouble instantiating " + clazz.getName(), e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/picard/TestDataProviders.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public Iterator<Object[]> testAllDataProvidersData() throws Exception {
// https://github.com/cbeust/testng/blob/master/src/test/java/test/inject/NoInjectionTest.java
@Test(dataProvider = "DataprovidersThatDontTestThemselves")
public void testDataProviderswithDP(@NoInjection final Method method, final Class clazz) throws
IllegalAccessException, InstantiationException {
IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {

Object instance = clazz.newInstance();
Object instance = clazz.getDeclaredConstructor().newInstance();

Set<Method> methodSet = new HashSet<>();
methodSet.addAll(Arrays.asList(clazz.getDeclaredMethods()));
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/picard/cmdline/PicardCommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class PicardCommandLineTest {
Expand Down Expand Up @@ -50,13 +52,13 @@ public void testLaunchAllCommandLineProgramsWithBarclayParser() {
// Check for missing annotations
Assert.assertNotNull(clProperties);
try {
final Object commandLineProgram = clazz.newInstance();
final Object commandLineProgram = clazz.getDeclaredConstructor().newInstance();
try {
new CommandLineArgumentParser(commandLineProgram);
} catch (CommandLineException.CommandLineParserInternalException e) {
throw new RuntimeException("Barclay command line parser internal exception parsing class: " + clazz.getName(), e);
}
} catch (IllegalAccessException | InstantiationException e) {
} catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException("Failure instantiating command line program: " + clazz.getName(), e);
}
});
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/picard/sam/CramCompatibilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import picard.cmdline.CommandLineProgram;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -258,7 +259,12 @@ private void launchProgram(String programClassname,
args.add("REFERENCE_SEQUENCE=" + new File(reference).getAbsolutePath());
}

final CommandLineProgram program = (CommandLineProgram) Class.forName(programClassname).newInstance();
final CommandLineProgram program;
try {
program = (CommandLineProgram) Class.forName(programClassname).getDeclaredConstructor().newInstance();
} catch (InvocationTargetException | NoSuchMethodException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced that with catch (ReflectiveOperationException e) and removed a bunch of boilerplate.

throw new RuntimeException(e);
}
program.instanceMain(args.toArray(new String[0]));
}

Expand Down