@Override public PsiField findFieldByName(@NonNls String name, boolean checkBases) { final PsiField[] fields = getFields(); for (final PsiField field : fields) { if (name.equals(field.getName())) return field; } return null; }
private static boolean isCompileTimeFlagReference(PsiElement element) { PsiElement resolved = element instanceof PsiReferenceExpression ? ((PsiReferenceExpression) element).resolve() : null; if (!(resolved instanceof PsiField)) return false; PsiField field = (PsiField) resolved; return field.hasModifierProperty(PsiModifier.FINAL) && field.hasModifierProperty(PsiModifier.STATIC) && PsiType.BOOLEAN.equals(field.getType()); }
public static boolean isFieldEquivalentTo(@NotNull PsiField field, PsiElement another) { if (!(another instanceof PsiField)) return false; String name1 = field.getName(); if (name1 == null) return false; if (!another.isValid()) return false; String name2 = ((PsiField) another).getName(); if (!name1.equals(name2)) return false; PsiClass aClass1 = field.getContainingClass(); PsiClass aClass2 = ((PsiField) another).getContainingClass(); return aClass1 != null && aClass2 != null && field.getManager().areElementsEquivalent(aClass1, aClass2); }
private static boolean isCompileConstantInIfCondition(PsiElement element) { if (!(element instanceof PsiReferenceExpression)) return false; PsiElement resolved = ((PsiReferenceExpression) element).resolve(); if (!(resolved instanceof PsiField)) return false; PsiField field = (PsiField) resolved; if (!field.hasModifierProperty(PsiModifier.FINAL)) return false; if (!field.hasModifierProperty(PsiModifier.STATIC)) return false; PsiElement parent = element.getParent(); if (parent instanceof PsiPrefixExpression && ((PsiPrefixExpression) parent).getOperationTokenType() == JavaTokenType.EXCL) { element = parent; parent = parent.getParent(); } return parent instanceof PsiIfStatement && ((PsiIfStatement) parent).getCondition() == element; }
public static void prepareWizardData(final WizardData data, PsiClass boundClass) throws MyException { final PsiMethod[] allGetDataMethods = boundClass.findMethodsByName("getData", false); final PsiMethod[] allSetDataMethods = boundClass.findMethodsByName("setData", false); PsiMethod setDataMethod = null; PsiClass beanClass = null; // find get/set pair and bean class outer: for (int i = 0; i < allGetDataMethods.length; i++) { final PsiMethod _getMethod = allGetDataMethods[i]; if (_getMethod.getReturnType() != PsiType.VOID) { continue; } final PsiParameter[] _getMethodParameters = _getMethod.getParameterList().getParameters(); if (_getMethodParameters.length != 1) { continue; } final PsiClass _getParameterClass = getClassByType(_getMethodParameters[0].getType()); if (_getParameterClass == null) { continue; } for (final PsiMethod _setMethod : allSetDataMethods) { if (_setMethod.getReturnType() != PsiType.VOID) { continue; } final PsiParameter[] _setMethodParameters = _setMethod.getParameterList().getParameters(); if (_setMethodParameters.length != 1) { continue; } final PsiClass _setParameterClass = getClassByType(_setMethodParameters[0].getType()); if (_setParameterClass != _getParameterClass) { continue; } // pair found !!! setDataMethod = _setMethod; beanClass = _getParameterClass; break outer; } } if (beanClass == null) { // nothing found return; } data.myBindToNewBean = false; data.myBeanClass = beanClass; // parse setData() and try to associate fields with bean { final PsiCodeBlock body = setDataMethod.getBody(); if (body == null) { return; } final PsiElement[] children = body.getChildren(); for (PsiElement child : children) { // Parses sequences like: a.foo(b.bar()); final PsiField bindingField; if (!(child instanceof PsiExpressionStatement)) { continue; } final PsiExpression expression = ((PsiExpressionStatement) child).getExpression(); if (!(expression instanceof PsiMethodCallExpression)) { continue; } final PsiMethodCallExpression callExpression = (PsiMethodCallExpression) expression; // find binding field ('a') int index = -1; { final PsiElement psiElement = getObjectForWhichMethodWasCalled(callExpression); if (!(psiElement instanceof PsiField)) { continue; } if (((PsiField) psiElement).getContainingClass() != boundClass) { continue; } bindingField = (PsiField) psiElement; // find binding for this field final FormProperty2BeanProperty[] bindings = data.myBindings; for (int j = 0; j < bindings.length; j++) { final FormProperty2BeanProperty binding = bindings[j]; if (bindingField .getName() .equals(binding.myFormProperty.getLwComponent().getBinding())) { index = j; break; } } } if (index == -1) { continue; } // find 'bar()' { final PsiReferenceParameterList parameterList = callExpression.getMethodExpression().getParameterList(); if (parameterList == null) { continue; } final PsiExpressionList argumentList = callExpression.getArgumentList(); if (argumentList == null) { continue; } final PsiExpression[] expressions = argumentList.getExpressions(); if (expressions == null || expressions.length != 1) { continue; } if (!(expressions[0] instanceof PsiMethodCallExpression)) { continue; } final PsiMethodCallExpression callExpression2 = ((PsiMethodCallExpression) expressions[0]); // check that 'b' is parameter final PsiElement psiElement = getObjectForWhichMethodWasCalled(callExpression2); if (!(psiElement instanceof PsiParameter)) { continue; } final PsiMethod barMethod = ((PsiMethod) callExpression2.getMethodExpression().resolve()); if (barMethod == null) { continue; } if (!PropertyUtil.isSimplePropertyGetter(barMethod)) { continue; } final String propertyName = PropertyUtil.getPropertyName(barMethod); // There are two possible types: boolean and java.lang.String String typeName = barMethod.getReturnType().getCanonicalText(); if (!"boolean".equals(typeName) && !"java.lang.String".equals(typeName)) { continue; } data.myBindings[index].myBeanProperty = new BeanProperty(propertyName, typeName); } } } }
private boolean checkCondition( @Nullable PsiExpression condition, @NotNull PsiStatement context, List<PsiExpression> notUpdated) { if (condition == null) { return false; } if (PsiUtil.isConstantExpression(condition) || PsiKeyword.NULL.equals(condition.getText())) { return true; } if (condition instanceof PsiInstanceOfExpression) { final PsiInstanceOfExpression instanceOfExpression = (PsiInstanceOfExpression) condition; final PsiExpression operand = instanceOfExpression.getOperand(); return checkCondition(operand, context, notUpdated); } else if (condition instanceof PsiParenthesizedExpression) { // catch stuff like "while ((x)) { ... }" final PsiExpression expression = ((PsiParenthesizedExpression) condition).getExpression(); return checkCondition(expression, context, notUpdated); } else if (condition instanceof PsiPolyadicExpression) { // while (value != x) { ... } // while (value != (x + y)) { ... } // while (b1 && b2) { ... } final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression) condition; for (PsiExpression operand : polyadicExpression.getOperands()) { if (!checkCondition(operand, context, notUpdated)) { return false; } } return true; } else if (condition instanceof PsiReferenceExpression) { final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) condition; final PsiElement element = referenceExpression.resolve(); if (element instanceof PsiField) { final PsiField field = (PsiField) element; final PsiType type = field.getType(); if (field.hasModifierProperty(PsiModifier.FINAL) && type.getArrayDimensions() == 0) { if (field.hasModifierProperty(PsiModifier.STATIC)) { return true; } final PsiExpression qualifier = referenceExpression.getQualifierExpression(); if (qualifier == null) { return true; } else if (checkCondition(qualifier, context, notUpdated)) { return true; } } } else if (element instanceof PsiVariable) { final PsiVariable variable = (PsiVariable) element; if (variable.hasModifierProperty(PsiModifier.FINAL)) { // final variables cannot be updated, don't bother to // flag them return true; } else if (element instanceof PsiLocalVariable || element instanceof PsiParameter) { if (!VariableAccessUtils.variableIsAssigned(variable, context)) { notUpdated.add(referenceExpression); return true; } } } } else if (condition instanceof PsiPrefixExpression) { final PsiPrefixExpression prefixExpression = (PsiPrefixExpression) condition; final IElementType tokenType = prefixExpression.getOperationTokenType(); if (JavaTokenType.EXCL.equals(tokenType) || JavaTokenType.PLUS.equals(tokenType) || JavaTokenType.MINUS.equals(tokenType)) { final PsiExpression operand = prefixExpression.getOperand(); return checkCondition(operand, context, notUpdated); } } else if (condition instanceof PsiArrayAccessExpression) { // Actually the contents of the array could change nevertheless // if it is accessed through a different reference like this: // int[] local_ints = new int[]{1, 2}; // int[] other_ints = local_ints; // while (local_ints[0] > 0) { other_ints[0]--; } // // Keep this check? final PsiArrayAccessExpression accessExpression = (PsiArrayAccessExpression) condition; final PsiExpression indexExpression = accessExpression.getIndexExpression(); return checkCondition(indexExpression, context, notUpdated) && checkCondition(accessExpression.getArrayExpression(), context, notUpdated); } else if (condition instanceof PsiConditionalExpression) { final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression) condition; final PsiExpression thenExpression = conditionalExpression.getThenExpression(); final PsiExpression elseExpression = conditionalExpression.getElseExpression(); if (thenExpression == null || elseExpression == null) { return false; } return checkCondition(conditionalExpression.getCondition(), context, notUpdated) && checkCondition(thenExpression, context, notUpdated) && checkCondition(elseExpression, context, notUpdated); } else if (condition instanceof PsiThisExpression) { return true; } else if (condition instanceof PsiMethodCallExpression && !ignoreIterators) { final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) condition; if (!IteratorUtils.isCallToHasNext(methodCallExpression)) { return false; } final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifierExpression = methodExpression.getQualifierExpression(); if (qualifierExpression instanceof PsiReferenceExpression) { final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) qualifierExpression; final PsiElement element = referenceExpression.resolve(); if (!(element instanceof PsiVariable)) { return false; } final PsiVariable variable = (PsiVariable) element; if (!IteratorUtils.containsCallToScannerNext(context, variable, true)) { notUpdated.add(qualifierExpression); return true; } } else { if (!IteratorUtils.containsCallToScannerNext(context, null, true)) { notUpdated.add(methodCallExpression); return true; } } } return false; }
private static String[] getFieldNames(final RadComponent component, final String currentName) { final ArrayList<String> result = new ArrayList<String>(); if (currentName != null) { result.add(currentName); } final IRootContainer root = FormEditingUtil.getRoot(component); final String className = root.getClassToBind(); if (className == null) { return ArrayUtil.toStringArray(result); } final PsiClass aClass = FormEditingUtil.findClassToBind(component.getModule(), className); if (aClass == null) { return ArrayUtil.toStringArray(result); } final PsiField[] fields = aClass.getFields(); for (final PsiField field : fields) { if (field.hasModifierProperty(PsiModifier.STATIC)) { continue; } final String fieldName = field.getName(); if (Comparing.equal(currentName, fieldName)) { continue; } if (!FormEditingUtil.isBindingUnique(component, fieldName, root)) { continue; } final String componentClassName; if (component instanceof RadErrorComponent) { componentClassName = component.getComponentClassName(); } else if (component instanceof RadHSpacer || component instanceof RadVSpacer) { componentClassName = Spacer.class.getName(); } else { componentClassName = component.getComponentClass().getName(); } final PsiType componentType; try { componentType = JavaPsiFacade.getInstance(component.getProject()) .getElementFactory() .createTypeFromText(componentClassName, null); } catch (IncorrectOperationException e) { continue; } final PsiType fieldType = field.getType(); if (!fieldType.isAssignableFrom(componentType)) { continue; } result.add(fieldName); } String text = FormInspectionUtil.getText(component.getModule(), component); if (text != null) { String binding = BindingProperty.suggestBindingFromText(component, text); if (binding != null && !result.contains(binding)) { result.add(binding); } } final String[] names = ArrayUtil.toStringArray(result); Arrays.sort(names); return names; }