Skip to content

Commit aebd2b8

Browse files
author
Fabrice-TIERCELIN
committed
ASTNodes.as(Expression)
1 parent 0d63a43 commit aebd2b8

1 file changed

Lines changed: 33 additions & 1 deletion

File tree

  • plugin/src/main/java/org/autorefactor/jdt/internal/corext/dom

plugin/src/main/java/org/autorefactor/jdt/internal/corext/dom/ASTNodes.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.Map;
4949
import java.util.Set;
5050
import java.util.TreeSet;
51+
import java.util.concurrent.atomic.AtomicReference;
5152

5253
import org.autorefactor.util.IllegalArgumentException;
5354
import org.autorefactor.util.IllegalStateException;
@@ -367,6 +368,37 @@ public static boolean is(Statement statement, Class<? extends Statement> stmtCla
367368
return as(statement, stmtClass) != null;
368369
}
369370

371+
/**
372+
* Casts the provided expression to an object of the provided type if type
373+
* matches.
374+
*
375+
* @param <T> the required expression type
376+
* @param expression the expression to cast
377+
* @return the provided expression as an object of the provided type if type
378+
* matches, null otherwise
379+
*/
380+
public static <T extends Expression> T as(Expression expression) {
381+
Class<T> theClass= getParameterizedClass();
382+
383+
if (theClass == null) {
384+
return null;
385+
}
386+
387+
return as(expression, theClass);
388+
}
389+
390+
private static <T extends Expression> Class<T> getParameterizedClass() {
391+
AtomicReference<T> ar= new AtomicReference<>();
392+
String className = ar.getClass().getCanonicalName();
393+
String elementClassName = className.replaceFirst("\\Ajava\\.util\\.concurrent\\.atomic\\.AtomicReference\\<(.*)\\>\\z", "\\1");
394+
throw new NullPointerException("className " + className + " elementClassName " + elementClassName);
395+
// try {
396+
// return (Class<T>) Class.forName(elementClassName);
397+
// } catch (ClassNotFoundException e) {
398+
// return null;
399+
// }
400+
}
401+
370402
/**
371403
* Casts the provided expression to an object of the provided type if type
372404
* matches.
@@ -1516,7 +1548,7 @@ public static boolean isHardCoded(final Expression expression) {
15161548
* @return Integer literal.
15171549
*/
15181550
public static Long integerLiteral(Expression input) {
1519-
NumberLiteral contant= as(input, NumberLiteral.class);
1551+
NumberLiteral contant= as(input);
15201552
PrefixExpression negativeContant= as(input, PrefixExpression.class);
15211553

15221554
if (negativeContant != null && hasOperator(negativeContant, PrefixExpression.Operator.MINUS)) {

0 commit comments

Comments
 (0)