diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/GenerateMembersUtil.java b/java/java-impl/src/com/intellij/codeInsight/generation/GenerateMembersUtil.java index e8a54036eafa..538d28a62bab 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/GenerateMembersUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/GenerateMembersUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -281,19 +281,17 @@ public class GenerateMembersUtil { final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); try { - final PsiMethod resultMethod = createMethod(factory, sourceMethod, target); + PsiMethod resultMethod = createMethod(factory, sourceMethod, target); copyModifiers(sourceMethod.getModifierList(), resultMethod.getModifierList()); - final PsiSubstitutor collisionResolvedSubstitutor = + PsiSubstitutor collisionResolvedSubstitutor = substituteTypeParameters(factory, target, sourceMethod.getTypeParameterList(), resultMethod.getTypeParameterList(), substitutor, sourceMethod); substituteReturnType(PsiManager.getInstance(project), resultMethod, sourceMethod.getReturnType(), collisionResolvedSubstitutor); substituteParameters(factory, codeStyleManager, sourceMethod.getParameterList(), resultMethod.getParameterList(), collisionResolvedSubstitutor, target); copyDocComment(sourceMethod, resultMethod, factory); GlobalSearchScope scope = sourceMethod.getResolveScope(); - final List thrownTypes = ExceptionUtil.collectSubstituted(collisionResolvedSubstitutor, sourceMethod.getThrowsList().getReferencedTypes(), - scope); + List thrownTypes = ExceptionUtil.collectSubstituted(collisionResolvedSubstitutor, sourceMethod.getThrowsList().getReferencedTypes(), scope); if (target instanceof PsiClass) { - final PsiMethod[] methods = ((PsiClass)target).findMethodsBySignature(sourceMethod, true); - for (PsiMethod psiMethod : methods) { + for (PsiMethod psiMethod : ((PsiClass)target).findMethodsBySignature(sourceMethod, true)) { if (psiMethod != null && psiMethod != sourceMethod && !MethodSignatureUtil.isSuperMethod(psiMethod, sourceMethod)) { PsiClass aSuper = psiMethod.getContainingClass(); if (aSuper != null && aSuper != target) { @@ -322,7 +320,7 @@ public class GenerateMembersUtil { @Nullable PsiElement target, @Nullable PsiTypeParameterList sourceTypeParameterList, @Nullable PsiTypeParameterList targetTypeParameterList, - @NotNull PsiSubstitutor substitutor, + @NotNull PsiSubstitutor substitutor, @NotNull PsiMethod sourceMethod) { if (sourceTypeParameterList == null || targetTypeParameterList == null || PsiUtil.isRawSubstitutor(sourceMethod, substitutor)) { return substitutor; @@ -386,7 +384,7 @@ public class GenerateMembersUtil { @NotNull private static PsiTypeParameter substituteTypeParameter(final @NotNull JVMElementFactory factory, @NotNull PsiTypeParameter typeParameter, - final @NotNull PsiSubstitutor substitutor, + final @NotNull PsiSubstitutor substitutor, @NotNull final PsiMethod sourceMethod) { final PsiElement copy = typeParameter.copy(); final Map replacementMap = new HashMap<>(); @@ -464,7 +462,7 @@ public class GenerateMembersUtil { private static void substituteThrows(@NotNull JVMElementFactory factory, @NotNull PsiReferenceList targetThrowsList, @NotNull PsiSubstitutor substitutor, - @NotNull PsiMethod sourceMethod, + @NotNull PsiMethod sourceMethod, List thrownTypes) { for (PsiClassType thrownType : thrownTypes) { targetThrowsList.add(factory.createReferenceElementByType((PsiClassType)substituteType(substitutor, thrownType, sourceMethod))); @@ -522,7 +520,7 @@ public class GenerateMembersUtil { return true; } final String typeName = JavaCodeStyleManagerImpl.getTypeName(parameterType); - return typeName != null && + return typeName != null && NameUtil.getSuggestionsByName(typeName, "", "", false, false, parameterType instanceof PsiArrayType).contains(paramName); } @@ -564,7 +562,7 @@ public class GenerateMembersUtil { int right = rBrace != null ? rBrace.getStartOffsetInParent() : body.getTextLength(); emptyTemplate = StringUtil.isEmptyOrSpaces(body.getText().substring(left, right)); } - + if (overridden == null) { if (emptyTemplate) { CreateFromUsageUtils.setupMethodBody(method, containingClass); @@ -624,21 +622,25 @@ public class GenerateMembersUtil { //custom getters/setters public static String suggestGetterName(PsiField field) { - final PsiMethod prototype = generateGetterPrototype(field); - return prototype != null ? prototype.getName() : PropertyUtil.suggestGetterName(field); + return generateGetterPrototype(field).getName(); } public static String suggestGetterName(String name, PsiType type, Project project) { - return suggestGetterName(JavaPsiFacade.getElementFactory(project).createField(name, type instanceof PsiEllipsisType ? ((PsiEllipsisType)type).toArrayType() : type)); + if (type instanceof PsiEllipsisType) { + type = ((PsiEllipsisType)type).toArrayType(); + } + return suggestGetterName(JavaPsiFacade.getElementFactory(project).createField(name, type)); } public static String suggestSetterName(PsiField field) { - final PsiMethod prototype = generateSetterPrototype(field); - return prototype != null ? prototype.getName() : PropertyUtil.suggestSetterName(field); + return generateSetterPrototype(field).getName(); } public static String suggestSetterName(String name, PsiType type, Project project) { - return suggestSetterName(JavaPsiFacade.getElementFactory(project).createField(name, type instanceof PsiEllipsisType ? ((PsiEllipsisType)type).toArrayType() : type)); + if (type instanceof PsiEllipsisType) { + type = ((PsiEllipsisType)type).toArrayType(); + } + return suggestSetterName(JavaPsiFacade.getElementFactory(project).createField(name, type)); } public static PsiMethod generateGetterPrototype(@NotNull PsiField field) { @@ -739,4 +741,4 @@ public class GenerateMembersUtil { } return generated; } -} +} \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/OverrideImplementsAnnotationsHandler.java b/java/java-impl/src/com/intellij/codeInsight/generation/OverrideImplementsAnnotationsHandler.java index e41d252a4883..e79fe2227540 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/OverrideImplementsAnnotationsHandler.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/OverrideImplementsAnnotationsHandler.java @@ -36,24 +36,23 @@ public interface OverrideImplementsAnnotationsHandler { ExtensionPointName EP_NAME = ExtensionPointName.create("com.intellij.overrideImplementsAnnotationsHandler"); /** - * By default no annotations from source method return type and parameters are repeated. - * - * Return annotations which should be copied from the source to the implementation. + * Returns annotations which should be copied from a source to an implementation (by default, no annotations are copied). */ String[] getAnnotations(Project project); @Deprecated @NotNull @SuppressWarnings("unused") - default String [] annotationsToRemove(Project project, @NotNull String fqName) { + default String[] annotationsToRemove(Project project, @NotNull String fqName) { return ArrayUtil.EMPTY_STRING_ARRAY; } static void repeatAnnotationsFromSource(PsiModifierListOwner source, @Nullable PsiElement targetClass, PsiModifierListOwner target) { - final Module module = ModuleUtilCore.findModuleForPsiElement(targetClass != null ? targetClass : target); - final GlobalSearchScope moduleScope = module != null ? GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module) : null; - final Project project = target.getProject(); - final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); + Module module = ModuleUtilCore.findModuleForPsiElement(targetClass != null ? targetClass : target); + GlobalSearchScope moduleScope = module != null ? GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module) : null; + Project project = target.getProject(); + JavaPsiFacade facade = JavaPsiFacade.getInstance(project); + for (OverrideImplementsAnnotationsHandler each : Extensions.getExtensions(EP_NAME)) { for (String annotation : each.getAnnotations(project)) { if (moduleScope != null && facade.findClass(annotation, moduleScope) == null) continue; @@ -65,7 +64,9 @@ public interface OverrideImplementsAnnotationsHandler { continue; } - AddAnnotationPsiFix.addPhysicalAnnotation(annotation, PsiNameValuePair.EMPTY_ARRAY, target.getModifierList()); + PsiModifierList modifierList = target.getModifierList(); + assert modifierList != null : target; + AddAnnotationPsiFix.addPhysicalAnnotation(annotation, PsiNameValuePair.EMPTY_ARRAY, modifierList); } } } diff --git a/java/java-psi-api/src/com/intellij/codeInsight/AnnotationUtil.java b/java/java-psi-api/src/com/intellij/codeInsight/AnnotationUtil.java index 03c9392951c7..d2d78150bd19 100644 --- a/java/java-psi-api/src/com/intellij/codeInsight/AnnotationUtil.java +++ b/java/java-psi-api/src/com/intellij/codeInsight/AnnotationUtil.java @@ -276,11 +276,11 @@ public class AnnotationUtil { } private static boolean isAnnotated(@NotNull PsiModifierListOwner listOwner, - @NotNull String annotationFQN, - boolean checkHierarchy, - boolean skipExternal, - boolean skipInferred, - @Nullable Set processed) { + @NotNull String annotationFQN, + boolean checkHierarchy, + boolean skipExternal, + boolean skipInferred, + @Nullable Set processed) { if (!listOwner.isValid()) return false; PsiModifierList modifierList = listOwner.getModifierList(); @@ -296,16 +296,19 @@ public class AnnotationUtil { else if (listOwner instanceof PsiVariable) { type = ((PsiVariable)listOwner).getType(); } - if (type != null && type.findAnnotation(annotationFQN) != null) return true; + if (type != null && type.findAnnotation(annotationFQN) != null) { + return true; + } if (!skipExternal) { - final Project project = listOwner.getProject(); + Project project = listOwner.getProject(); if (ExternalAnnotationsManager.getInstance(project).findExternalAnnotation(listOwner, annotationFQN) != null) { return true; } } + if (!skipInferred) { - final Project project = listOwner.getProject(); + Project project = listOwner.getProject(); if (InferredAnnotationsManager.getInstance(project).findInferredAnnotation(listOwner, annotationFQN) != null) { return true; } @@ -316,18 +319,20 @@ public class AnnotationUtil { PsiMethod method = (PsiMethod)listOwner; if (processed == null) processed = new THashSet<>(); if (!processed.add(method)) return false; - final PsiMethod[] superMethods = method.findSuperMethods(); - for (PsiMethod superMethod : superMethods) { - if (isAnnotated(superMethod, annotationFQN, true, skipExternal, skipInferred, processed)) return true; + for (PsiMethod superMethod : method.findSuperMethods()) { + if (isAnnotated(superMethod, annotationFQN, true, skipExternal, skipInferred, processed)) { + return true; + } } } else if (listOwner instanceof PsiClass) { - final PsiClass clazz = (PsiClass)listOwner; + PsiClass clazz = (PsiClass)listOwner; if (processed == null) processed = new THashSet<>(); if (!processed.add(clazz)) return false; - final PsiClass[] superClasses = clazz.getSupers(); - for (PsiClass superClass : superClasses) { - if (isAnnotated(superClass, annotationFQN, true, skipExternal, skipInferred, processed)) return true; + for (PsiClass superClass : clazz.getSupers()) { + if (isAnnotated(superClass, annotationFQN, true, skipExternal, skipInferred, processed)) { + return true; + } } } }