Skip to content

Commit ea0b5a9

Browse files
committed
Replace deprecated newInstance() method
1 parent 87f92a7 commit ea0b5a9

17 files changed

+48
-56
lines changed

src/edu/stanford/nlp/dcoref/SieveCoreferenceSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public static String initializeAndRunCoref(Properties props) throws Exception {
371371
}
372372
mentionFinder = (CorefMentionFinder) Class.forName(mentionFinderClass).getConstructor(Properties.class).newInstance(mentionFinderProps);
373373
} else {
374-
mentionFinder = (CorefMentionFinder) Class.forName(mentionFinderClass).newInstance();
374+
mentionFinder = (CorefMentionFinder) Class.forName(mentionFinderClass).getDeclaredConstructor().newInstance();
375375
}
376376
mentionExtractor.setMentionFinder(mentionFinder);
377377
}

src/edu/stanford/nlp/ie/crf/CRFClassifier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ public List<IN> classifyGibbs(List<IN> document, Triple<int[][][], int[], double
12241224

12251225
CRFCliqueTree<? extends CharSequence> cliqueTree = getCliqueTree(documentDataAndLabels);
12261226

1227-
PriorModelFactory<IN> pmf = (PriorModelFactory<IN>) Class.forName(flags.priorModelFactory).newInstance();
1227+
PriorModelFactory<IN> pmf = (PriorModelFactory<IN>) Class.forName(flags.priorModelFactory).getDeclaredConstructor().newInstance();
12281228
ListeningSequenceModel prior = pmf.getInstance(flags.backgroundSymbol, classIndex, tagIndex, newDocument, entityMatrices, flags);
12291229

12301230
if ( ! flags.useUniformPrior) {
@@ -2235,7 +2235,7 @@ protected void loadTextClassifier(BufferedReader br) throws Exception {
22352235
}
22362236
featureFactories = Generics.newArrayList();
22372237
for (int ff = 1; ff < featureFactoryName.length - 1; ++ff) {
2238-
FeatureFactory<IN> featureFactory = (FeatureFactory<IN>) Class.forName(featureFactoryName[1]).newInstance();
2238+
FeatureFactory<IN> featureFactory = (FeatureFactory<IN>) Class.forName(featureFactoryName[1]).getDeclaredConstructor().newInstance();
22392239
featureFactory.init(flags);
22402240
featureFactories.add(featureFactory);
22412241
}

src/edu/stanford/nlp/ling/AnnotationLookup.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ <T> KeyLookup(Class<? extends CoreAnnotation<T>> coreKey, String oldKey) {
112112
/**
113113
* This constructor allows us to use reflection for loading old class keys.
114114
* This is useful because we can then create distributions that do not have
115-
* all of the classes required for all the old keys (such as trees package classes).
115+
* all the classes required for all the old keys (such as trees package classes).
116116
*/
117117
@SuppressWarnings("unused")
118118
KeyLookup(String className, String oldKey) {
@@ -155,16 +155,15 @@ public static Class<? extends CoreAnnotation<?>> toCoreKey(String stringKey) {
155155
* @param key The annotation key (non-null)
156156
* @return The type of the value of that key (non-null)
157157
*/
158-
@SuppressWarnings("unchecked")
159-
public static Class<?> getValueType(Class<? extends CoreAnnotation<?>> key) {
160-
Class type = valueCache.get(key);
158+
public static Class<?> getValueType(Class<? extends CoreAnnotation<?>> key) {
159+
Class<?> type = valueCache.get(key);
161160
if (type == null) {
162161
try {
163-
type = key.newInstance().getType();
164-
} catch (Exception e) {
162+
type = key.getDeclaredConstructor().newInstance().getType();
163+
} catch (ReflectiveOperationException e) {
165164
throw new UnsupportedOperationException("Unexpected failure to instantiate - is your key class fancy?", e);
166165
}
167-
valueCache.put((Class)key, type);
166+
valueCache.put(key, type);
168167
}
169168
return type;
170169
}

src/edu/stanford/nlp/ling/tokensregex/types/Expressions.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,14 +1261,14 @@ private static Value attemptTypeConversion(CompositeValue cv, Env env, Object...
12611261
// If so, then try to instantiate a new instance of the class
12621262
if (TYPE_CLASS.equals(typeValue.getType())) {
12631263
// Variable maps to a java class
1264-
Class c = (Class) typeValue.get();
1264+
Class<?> c = (Class<?>) typeValue.get();
12651265
try {
1266-
Object obj = c.newInstance();
1266+
Object obj = c.getDeclaredConstructor().newInstance();
12671267
// for any field other than the "type", set the value of the field
12681268
// of the created object to the specified value
12691269
for (String s:cv.value.keySet()) {
12701270
if (!"type".equals(s)) {
1271-
Value v = cv.value.get(s).evaluate(env, args);
1271+
Value<?> v = cv.value.get(s).evaluate(env, args);
12721272
try {
12731273
Field f = c.getField(s);
12741274
Object objVal = toCompatibleObject(f, v.get());
@@ -1281,14 +1281,14 @@ private static Value attemptTypeConversion(CompositeValue cv, Env env, Object...
12811281
}
12821282
}
12831283
return new PrimitiveValue<>(typeName, obj);
1284-
} catch (InstantiationException | IllegalAccessException ex) {
1284+
} catch (ReflectiveOperationException ex) {
12851285
throw new RuntimeException("Cannot instantiate " + c, ex);
12861286
}
12871287
} else if (typeValue.get() != null){
12881288
// When evaluated, variable does not explicitly map to "CLASS"
12891289
// See if we can convert this CompositeValue into appropriate object
12901290
// by calling "create(CompositeValue cv)"
1291-
Class c = typeValue.get().getClass();
1291+
Class<?> c = typeValue.get().getClass();
12921292
try {
12931293
Method m = c.getMethod("create", CompositeValue.class);
12941294
CompositeValue evaluatedCv = cv.evaluateNoTypeConversion(env, args);

src/edu/stanford/nlp/parser/lexparser/ChineseTreebankParserParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ public int setOptionFlag(String[] args, int i) {
11531153
i += 2;
11541154
} else if (args[i].equalsIgnoreCase("-headFinder") && (i + 1 < args.length)) {
11551155
try {
1156-
headFinder = (HeadFinder) Class.forName(args[i + 1]).newInstance();
1156+
headFinder = (HeadFinder) Class.forName(args[i + 1]).getDeclaredConstructor().newInstance();
11571157
} catch (Exception e) {
11581158
log.info(e);
11591159
log.info(this.getClass().getName() + ": Could not load head finder " + args[i + 1]);

src/edu/stanford/nlp/parser/lexparser/FactoredParser.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,13 @@ public static void main(String[] args) {
118118
i += 2;
119119
} else if (args[i].equalsIgnoreCase("-tLPP") && (i + 1 < args.length)) {
120120
try {
121-
op.tlpParams = (TreebankLangParserParams) Class.forName(args[i + 1]).newInstance();
121+
op.tlpParams = (TreebankLangParserParams) Class.forName(args[i + 1]).getDeclaredConstructor().newInstance();
122122
} catch (ClassNotFoundException e) {
123123
log.info("Class not found: " + args[i + 1]);
124124
throw new RuntimeException(e);
125-
} catch (InstantiationException e) {
125+
} catch (ReflectiveOperationException e) {
126126
log.info("Couldn't instantiate: " + args[i + 1] + ": " + e.toString());
127127
throw new RuntimeException(e);
128-
} catch (IllegalAccessException e) {
129-
log.info("illegal access" + e);
130-
throw new RuntimeException(e);
131128
}
132129
i += 2;
133130
} else if (args[i].equals("-encoding")) {

src/edu/stanford/nlp/parser/lexparser/FrenchTreebankParserParams.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ public int setOptionFlag(String[] args, int i) {
663663

664664
} else if (args[i].equalsIgnoreCase("-headFinder") && (i + 1 < args.length)) {
665665
try {
666-
HeadFinder hf = (HeadFinder) Class.forName(args[i + 1]).newInstance();
666+
HeadFinder hf = (HeadFinder) Class.forName(args[i + 1]).getDeclaredConstructor().newInstance();
667667
setHeadFinder(hf);
668668
optionsString.append("HeadFinder: " + args[i + 1] + "\n");
669669

src/edu/stanford/nlp/parser/lexparser/SpanishTreebankParserParams.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ public MemoryTreebank memoryTreebank() {
297297
public int setOptionFlag(String[] args, int i) {
298298
if (args[i].equalsIgnoreCase("-headFinder") && (i + 1 < args.length)) {
299299
try {
300-
HeadFinder hf = (HeadFinder) Class.forName(args[i + 1]).newInstance();
300+
HeadFinder hf = (HeadFinder) Class.forName(args[i + 1]).getDeclaredConstructor().newInstance();
301301
setHeadFinder(hf);
302302

303303
optionsString.append("HeadFinder: " + args[i + 1] + "\n");
304-
} catch (ReflectiveOperationException|SecurityException e) {
304+
} catch (ReflectiveOperationException | SecurityException e) {
305305
log.info(e);
306306
log.info(this.getClass().getName() + ": Could not load head finder " + args[i + 1]);
307307
}

src/edu/stanford/nlp/parser/lexparser/TreeBinarizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,15 +662,15 @@ public static void main(String[] args) {
662662
while (i < args.length && args[i].startsWith("-")) {
663663
if (args[i].equalsIgnoreCase("-tlp") && i + 1 < args.length) {
664664
try {
665-
tlp = (TreebankLanguagePack) Class.forName(args[i+1]).newInstance();
665+
tlp = (TreebankLanguagePack) Class.forName(args[i+1]).getDeclaredConstructor().newInstance();
666666
} catch (Exception e) {
667667
log.info("Couldn't instantiate: " + args[i+1]);
668668
throw new RuntimeException(e);
669669
}
670670
i++;
671671
} else if (args[i].equalsIgnoreCase("-tlpp") && i + 1 < args.length) {
672672
try {
673-
tlpp = (TreebankLangParserParams) Class.forName(args[i+1]).newInstance();
673+
tlpp = (TreebankLangParserParams) Class.forName(args[i+1]).getDeclaredConstructor().newInstance();
674674
} catch (Exception e) {
675675
log.info("Couldn't instantiate: " + args[i+1]);
676676
throw new RuntimeException(e);

src/edu/stanford/nlp/sequences/ColumnTabDocumentReaderWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void init(SeqClassifierFlags flags) {
7474

7575
if (flags.tokenFactory != null) {
7676
try {
77-
this.tokenFactory = (CoreTokenFactory<IN>) Class.forName(flags.tokenFactory).newInstance();
77+
this.tokenFactory = (CoreTokenFactory<IN>) Class.forName(flags.tokenFactory).getDeclaredConstructor().newInstance();
7878
} catch (Exception e) {
7979
throw new RuntimeException(e);
8080
}
@@ -102,7 +102,7 @@ public void init(String name, Properties props) {
102102
String tokenFactoryClassName = props.getProperty(prefix + "tokenFactory");
103103
if (tokenFactoryClassName != null) {
104104
try {
105-
this.tokenFactory = (CoreTokenFactory<IN>) Class.forName(tokenFactoryClassName).newInstance();
105+
this.tokenFactory = (CoreTokenFactory<IN>) Class.forName(tokenFactoryClassName).getDeclaredConstructor().newInstance();
106106
} catch (Exception e) {
107107
throw new RuntimeException(e);
108108
}

0 commit comments

Comments
 (0)