Skip to content

Instantly share code, notes, and snippets.

@nelsonni
Created November 25, 2016 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelsonni/058f5e275904e698dc5d7e55b2c15609 to your computer and use it in GitHub Desktop.
Save nelsonni/058f5e275904e698dc5d7e55b2c15609 to your computer and use it in GitHub Desktop.
Java method for examining IntelliJ PsiClass object for Constant Integer Fields
/**
* @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