@Override public LightRef asLightUsage(@NotNull PsiElement element, @NotNull ByteArrayEnumerator names) { if (mayBeVisibleOutsideOwnerFile(element)) { if (element instanceof PsiField) { final PsiField field = (PsiField) element; final PsiClass aClass = field.getContainingClass(); if (aClass == null || aClass instanceof PsiAnonymousClass) return null; final String jvmOwnerName = ClassUtil.getJVMClassName(aClass); final String name = field.getName(); if (name == null || jvmOwnerName == null) return null; return new LightRef.JavaLightFieldRef(id(jvmOwnerName, names), id(name, names)); } else if (element instanceof PsiMethod) { final PsiClass aClass = ((PsiMethod) element).getContainingClass(); if (aClass == null || aClass instanceof PsiAnonymousClass) return null; final String jvmOwnerName = ClassUtil.getJVMClassName(aClass); if (jvmOwnerName == null) return null; final PsiMethod method = (PsiMethod) element; final String name = method.isConstructor() ? "<init>" : method.getName(); final int parametersCount = method.getParameterList().getParametersCount(); return new LightRef.JavaLightMethodRef( id(jvmOwnerName, names), id(name, names), parametersCount); } else if (element instanceof PsiClass) { final String jvmClassName = ClassUtil.getJVMClassName((PsiClass) element); if (jvmClassName != null) { return new LightRef.JavaLightClassRef(id(jvmClassName, names)); } } } return null; }
private NecessaryAccessorsVisitor checkNecessaryGettersSetters4SourceClass() { final NecessaryAccessorsVisitor visitor = new NecessaryAccessorsVisitor() { @Override protected boolean hasGetterOrSetter(PsiMethod[] getters) { for (PsiMethod getter : getters) { if (!isInMovedElement(getter)) return true; } return false; } @Override protected boolean isProhibitedReference(PsiField field) { if (fields.contains(field)) { return false; } if (innerClasses.contains(field.getContainingClass())) { return false; } return true; } }; for (PsiField field : fields) { field.accept(visitor); } for (PsiMethod method : methods) { method.accept(visitor); } for (PsiClass innerClass : innerClasses) { innerClass.accept(visitor); } return visitor; }
protected void generatePsiElements( @NotNull PsiField psiField, @NotNull PsiAnnotation psiAnnotation, @NotNull List<? super PsiElement> target) { final String fieldName = psiField.getName(); final PsiType psiFieldType = psiField.getType(); final String methodName = getFindByName(psiField); PsiClass psiClass = psiField.getContainingClass(); assert psiClass != null; UserMapKeys.addWriteUsageFor(psiField); LombokLightMethodBuilder method = new LombokLightMethodBuilder(psiField.getManager(), methodName) .withMethodReturnType(PsiTypesUtil.getClassType(psiClass)) .withContainingClass(psiClass) .withParameter(fieldName, psiFieldType) .withNavigationElement(psiField); method.withModifier(PsiModifier.STATIC); method.withModifier(PsiModifier.PUBLIC); target.add(method); }
private static boolean processField( @NotNull GrTypeDefinition grType, @NotNull PsiScopeProcessor processor, @NotNull ResolveState state, @NotNull PsiElement place, boolean processInstanceMethods, @NotNull PsiSubstitutor substitutor, @NotNull PsiElementFactory factory, @NotNull LanguageLevel level, CandidateInfo fieldInfo) { final PsiField field = (PsiField) fieldInfo.getElement(); if (!processInstanceMember(processInstanceMethods, field) || isSameDeclaration(place, field)) { return true; } LOG.assertTrue(field.getContainingClass() != null); final PsiSubstitutor finalSubstitutor = PsiClassImplUtil.obtainFinalSubstitutor( field.getContainingClass(), fieldInfo.getSubstitutor(), grType, substitutor, factory, level); return processor.execute(field, state.put(PsiSubstitutor.KEY, finalSubstitutor)); }
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 = PropertyUtil.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 static void checkCodeBlock( final PsiCodeBlock body, final Set<PsiField> candidates, Set<PsiField> usedFields) { try { final ControlFlow controlFlow = ControlFlowFactory.getInstance(body.getProject()) .getControlFlow(body, AllVariablesControlFlowPolicy.getInstance()); final List<PsiVariable> usedVars = ControlFlowUtil.getUsedVariables(controlFlow, 0, controlFlow.getSize()); for (PsiVariable usedVariable : usedVars) { if (usedVariable instanceof PsiField) { final PsiField usedField = (PsiField) usedVariable; if (!usedFields.add(usedField)) { candidates.remove(usedField); // used in more than one code block } } } final List<PsiReferenceExpression> readBeforeWrites = ControlFlowUtil.getReadBeforeWrite(controlFlow); for (final PsiReferenceExpression readBeforeWrite : readBeforeWrites) { final PsiElement resolved = readBeforeWrite.resolve(); if (resolved instanceof PsiField) { final PsiField field = (PsiField) resolved; PsiElement parent = body.getParent(); if (!(parent instanceof PsiMethod) || !((PsiMethod) parent).isConstructor() || field.getInitializer() == null || field.hasModifierProperty(PsiModifier.STATIC)) { candidates.remove(field); } } } } catch (AnalysisCanceledException e) { candidates.clear(); } }
@NonNls private static String computeConstantBooleanText(PsiExpression expression) { if (!(expression instanceof PsiReferenceExpression)) { return null; } final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) expression; final PsiElement target = referenceExpression.resolve(); if (!(target instanceof PsiField)) { return null; } final PsiField field = (PsiField) target; final PsiClass containingClass = field.getContainingClass(); if (containingClass == null) { return null; } final String qualifiedName = containingClass.getQualifiedName(); if (!CommonClassNames.JAVA_LANG_BOOLEAN.equals(qualifiedName)) { return null; } @NonNls final String name = field.getName(); if ("TRUE".equals(name)) { return "true"; } else if ("FALSE".equals(name)) { return "false"; } else { return null; } }
private static PsiField createField( PsiLocalVariable local, PsiType forcedType, String fieldName, boolean includeInitializer) { @NonNls StringBuilder pattern = new StringBuilder(); pattern.append("private int "); pattern.append(fieldName); if (local.getInitializer() == null) { includeInitializer = false; } if (includeInitializer) { pattern.append("=0"); } pattern.append(";"); final Project project = local.getProject(); PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory(); try { PsiField field = factory.createFieldFromText(pattern.toString(), null); field.getTypeElement().replace(factory.createTypeElement(forcedType)); if (includeInitializer) { PsiExpression initializer = RefactoringUtil.convertInitializerToNormalExpression( local.getInitializer(), forcedType); field.getInitializer().replace(initializer); } for (PsiAnnotation annotation : local.getModifierList().getAnnotations()) { field.getModifierList().add(annotation.copy()); } return field; } catch (IncorrectOperationException e) { LOG.error(e); return null; } }
@NotNull private static PsiElement[] extractReferencedVariables(@NotNull PsiTypeElement typeElement) { final PsiElement parent = typeElement.getParent(); if (parent instanceof PsiVariable) { if (parent instanceof PsiField) { PsiField aField = (PsiField) parent; List<PsiField> fields = new ArrayList<>(); while (true) { fields.add(aField); aField = PsiTreeUtil.getNextSiblingOfType(aField, PsiField.class); if (aField == null || aField.getTypeElement() != typeElement) { return fields.toArray(new PsiElement[fields.size()]); } } } else if (parent instanceof PsiLocalVariable) { final PsiDeclarationStatement declaration = PsiTreeUtil.getParentOfType(parent, PsiDeclarationStatement.class); if (declaration != null) { return Arrays.stream(declaration.getDeclaredElements()) .filter(PsiVariable.class::isInstance) .toArray(PsiVariable[]::new); } } return new PsiElement[] {parent}; } else { return PsiElement.EMPTY_ARRAY; } }
public void buildReferences() { PsiClass psiClass = getElement(); if (psiClass != null) { for (PsiClassInitializer classInitializer : psiClass.getInitializers()) { RefJavaUtil.getInstance().addReferences(psiClass, this, classInitializer.getBody()); } RefJavaUtil.getInstance().addReferences(psiClass, this, psiClass.getModifierList()); PsiField[] psiFields = psiClass.getFields(); for (PsiField psiField : psiFields) { getRefManager().getReference(psiField); final PsiExpression initializer = psiField.getInitializer(); if (initializer != null) { RefJavaUtil.getInstance().addReferences(psiClass, this, initializer); } } PsiMethod[] psiMethods = psiClass.getMethods(); for (PsiMethod psiMethod : psiMethods) { getRefManager().getReference(psiMethod); } getRefManager().fireBuildReferences(this); } }
private static boolean doCreate( Project project, Editor editor, PsiParameter[] parameters, SmartPsiElementPointer constructorPointer, ParameterInfoImpl[] parameterInfos, HashMap<PsiField, String> fields) { PsiMethod constructor = (PsiMethod) constructorPointer.getElement(); assert constructor != null; PsiParameter[] newParameters = constructor.getParameterList().getParameters(); if (newParameters == parameters) return false; // user must have canceled dialog boolean created = false; // do not introduce assignment in chanined constructor if (JavaHighlightUtil.getChainedConstructors(constructor) == null) { for (PsiField field : fields.keySet()) { final String defaultParamName = fields.get(field); PsiParameter parameter = findParamByName(defaultParamName, field.getType(), newParameters, parameterInfos); if (parameter == null) { continue; } notNull(project, field, parameter); AssignFieldFromParameterAction.addFieldAssignmentStatement( project, field, parameter, editor); created = true; } } return created; }
public VariableLookupItem(PsiField field, boolean shouldImport) { super(field, field.getName()); myHelper = new MemberLookupHelper(field, field.getContainingClass(), shouldImport, false); if (!shouldImport) { forceQualify(); } }
public FieldConflictsResolver(String name, PsiCodeBlock scope) { myScope = scope; if (myScope == null) { myField = null; myReferenceExpressions = null; return; } JavaPsiFacade facade = JavaPsiFacade.getInstance(myScope.getProject()); final PsiVariable oldVariable = facade.getResolveHelper().resolveAccessibleReferencedVariable(name, myScope); myField = oldVariable instanceof PsiField ? (PsiField) oldVariable : null; if (!(oldVariable instanceof PsiField)) { myReferenceExpressions = null; return; } myReferenceExpressions = new ArrayList<PsiReferenceExpression>(); for (PsiReference reference : ReferencesSearch.search(myField, new LocalSearchScope(myScope), false)) { final PsiElement element = reference.getElement(); if (element instanceof PsiReferenceExpression) { final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) element; if (referenceExpression.getQualifierExpression() == null) { myReferenceExpressions.add(referenceExpression); } } } if (myField.hasModifierProperty(PsiModifier.STATIC)) { myQualifyingClass = myField.getContainingClass(); } }
@NotNull public String elementToKotlin(@NotNull PsiElement element) { if (element instanceof PsiJavaFile) { return fileToFile((PsiJavaFile) element).toKotlin(); } if (element instanceof PsiClass) { return classToClass((PsiClass) element).toKotlin(); } if (element instanceof PsiMethod) { return methodToFunction((PsiMethod) element).toKotlin(); } if (element instanceof PsiField) { PsiField field = (PsiField) element; return fieldToField(field, field.getContainingClass()).toKotlin(); } if (element instanceof PsiStatement) { return statementToStatement((PsiStatement) element).toKotlin(); } if (element instanceof PsiExpression) { return expressionToExpression((PsiExpression) element).toKotlin(); } return ""; }
public SNode convertField(PsiField x, SNode parentConcept) { SNode field; if (isStatic(x) || SConceptOperations.isSubConceptOf( parentConcept, "jetbrains.mps.baseLanguage.structure.Interface")) { field = SConceptOperations.createNewNode( "jetbrains.mps.baseLanguage.structure.StaticFieldDeclaration", null); } else { field = SConceptOperations.createNewNode( "jetbrains.mps.baseLanguage.structure.FieldDeclaration", null); SPropertyOperations.set( SNodeOperations.cast(field, "jetbrains.mps.baseLanguage.structure.FieldDeclaration"), "isVolatile", "" + (x.hasModifierProperty(PsiModifier.VOLATILE))); SPropertyOperations.set( SNodeOperations.cast(field, "jetbrains.mps.baseLanguage.structure.FieldDeclaration"), "isTransient", "" + (x.hasModifierProperty(PsiModifier.TRANSIENT))); } SPropertyOperations.set(field, "name", x.getName()); SLinkOperations.setTarget( SNodeOperations.cast(field, "jetbrains.mps.baseLanguage.structure.ClassifierMember"), "visibility", getVisibility(x), true); SPropertyOperations.set(field, "isFinal", "" + (isFinal(x))); SLinkOperations.setTarget(field, "type", convertType(x.getType()), true); addAnnotations(x, field); return field; }
@Nullable static HighlightInfo checkFinalFieldInitialized(@NotNull PsiField field) { if (!field.hasModifierProperty(PsiModifier.FINAL)) return null; if (isFieldInitializedAfterObjectConstruction(field)) return null; String description = JavaErrorMessages.message("variable.not.initialized", field.getName()); TextRange range = HighlightNamesUtil.getFieldDeclarationTextRange(field); HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR) .range(range) .descriptionAndTooltip(description) .create(); QuickFixAction.registerQuickFixAction( highlightInfo, HighlightMethodUtil.getFixRange(field), QUICK_FIX_FACTORY.createCreateConstructorParameterFromFieldFix(field)); QuickFixAction.registerQuickFixAction( highlightInfo, HighlightMethodUtil.getFixRange(field), QUICK_FIX_FACTORY.createInitializeFinalFieldInConstructorFix(field)); final PsiClass containingClass = field.getContainingClass(); if (containingClass != null && !containingClass.isInterface()) { QuickFixAction.registerQuickFixAction( highlightInfo, QUICK_FIX_FACTORY.createModifierListFix(field, PsiModifier.FINAL, false, false)); } QuickFixAction.registerQuickFixAction( highlightInfo, QUICK_FIX_FACTORY.createAddVariableInitializerFix(field)); return highlightInfo; }
private static String gettext(PsiField field, PsiType uiComponentType) { return UIDesignerBundle.message( "action.change.field.type", field.getName(), field.getType().getCanonicalText(), uiComponentType.getCanonicalText()); }
/** Create fields for injections inside main class */ protected void generateFields() { for (Iterator<Element> iterator = mElements.iterator(); iterator.hasNext(); ) { Element element = iterator.next(); if (!element.used) { iterator.remove(); continue; } // remove duplicate field PsiField[] fields = mClass.getFields(); boolean duplicateField = false; for (PsiField field : fields) { String name = field.getName(); if (name != null && name.equals(element.getFieldName())) { duplicateField = true; break; } } if (duplicateField) { iterator.remove(); continue; } mClass.add( mFactory.createFieldFromText( "private " + element.name + " " + element.getFieldName() + ";", mClass)); } }
@Override public int compare(PsiVariable o1, PsiVariable o2) { if (o1 instanceof PsiParameter && ((PsiParameter) o1).isVarArgs()) return 1; if (o2 instanceof PsiParameter && ((PsiParameter) o2).isVarArgs()) return -1; if (o1 instanceof PsiField && o2 instanceof PsiField) { return o1.getTextOffset() - o2.getTextOffset(); } if (o1 instanceof PsiParameter && o2 instanceof PsiParameter) { return myParameterList.getParameterIndex((PsiParameter) o1) - myParameterList.getParameterIndex((PsiParameter) o2); } if (o1 instanceof PsiField && o2 instanceof PsiParameter) { final PsiField field = FieldFromParameterUtils.getParameterAssignedToField((PsiParameter) o2); if (field == null) return 1; return o1.getTextOffset() - field.getTextOffset(); } if (o1 instanceof PsiParameter && o2 instanceof PsiField) { final PsiField field = FieldFromParameterUtils.getParameterAssignedToField((PsiParameter) o1); if (field == null) return -1; return field.getTextOffset() - o2.getTextOffset(); } return 0; }
@Nullable private static String getCannotRefactorMessage(PsiMember member) { if (member == null) { return RefactoringBundle.message("locate.caret.inside.a.method"); } if (member instanceof PsiMethod) { if (((PsiMethod) member).isConstructor()) { return RefactoringBundle.message("replace.with.method.call.does.not.work.for.constructors"); } final PsiCodeBlock body = ((PsiMethod) member).getBody(); if (body == null) { return RefactoringBundle.message("method.does.not.have.a.body", member.getName()); } final PsiStatement[] statements = body.getStatements(); if (statements.length == 0) { return RefactoringBundle.message("method.has.an.empty.body", member.getName()); } } else if (member instanceof PsiField) { final PsiField field = (PsiField) member; if (field.getInitializer() == null) { return "Field " + member.getName() + " doesn't have initializer"; } final PsiClass containingClass = field.getContainingClass(); if (!field.hasModifierProperty(PsiModifier.FINAL) || !field.hasModifierProperty(PsiModifier.STATIC) || containingClass == null || containingClass.getQualifiedName() == null) { return "Replace Duplicates works with constants only"; } } else { return "Caret should be inside method or constant"; } return null; }
private static boolean canWriteToFinal( @NotNull PsiVariable variable, @NotNull PsiExpression expression, @NotNull PsiReferenceExpression reference, @NotNull PsiFile containingFile) { if (variable.hasInitializer()) return false; if (variable instanceof PsiParameter) return false; PsiElement innerClass = getInnerClassVariableReferencedFrom(variable, expression); if (variable instanceof PsiField) { // if inside some field initializer if (HighlightUtil.findEnclosingFieldInitializer(expression) != null) return true; // assignment from within inner class is illegal always PsiField field = (PsiField) variable; if (innerClass != null && !containingFile .getManager() .areElementsEquivalent(innerClass, field.getContainingClass())) return false; final PsiMember enclosingCtrOrInitializer = PsiUtil.findEnclosingConstructorOrInitializer(expression); return enclosingCtrOrInitializer != null && isSameField(enclosingCtrOrInitializer, field, reference, containingFile); } if (variable instanceof PsiLocalVariable) { boolean isAccessedFromOtherClass = innerClass != null; if (isAccessedFromOtherClass) { return false; } } return true; }
private static PsiField convertFieldToLanguage(PsiField field, Language language) { if (field.getLanguage().equals(language)) { return field; } return JVMElementFactories.getFactory(language, field.getProject()) .createField(field.getName(), field.getType()); }
public static void checkFieldConflicts( @Nullable PsiClass aClass, String newName, final Map<PsiElement, String> conflicts) { PsiField existingField = aClass != null ? aClass.findFieldByName(newName, true) : null; if (existingField != null) { if (aClass.equals(existingField.getContainingClass())) { String className = aClass instanceof PsiAnonymousClass ? RefactoringBundle.message("current.class") : RefactoringUIUtil.getDescription(aClass, false); final String conflict = RefactoringBundle.message( "field.0.is.already.defined.in.the.1", existingField.getName(), className); conflicts.put(existingField, conflict); } else { // method somewhere in base class if (!existingField.hasModifierProperty(PsiModifier.PRIVATE)) { String fieldInfo = PsiFormatUtil.formatVariable( existingField, PsiFormatUtil.SHOW_NAME | PsiFormatUtil.SHOW_TYPE | PsiFormatUtil.TYPE_AFTER, PsiSubstitutor.EMPTY); String className = RefactoringUIUtil.getDescription(existingField.getContainingClass(), false); final String descr = RefactoringBundle.message( "field.0.will.hide.field.1.of.the.base.class", newName, fieldInfo, className); conflicts.put(existingField, descr); } } } }
public FieldConflictsResolver(String name, GrCodeBlock scope) { myScope = scope; if (myScope == null) return; final GroovyPsiElement resolved = ResolveUtil.resolveProperty(myScope, name); if (resolved instanceof GrReferenceExpression || resolved == null) return; assert resolved instanceof PsiVariable; final PsiVariable oldVariable = (PsiVariable) resolved; myField = oldVariable instanceof PsiField ? (PsiField) oldVariable : null; if (!(oldVariable instanceof PsiField)) return; myReferenceExpressions = new ArrayList<>(); for (PsiReference reference : ReferencesSearch.search(myField, new LocalSearchScope(myScope), false)) { final PsiElement element = reference.getElement(); if (element instanceof GrReferenceExpression) { final GrReferenceExpression referenceExpression = (GrReferenceExpression) element; if (referenceExpression.getQualifier() == null) { myReferenceExpressions.add(referenceExpression); } } } if (myField.hasModifierProperty(PsiModifier.STATIC)) { myQualifyingClass = myField.getContainingClass(); } }
private void generateAccessors() { // generate accessors myNameToGetter = new HashMap<String, PsiMethod>(); myNameToSetter = new HashMap<String, PsiMethod>(); for (FieldDescriptor fieldDescriptor : myFieldDescriptors) { final DocCommentPolicy<PsiDocComment> commentPolicy = new DocCommentPolicy<PsiDocComment>(myDescriptor.getJavadocPolicy()); PsiField field = fieldDescriptor.getField(); final PsiDocComment docComment = field.getDocComment(); if (myDescriptor.isToEncapsulateGet()) { final PsiMethod prototype = fieldDescriptor.getGetterPrototype(); assert prototype != null; final PsiMethod getter = addOrChangeAccessor(prototype, myNameToGetter); if (docComment != null) { final PsiDocComment getterJavadoc = (PsiDocComment) getter.addBefore(docComment, getter.getFirstChild()); commentPolicy.processNewJavaDoc(getterJavadoc); } } if (myDescriptor.isToEncapsulateSet() && !field.hasModifierProperty(PsiModifier.FINAL)) { PsiMethod prototype = fieldDescriptor.getSetterPrototype(); assert prototype != null; addOrChangeAccessor(prototype, myNameToSetter); } if (docComment != null) { commentPolicy.processOldJavaDoc(docComment); } } }
@Override public void visitField(@NotNull PsiField field) { if (!field.hasModifierProperty(PsiModifier.STATIC) || field.hasModifierProperty(PsiModifier.FINAL)) { return; } registerFieldError(field, field); }
public CreateConstructorParameterFromFieldFix(@NotNull PsiField field) { myClass = field.getContainingClass(); myField = SmartPointerManager.getInstance(field.getProject()).createSmartPsiElementPointer(field); if (myClass != null) { getFieldsToFix().add(myField); } }
@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; }
public Collection<PsiElement> getAdditionalElementsToDelete( @NotNull final PsiElement element, @NotNull final Collection<PsiElement> allElementsToDelete, final boolean askUser) { if (element instanceof PsiField) { PsiField field = (PsiField) element; final Project project = element.getProject(); String propertyName = JavaCodeStyleManager.getInstance(project) .variableNameToPropertyName(field.getName(), VariableKind.FIELD); PsiClass aClass = field.getContainingClass(); if (aClass != null) { boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC); PsiMethod[] getters = GetterSetterPrototypeProvider.findGetters(aClass, propertyName, isStatic); if (getters != null) { final List<PsiMethod> validGetters = new ArrayList<>(1); for (PsiMethod getter : getters) { if (!allElementsToDelete.contains(getter) && (getter != null && getter.isPhysical())) { validGetters.add(getter); } } getters = validGetters.isEmpty() ? null : validGetters.toArray(new PsiMethod[validGetters.size()]); } PsiMethod setter = PropertyUtil.findPropertySetter(aClass, propertyName, isStatic, false); if (allElementsToDelete.contains(setter) || setter != null && !setter.isPhysical()) setter = null; if (askUser && (getters != null || setter != null)) { final String message = RefactoringMessageUtil.getGetterSetterMessage( field.getName(), RefactoringBundle.message("delete.title"), getters != null ? getters[0] : null, setter); if (!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog( project, message, RefactoringBundle.message("safe.delete.title"), Messages.getQuestionIcon()) != Messages.YES) { getters = null; setter = null; } } List<PsiElement> elements = new ArrayList<>(); if (setter != null) elements.add(setter); if (getters != null) Collections.addAll(elements, getters); return elements; } } return null; }
/** * Check the element. If the element is a PsiMethod, than we want to know if it's a @Provides * method, or a Constructor annotated w/ @Inject. * * <p>If element is a field, than we only want to see if it is annotated with @Inject. * * @return a {@link com.intellij.codeInsight.daemon.GutterIconNavigationHandler} for the * appropriate type, or null if we don't care about it. */ @Nullable @Override public LineMarkerInfo getLineMarkerInfo(@NotNull final PsiElement element) { // Check methods first (includes constructors). if (element instanceof PsiMethod) { PsiMethod methodElement = (PsiMethod) element; // @Provides if (PsiConsultantImpl.hasAnnotation(element, CLASS_PROVIDES)) { PsiTypeElement returnTypeElement = methodElement.getReturnTypeElement(); if (returnTypeElement != null) { return new LineMarkerInfo<PsiElement>( element, returnTypeElement.getTextRange(), ICON, UPDATE_ALL, null, new ProvidesToInjectHandler(), LEFT); } } // Constructor injection. if (methodElement.isConstructor() && PsiConsultantImpl.hasAnnotation(element, CLASS_INJECT)) { PsiIdentifier nameIdentifier = methodElement.getNameIdentifier(); if (nameIdentifier != null) { return new LineMarkerInfo<PsiElement>( element, nameIdentifier.getTextRange(), ICON, UPDATE_ALL, null, new ConstructorInjectToProvidesHandler(), LEFT); } } // Not a method, is it a Field? } else if (element instanceof PsiField) { // Field injection. PsiField fieldElement = (PsiField) element; PsiTypeElement typeElement = fieldElement.getTypeElement(); if (PsiConsultantImpl.hasAnnotation(element, CLASS_INJECT) && typeElement != null) { return new LineMarkerInfo<PsiElement>( element, typeElement.getTextRange(), ICON, UPDATE_ALL, null, new FieldInjectToProvidesHandler(), LEFT); } } return null; }