Created
November 25, 2016 22:21
-
-
Save nelsonni/058f5e275904e698dc5d7e55b2c15609 to your computer and use it in GitHub Desktop.
Java method for examining IntelliJ PsiClass object for Constant Integer Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Nicholas Nelson <nelsonni@oregonstate.edu> Created on on 7/28/16. | |
*/ | |
public class CallbackUtil { | |
// ... | |
/** | |
* Examines all PsiFields within a target PsiClass and returns an array of Integer-type fields ordered by | |
* their numerical values. | |
* | |
* @param psiClass the class to target for analysis of all declared PsiFields. | |
* @return an Optional containing constant Integer PsiFields ordered by Integer value; or Optional.empty if none. | |
*/ | |
@NotNull | |
private static Optional<PsiField[]> getConstantIntegerFields(PsiClass psiClass) { | |
PsiField[] fields = psiClass.getFields(); | |
PsiField[] integerFields = Arrays.stream(fields) | |
.filter(f -> f.getType().equalsToText("int")) | |
.filter(f -> f.computeConstantValue() != null) | |
.sorted((f1, f2) -> Integer.compare((int) f1.computeConstantValue(), (int) f2.computeConstantValue())) | |
.toArray(PsiField[]::new); | |
return integerFields.length > 0 ? Optional.of(integerFields) : Optional.empty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment