From e370030e6c6dcb5920fe5119a65d3bfee6c54b87 Mon Sep 17 00:00:00 2001 From: Eric Banks Date: Wed, 18 Jul 2012 22:44:37 -0400 Subject: [PATCH] As requested by Mark, I've broken out the code to pull out the protected subclass when available (and otherwise use the public version) into the GATKLiteUtils class. People should use this code instead of reimplementing all of the java reflection on their own. --- .../sting/gatk/GenomeAnalysisEngine.java | 4 +- .../bqsr/BaseQualityScoreRecalibrator.java | 21 +---- .../utils/classloader/GATKLiteUtils.java | 85 +++++++++++++++++++ .../sting/utils/classloader/JVMUtils.java | 17 ---- 4 files changed, 91 insertions(+), 36 deletions(-) create mode 100755 public/java/src/org/broadinstitute/sting/utils/classloader/GATKLiteUtils.java diff --git a/public/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisEngine.java b/public/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisEngine.java index 9d2a138fa..8dfa89083 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisEngine.java +++ b/public/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisEngine.java @@ -51,7 +51,7 @@ import org.broadinstitute.sting.gatk.samples.SampleDBBuilder; import org.broadinstitute.sting.gatk.walkers.*; import org.broadinstitute.sting.utils.*; import org.broadinstitute.sting.utils.baq.BAQ; -import org.broadinstitute.sting.utils.classloader.JVMUtils; +import org.broadinstitute.sting.utils.classloader.GATKLiteUtils; import org.broadinstitute.sting.utils.codecs.vcf.VCFCodec; import org.broadinstitute.sting.utils.codecs.vcf.VCFHeader; import org.broadinstitute.sting.utils.collections.Pair; @@ -206,7 +206,7 @@ public class GenomeAnalysisEngine { * Utility method to determine whether this is the lite version of the GATK */ public boolean isGATKLite() { - return JVMUtils.isGATKLite(); + return GATKLiteUtils.isGATKLite(); } /** diff --git a/public/java/src/org/broadinstitute/sting/gatk/walkers/bqsr/BaseQualityScoreRecalibrator.java b/public/java/src/org/broadinstitute/sting/gatk/walkers/bqsr/BaseQualityScoreRecalibrator.java index 1a99d51e6..8210a11b7 100755 --- a/public/java/src/org/broadinstitute/sting/gatk/walkers/bqsr/BaseQualityScoreRecalibrator.java +++ b/public/java/src/org/broadinstitute/sting/gatk/walkers/bqsr/BaseQualityScoreRecalibrator.java @@ -34,8 +34,7 @@ import org.broadinstitute.sting.gatk.filters.MappingQualityZeroFilter; import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker; import org.broadinstitute.sting.gatk.walkers.*; import org.broadinstitute.sting.utils.baq.BAQ; -import org.broadinstitute.sting.utils.classloader.PluginManager; -import org.broadinstitute.sting.utils.classloader.ProtectedPackageSource; +import org.broadinstitute.sting.utils.classloader.GATKLiteUtils; import org.broadinstitute.sting.utils.collections.Pair; import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; import org.broadinstitute.sting.utils.exceptions.UserException; @@ -179,27 +178,15 @@ public class BaseQualityScoreRecalibrator extends LocusWalker implem } private RecalibrationEngine initializeRecalibrationEngine() { - List> REclasses = new PluginManager(RecalibrationEngine.class).getPlugins(); - if ( REclasses.isEmpty() ) - throw new ReviewedStingException("The RecalibrationEngine class is not found; repository must be corrupted"); - - Class c = null; - for ( Class REclass : REclasses ) { - if ( ProtectedPackageSource.class.isAssignableFrom(REclass) ) { - c = REclass; - break; - } - } - if ( c == null ) - c = REclasses.get(0); + final Class recalibrationEngineClass = GATKLiteUtils.getProtectedClassIfAvailable(RecalibrationEngine.class); try { - Constructor constructor = c.getDeclaredConstructor((Class[])null); + Constructor constructor = recalibrationEngineClass.getDeclaredConstructor((Class[])null); constructor.setAccessible(true); return (RecalibrationEngine)constructor.newInstance(); } catch (Exception e) { - throw new ReviewedStingException("Unable to create RecalibrationEngine class instance " + c.getSimpleName()); + throw new ReviewedStingException("Unable to create RecalibrationEngine class instance " + recalibrationEngineClass.getSimpleName()); } } diff --git a/public/java/src/org/broadinstitute/sting/utils/classloader/GATKLiteUtils.java b/public/java/src/org/broadinstitute/sting/utils/classloader/GATKLiteUtils.java new file mode 100755 index 000000000..db9b9d8b7 --- /dev/null +++ b/public/java/src/org/broadinstitute/sting/utils/classloader/GATKLiteUtils.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2010 The Broad Institute + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR + * THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package org.broadinstitute.sting.utils.classloader; + +import org.broadinstitute.sting.utils.exceptions.ReviewedStingException; + +import java.util.*; + +/** + * Created by IntelliJ IDEA. + * User: ebanks + * + * A set of static utility methods for working with the full vs. Lite GATK build + */ +public class GATKLiteUtils { + /** + * Constructor access disallowed...static utility methods only! + */ + private GATKLiteUtils() { } + + /** + * Utility method to determine whether this is the lite version of the GATK + */ + public static boolean isGATKLite() { + if ( isLiteVersion == null ) { + try { + Class.forName(DummyProtectedClassName); + isLiteVersion = false; + } catch ( ClassNotFoundException e) { + isLiteVersion = true; + } + } + return isLiteVersion; + } + private static final String DummyProtectedClassName = "org.broadinstitute.sting.gatk.DummyProtectedClass"; + private static Boolean isLiteVersion = null; + + + /** + * Utility method to pull out a protected subclass if possible, otherwise it falls back to a public subclass. + * Important note: the protected classes MUST implement ProtectedPackageSource! + * + * @param interfaceClass the interface class which the target classes implement + */ + public static Class getProtectedClassIfAvailable(final Class interfaceClass) { + List> classes = new PluginManager(interfaceClass).getPlugins(); + if ( classes.isEmpty() ) + throw new ReviewedStingException("No classes implementing the interface class " + interfaceClass.getSimpleName() + " were found"); + + Class result = null; + for ( Class c : classes ) { + if ( ProtectedPackageSource.class.isAssignableFrom(c) ) { + result = c; + break; + } + } + if ( result == null ) + result = classes.get(0); + + return result; + } +} diff --git a/public/java/src/org/broadinstitute/sting/utils/classloader/JVMUtils.java b/public/java/src/org/broadinstitute/sting/utils/classloader/JVMUtils.java index 745c9ad1e..fa154fca3 100755 --- a/public/java/src/org/broadinstitute/sting/utils/classloader/JVMUtils.java +++ b/public/java/src/org/broadinstitute/sting/utils/classloader/JVMUtils.java @@ -105,23 +105,6 @@ public class JVMUtils { return allFields; } - /** - * Utility method to determine whether this is the lite version of the GATK - */ - public static boolean isGATKLite() { - if ( isLiteVersion == null ) { - try { - Class.forName(DummyProtectedClassName); - isLiteVersion = false; - } catch ( ClassNotFoundException e) { - isLiteVersion = true; - } - } - return isLiteVersion; - } - private static final String DummyProtectedClassName = "org.broadinstitute.sting.gatk.DummyProtectedClass"; - private static Boolean isLiteVersion = null; - /** * Find the field with the given name in the class. Will inspect all fields, independent * of access level.