@Nullable public static PsiType getWritablePropertyType(@Nullable PsiClass containingClass, @Nullable PsiElement declaration) { if (declaration instanceof PsiField) { return getWrappedPropertyType((PsiField) declaration, JavaFxCommonNames.ourWritableMap); } if (declaration instanceof PsiMethod) { final PsiMethod method = (PsiMethod) declaration; if (method.getParameterList().getParametersCount() != 0) { return getSetterArgumentType(method); } final String propertyName = PropertyUtil.getPropertyName(method); final PsiClass psiClass = containingClass != null ? containingClass : method.getContainingClass(); if (propertyName != null && containingClass != null) { final PsiMethod setter = findInstancePropertySetter(psiClass, propertyName); if (setter != null) { final PsiType setterArgumentType = getSetterArgumentType(setter); if (setterArgumentType != null) return setterArgumentType; } } return getGetterReturnType(method); } return null; }
@Override public Access getExpressionAccess(final PsiElement expression) { if (!(expression instanceof PsiExpression)) return Access.Read; PsiExpression expr = (PsiExpression) expression; boolean readAccess = PsiUtil.isAccessedForReading(expr); boolean writeAccess = PsiUtil.isAccessedForWriting(expr); if (!writeAccess && expr instanceof PsiReferenceExpression) { //when searching usages of fields, should show all found setters as a "only write usage" PsiElement actualReferee = ((PsiReferenceExpression) expr).resolve(); if (actualReferee instanceof PsiMethod && PropertyUtil.isSimplePropertySetter((PsiMethod)actualReferee)) { writeAccess = true; readAccess = false; } } if (writeAccess && readAccess) return Access.ReadWrite; return writeAccess ? Access.Write : Access.Read; }
public PsiMember fromString(final String s, final ConvertContext context) { if (s == null) return null; final PsiClass psiClass = getTargetClass(context); if (psiClass == null) return null; for (PropertyMemberType type : getMemberTypes(context)) { switch (type) { case FIELD: final PsiField field = psiClass.findFieldByName(s, isLookDeep()); if (field != null) return field; break; case GETTER: final PsiMethod getter = PropertyUtil.findPropertyGetter(psiClass, getPropertyName(s, context), false, isLookDeep()); if (getter != null) return getter; break; case SETTER: final PsiMethod setter = PropertyUtil.findPropertySetter(psiClass, getPropertyName(s, context), false, isLookDeep()); if (setter != null) return setter; break; } } return null; }
@Nullable private static PsiModifierListOwner getAccessedVariableOrGetter(final PsiElement target) { if (target instanceof PsiVariable) { return (PsiVariable)target; } if (target instanceof PsiMethod) { PsiMethod method = (PsiMethod)target; if (PropertyUtil.isSimplePropertyGetter(method) && !(method.getReturnType() instanceof PsiPrimitiveType)) { String qName = PsiUtil.getMemberQualifiedName(method); if (qName == null || !FALSE_GETTERS.value(qName)) { return method; } } } return null; }
public static boolean inferPurity(@NotNull final PsiMethod method) { if (!InferenceFromSourceUtil.shouldInferFromSource(method) || method.getReturnType() == PsiType.VOID || method.getBody() == null || method.isConstructor() || PropertyUtil.isSimpleGetter(method)) { return false; } return CachedValuesManager.getCachedValue(method, new CachedValueProvider<Boolean>() { @Nullable @Override public Result<Boolean> compute() { boolean pure = RecursionManager.doPreventingRecursion(method, true, new Computable<Boolean>() { @Override public Boolean compute() { return doInferPurity(method); } }) == Boolean.TRUE; return Result.create(pure, method); } }); }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { if (!myField.isValid()) return false; final PsiClass aClass = myField.getContainingClass(); if (aClass == null) { return false; } if (myCreateGetter){ if (isStaticFinal(myField) || PropertyUtil.findPropertyGetter(aClass, myPropertyName, isStatic(myField), false) != null){ return false; } } if (myCreateSetter){ if(isFinal(myField) || PropertyUtil.findPropertySetter(aClass, myPropertyName, isStatic(myField), false) != null){ return false; } } return true; }
private void visitMethodReturnType(final PsiMethod scopeMethod, PsiType type, boolean tailTypeSemicolon) { if (type != null) { NullableComputable<String> expectedName; if (PropertyUtil.isSimplePropertyAccessor(scopeMethod)) { expectedName = new NullableComputable<String>() { @Override public String compute() { return PropertyUtil.getPropertyName(scopeMethod); } }; } else { expectedName = ExpectedTypeInfoImpl.NULL; } myResult.add(createInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, type, tailTypeSemicolon ? TailType.SEMICOLON : TailType.NONE, null, expectedName)); } }
protected PsiElement createGetter(final boolean createField) throws IncorrectOperationException { final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(myPsiClass.getProject()).getElementFactory(); final String methodName = PropertyUtil.suggestGetterName(myPropertyName, myType); final String typeName = myType.getCanonicalText(); @NonNls final String text; boolean isInterface = myPsiClass.isInterface(); if (createField) { final String fieldName = getFieldName(); text = "public " + typeName + " " + methodName + "() { return " + fieldName + "; }"; } else { if (isInterface) { text = typeName + " " + methodName + "();"; } else { text = "public " + typeName + " " + methodName + "() { return null; }"; } } final PsiMethod method = elementFactory.createMethodFromText(text, null); final PsiMethod psiElement = (PsiMethod)myPsiClass.add(method); if (!createField && !isInterface) { CreateFromUsageUtils.setupMethodBody(psiElement); } return psiElement; }
private void applyNewSetterPrefix() { final String setterPrefix = Messages.showInputDialog(myTable, "New setter prefix:", "Rename Setters Prefix", null, mySetterPrefix, new MySetterPrefixInputValidator()); if (setterPrefix != null) { mySetterPrefix = setterPrefix; PropertiesComponent.getInstance(myProject).setValue(SETTER_PREFIX_KEY, setterPrefix); final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(myProject); for (String paramName : myParametersMap.keySet()) { final ParameterData data = myParametersMap.get(paramName); paramName = data.getParamName(); final String propertyName = javaCodeStyleManager.variableNameToPropertyName(paramName, VariableKind.PARAMETER); data.setSetterName(PropertyUtil.suggestSetterName(propertyName, setterPrefix)); } myTable.revalidate(); myTable.repaint(); } }
private static void addSettersAndListeners(CompletionResultSet result, PsiClass containingClass) { // see PyJavaType.init() in Jython source code for matching logic for (PsiMethod method : containingClass.getAllMethods()) { final Project project = containingClass.getProject(); if (PropertyUtil.isSimplePropertySetter(method)) { final String propName = PropertyUtil.getPropertyName(method); result.addElement(PyUtil.createNamedParameterLookup(propName, project)); } else if (method.getName().startsWith("add") && method.getName().endsWith("Listener") && PsiType.VOID.equals(method.getReturnType())) { final PsiParameter[] parameters = method.getParameterList().getParameters(); if (parameters.length == 1) { final PsiType type = parameters[0].getType(); if (type instanceof PsiClassType) { final PsiClass parameterClass = ((PsiClassType)type).resolve(); if (parameterClass != null) { result.addElement(PyUtil.createNamedParameterLookup(StringUtil.decapitalize(parameterClass.getName()), project)); for (PsiMethod parameterMethod : parameterClass.getMethods()) { result.addElement(PyUtil.createNamedParameterLookup(parameterMethod.getName(), project)); } } } } } } }
@SuppressWarnings("UnusedDeclaration") public static List<String> findProperties(@Nullable PsiClass input) { if (input == null) { return Collections.emptyList(); } final List<String> result = new ArrayList<String>(); input.accept(new JavaRecursiveElementVisitor() { @Override public void visitMethod(PsiMethod method) { super.visitMethod(method); if (method.getName().startsWith("get")) { result.add(PropertyUtil.getPropertyName(method)); } } }); return result; }
protected void checkComponentProperties(Module module, final IComponent component, final FormErrorCollector collector) { final GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module); final PsiManager psiManager = PsiManager.getInstance(module.getProject()); final PsiClass aClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(component.getComponentClassName(), scope); if (aClass == null) { return; } for(final IProperty prop: component.getModifiedProperties()) { final PsiMethod getter = PropertyUtil.findPropertyGetter(aClass, prop.getName(), false, true); if (getter == null) continue; final LanguageLevel languageLevel = LanguageLevelUtil.getEffectiveLanguageLevel(module); if (Java15APIUsageInspection.isForbiddenApiUsage(getter, languageLevel)) { registerError(component, collector, prop, "@since " + Java15APIUsageInspection.getShortName(languageLevel)); } } }
public boolean isPropertyDeprecated(final Module module, final Class aClass, final String propertyName) { // TODO[yole]: correct module-dependent caching Set<String> deprecated = myClass2DeprecatedProperties.get(aClass.getName()); if (deprecated == null) { deprecated = new HashSet<String>(); PsiClass componentClass = JavaPsiFacade.getInstance(module.getProject()).findClass(aClass.getName(), module.getModuleWithDependenciesAndLibrariesScope(true)); if (componentClass != null) { PsiMethod[] methods = componentClass.getAllMethods(); for(PsiMethod method: methods) { if (method.isDeprecated() && PropertyUtil.isSimplePropertySetter(method)) { deprecated.add(PropertyUtil.getPropertyNameBySetter(method)); } } } myClass2DeprecatedProperties.put(aClass.getName(), deprecated); } return deprecated.contains(propertyName); }
private static boolean isSetterNonNls(final Project project, final GlobalSearchScope searchScope, final String componentClassName, final String propertyName) { PsiClass componentClass = JavaPsiFacade.getInstance(project).findClass(componentClassName, searchScope); if (componentClass == null) { return false; } PsiMethod setter = PropertyUtil.findPropertySetter(componentClass, propertyName, false, true); if (setter != null) { PsiParameter[] parameters = setter.getParameterList().getParameters(); if (parameters.length == 1 && "java.lang.String".equals(parameters[0].getType().getCanonicalText()) && AnnotationUtil.isAnnotated(parameters [0], AnnotationUtil.NON_NLS, false, true)) { return true; } } return false; }
@Override public void visitMethod(@NotNull PsiMethod method) { //no drilldown if (method.getNameIdentifier() == null) { return; } if (!method.hasModifierProperty(PsiModifier.PUBLIC)) { return; } final PsiCodeBlock body = method.getBody(); if (body == null) { return; } if (method.isConstructor()) { return; } if (PropertyUtil.isSimpleGetter(method) || PropertyUtil.isSimpleSetter(method)) { return; } if (containsLoggingCall(body)) { return; } registerMethodError(method); }
private int calculateTotalMethodCount(PsiClass aClass) { final PsiMethod[] methods = aClass.getMethods(); int totalCount = 0; for (final PsiMethod method : methods) { if (method.isConstructor()) { continue; } if (ignoreGettersAndSetters) { if (PropertyUtil.isSimpleGetter(method) || PropertyUtil.isSimpleSetter(method)) { continue; } } if (ignoreOverridingMethods) { if (MethodUtils.hasSuper(method)) { continue; } } totalCount++; } return totalCount; }
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { final PsiElement resolved = resolve(); if (resolved instanceof PsiMethod) { final PsiMethod method = (PsiMethod) resolved; final String oldName = getElement().getName(); if (!method.getName().equals(oldName)) { //was property reference to accessor if (PropertyUtil.isSimplePropertySetter(method)) { final String newPropertyName = PropertyUtil.getPropertyName(newElementName); if (newPropertyName != null) { newElementName = newPropertyName; } } } } return super.handleElementRename(newElementName); }
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { final PsiElement resolved = resolve(); if (resolved instanceof PsiMethod) { final PsiMethod method = (PsiMethod) resolved; final String oldName = getReferenceName(); if (!method.getName().equals(oldName)) { //was property reference to accessor if (PropertyUtil.isSimplePropertyAccessor(method)) { final String newPropertyName = PropertyUtil.getPropertyName(newElementName); if (newPropertyName != null) { return super.handleElementRename(newPropertyName); } } } } return super.handleElementRename(newElementName); }
/** * Create a new {@link FieldElement} object. * * @param field the {@link com.intellij.psi.PsiField} to get the information from. * @return a new {@link FieldElement} object. */ public static FieldElement newFieldElement(PsiField field, boolean useAccessor) { FieldElement fe = new FieldElement(); fe.setName(field.getName()); final PsiMethod getterForField = useAccessor ? PropertyUtil.findGetterForField(field) : null; fe.setAccessor(getterForField != null ? getterForField.getName() + "()" : field.getName()); if (PsiAdapter.isConstantField(field)) fe.setConstant(true); if (PsiAdapter.isEnumField(field)) fe.setEnum(true); PsiModifierList modifiers = field.getModifierList(); if (modifiers != null) { if (modifiers.hasModifierProperty(PsiModifier.TRANSIENT)) fe.setModifierTransient(true); if (modifiers.hasModifierProperty(PsiModifier.VOLATILE)) fe.setModifierVolatile(true); } PsiElementFactory factory = JavaPsiFacade.getInstance(field.getProject()).getElementFactory(); PsiType type = field.getType(); setElementInfo(fe, factory, type, modifiers); return fe; }
@Override public PsiMethod[] generateGetters(PsiField field) { final Project project = field.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiMethod getter = GenerateMembersUtil.generateSimpleGetterPrototype(field); final PsiType wrappedType = JavaFxPsiUtil.getWrappedPropertyType(field, project, JavaFxCommonClassNames.ourReadOnlyMap); final PsiTypeElement returnTypeElement = getter.getReturnTypeElement(); LOG.assertTrue(returnTypeElement != null); returnTypeElement.replace(factory.createTypeElement(wrappedType)); final PsiCodeBlock getterBody = getter.getBody(); LOG.assertTrue(getterBody != null); getterBody.getStatements()[0].replace(factory.createStatementFromText("return " + field.getName() + ".get();", field)); final PsiMethod propertyGetter = PropertyUtil.generateGetterPrototype(field); propertyGetter.setName(JavaCodeStyleManager.getInstance(project).variableNameToPropertyName(field.getName(), VariableKind.FIELD) + "Property"); return new PsiMethod[] {getter, GenerateMembersUtil.annotateOnOverrideImplement(field.getContainingClass(), propertyGetter)}; }
/** * Identifies the list of properties on the supplied class and optionally its superclasses. * Properties are located through their setters, ensuring that all properties are writable. * Located properties may or may not also have getters. * * @param owner the class to be scanned for properties * @param includeSuperclasses whether to include properties declared in superclasses * @return the list of qualifying properties */ @NotNull public static List<Property> locatePropertiesFromSetters(@NotNull PsiClass owner, boolean includeSuperclasses) { Map<String, PsiMethod> propertyMap = PropertyUtil.getAllProperties(owner, true, // Do accept setters false, // Don't accept getters includeSuperclasses); @SuppressWarnings("ConstantConditions") List<Property> matchedProperties = propertyMap .entrySet() .stream() .sorted(Map.Entry.comparingByKey()) .map(e -> new Property(e.getKey(), PropertyUtil.getPropertyType(e.getValue()), // known to be non-null PropertyUtil.findPropertyGetter(owner, e.getKey(), false, includeSuperclasses), e.getValue())) .collect(toList()); return matchedProperties; }
/** * Identifies the list of properties on the supplied class and optionally its superclasses. * Properties are located through their getters, ensuring that all properties are readable. * Located properties may or may not also have setters. * * @param owner the class to be scanned for properties * @param includeSuperclasses whether to include properties declared in superclasses * @return the list of qualifying properties */ @NotNull public static List<Property> locatePropertiesFromGetters(@NotNull PsiClass owner, boolean includeSuperclasses) { Map<String, PsiMethod> propertyMap = PropertyUtil.getAllProperties(owner, false, // Don't accept setters true, // Do accept getters includeSuperclasses); @SuppressWarnings("ConstantConditions") List<Property> matchedProperties = propertyMap .entrySet() .stream() .sorted(Map.Entry.comparingByKey()) .map(e -> new Property(e.getKey(), PropertyUtil.getPropertyType(e.getValue()), // known to be non-null e.getValue(), PropertyUtil.findPropertySetter(owner, e.getKey(), false, includeSuperclasses))) .collect(toList()); return matchedProperties; }
private static boolean isSimplePropertyAccessor(PsiMethod method) { PsiCodeBlock body = method.getBody(); if (body == null) return false; PsiStatement[] statements = body.getStatements(); if (statements.length == 0) return false; PsiStatement statement = statements[0]; if (PropertyUtil.isSimplePropertyGetter(method)) { if (statement instanceof PsiReturnStatement) { return ((PsiReturnStatement)statement).getReturnValue() instanceof PsiReferenceExpression; } } else if (PropertyUtil.isSimplePropertySetter(method)) { if (statements.length > 1 && !(statements[1] instanceof PsiReturnStatement)) return false; if (statement instanceof PsiExpressionStatement) { PsiExpression expr = ((PsiExpressionStatement)statement).getExpression(); if (expr instanceof PsiAssignmentExpression) { PsiExpression lhs = ((PsiAssignmentExpression)expr).getLExpression(); PsiExpression rhs = ((PsiAssignmentExpression)expr).getRExpression(); return lhs instanceof PsiReferenceExpression && rhs instanceof PsiReferenceExpression && !((PsiReferenceExpression)rhs).isQualified(); } } } return false; }
public static void registerQuickFix(PsiMember refElement, PsiJavaCodeReferenceElement place, PsiClass accessObjectClass, HighlightInfo error) { if (refElement instanceof PsiField && place instanceof PsiReferenceExpression) { final PsiField psiField = (PsiField)refElement; final PsiClass containingClass = psiField.getContainingClass(); if (containingClass != null) { if (PsiUtil.isOnAssignmentLeftHand((PsiExpression)place)) { final PsiMethod setterPrototype = PropertyUtil.generateSetterPrototype(psiField); final PsiMethod setter = containingClass.findMethodBySignature(setterPrototype, true); if (setter != null && PsiUtil.isAccessible(setter, place, accessObjectClass)) { QuickFixAction.registerQuickFixAction(error, new ReplaceInaccessibleFieldWithGetterSetterFix(place, setter, true)); } } else if (PsiUtil.isAccessedForReading((PsiExpression)place)) { final PsiMethod getterPrototype = GenerateMembersUtil.generateGetterPrototype(psiField); final PsiMethod getter = containingClass.findMethodBySignature(getterPrototype, true); if (getter != null && PsiUtil.isAccessible(getter, place, accessObjectClass)) { QuickFixAction.registerQuickFixAction(error, new ReplaceInaccessibleFieldWithGetterSetterFix(place, getter, false)); } } } } }
private void visitMethodReturnType(final PsiMethod scopeMethod, PsiType type, boolean tailTypeSemicolon) { if (type != null) { ExpectedTypeInfoImpl info = createInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, type, tailTypeSemicolon ? TailType.SEMICOLON : TailType.NONE); if (PropertyUtil.isSimplePropertyAccessor(scopeMethod)) { info.expectedName = new NullableComputable<String>() { @Override public String compute() { return PropertyUtil.getPropertyName(scopeMethod); } }; } myResult = new ExpectedTypeInfo[]{info}; } else { myResult = ExpectedTypeInfo.EMPTY_ARRAY; } }
public static boolean anyFieldsWithGettersPresent(List<UsageInfo> classMemberRefs) { for (UsageInfo usageInfo : classMemberRefs) { if (usageInfo.getElement() instanceof PsiReferenceExpression) { PsiElement e = ((PsiReferenceExpression)usageInfo.getElement()).resolve(); if (e instanceof PsiField) { PsiField psiField = (PsiField)e; PsiMethod getterPrototype = PropertyUtil.generateGetterPrototype(psiField); PsiMethod getter = psiField.getContainingClass().findMethodBySignature(getterPrototype, true); if (getter != null) return true; } } } return false; }
@Override public void visitField(@NotNull PsiField field) { final Project project = field.getProject(); final String propertyName = PropertyUtil.suggestPropertyName(project, field); final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC); final PsiClass containingClass = field.getContainingClass(); final PsiMethod setter = PropertyUtil.findPropertySetter(containingClass, propertyName, isStatic, false); if (setter == null) { return; } final PsiMethod getter = PropertyUtil.findPropertyGetter(containingClass, propertyName, isStatic, false); if (getter != null) { return; } registerFieldError(field); }
private int calculateTotalMethodCount(PsiClass aClass) { final PsiMethod[] methods = aClass.getMethods(); int totalCount = 0; for (final PsiMethod method : methods) { if (method.isConstructor()) { continue; } if (ignoreGettersAndSetters) { if (PropertyUtil.isSimpleGetter(method) || PropertyUtil.isSimpleSetter(method)) { continue; } } totalCount++; } return totalCount; }
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException { final PsiElement resolved = resolve(); if (resolved instanceof PsiMethod) { final PsiMethod method = (PsiMethod) resolved; final String oldName = getReferenceName(); if (!method.getName().equals(oldName)) { //was property reference to accessor if (PropertyUtil.isSimplePropertyAccessor(method)) { final String newPropertyName = PropertyUtil.getPropertyName(newElementName); if (newPropertyName != null) { return super.handleElementRename(newPropertyName); } } } } return super.handleElementRename(newElementName); }
@Override public PsiMethod[] generateGetters(PsiField field) { final Project project = field.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiMethod getter = PropertyUtil.generateGetterPrototype(field); final PsiType wrappedType = JavaFxPsiUtil.getWrappedPropertyType(field, project, JavaFxCommonClassNames.ourReadOnlyMap); final PsiTypeElement returnTypeElement = getter.getReturnTypeElement(); LOG.assertTrue(returnTypeElement != null); returnTypeElement.replace(factory.createTypeElement(wrappedType)); final PsiCodeBlock getterBody = getter.getBody(); LOG.assertTrue(getterBody != null); getterBody.getStatements()[0].replace(factory.createStatementFromText("return " + field.getName() + ".get();", field)); final PsiMethod propertyGetter = PropertyUtil.generateGetterPrototype(field); propertyGetter.setName(JavaCodeStyleManager.getInstance(project).variableNameToPropertyName(field.getName(), VariableKind.FIELD) + "Property"); return new PsiMethod[] {getter, propertyGetter}; }
@Override public PsiMethod[] generateSetters(PsiField field) { final PsiMethod setter = PropertyUtil.generateSetterPrototype(field); final Project project = field.getProject(); final PsiType wrappedType = JavaFxPsiUtil.getWrappedPropertyType(field, project, JavaFxCommonClassNames.ourWritableMap); final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project); final PsiTypeElement newTypeElement = elementFactory.createTypeElement(wrappedType); final PsiParameter[] parameters = setter.getParameterList().getParameters(); LOG.assertTrue(parameters.length == 1); final PsiParameter parameter = parameters[0]; final PsiTypeElement typeElement = parameter.getTypeElement(); LOG.assertTrue(typeElement != null); typeElement.replace(newTypeElement); final PsiCodeBlock body = setter.getBody(); LOG.assertTrue(body != null); body.getStatements()[0].replace(elementFactory.createStatementFromText("this." + field.getName() + ".set(" + parameter.getName() + ");", field)); return new PsiMethod[] {setter}; }