private static boolean containsAllCases(GrSwitchStatement statement) { final GrCaseSection[] sections = statement.getCaseSections(); for (GrCaseSection section : sections) { if (section.isDefault()) return true; } final GrExpression condition = statement.getCondition(); if (!(condition instanceof GrReferenceExpression)) return false; PsiType type = TypesUtil.unboxPrimitiveTypeWrapper(getNominalTypeNoRecursion(condition)); if (type == null) return false; if (type instanceof PsiPrimitiveType) { if (type == PsiType.BOOLEAN) return sections.length == 2; if (type == PsiType.BYTE || type == PsiType.CHAR) return sections.length == 128; return false; } if (type instanceof PsiClassType) { final PsiClass resolved = ((PsiClassType) type).resolve(); if (resolved != null && resolved.isEnum()) { int enumConstantCount = 0; final PsiField[] fields = resolved.getFields(); for (PsiField field : fields) { if (field instanceof PsiEnumConstant) enumConstantCount++; } if (sections.length == enumConstantCount) return true; } } return false; }
@Nullable public static PsiType getExpectedClosureReturnType(GrClosableBlock closure) { final Set<PsiType> expectedTypes = getDefaultExpectedTypes(closure); List<PsiType> expectedReturnTypes = new ArrayList<PsiType>(); for (PsiType expectedType : expectedTypes) { if (!(expectedType instanceof PsiClassType)) return null; final PsiClassType.ClassResolveResult resolveResult = ((PsiClassType) expectedType).resolveGenerics(); final PsiClass resolved = resolveResult.getElement(); if (resolved == null || !(GroovyCommonClassNames.GROOVY_LANG_CLOSURE.equals(resolved.getQualifiedName()))) return null; final PsiTypeParameter[] typeParameters = resolved.getTypeParameters(); if (typeParameters.length != 1) return null; final PsiTypeParameter expected = typeParameters[0]; final PsiType expectedReturnType = resolveResult.getSubstitutor().substitute(expected); if (expectedReturnType == PsiType.VOID || expectedReturnType == null) return null; expectedReturnTypes.add(expectedReturnType); } return TypesUtil.getLeastUpperBoundNullable(expectedReturnTypes, closure.getManager()); }
private static boolean generateDelegate(GrChangeInfoImpl grInfo) { final GrMethod method = grInfo.getMethod(); final PsiClass psiClass = method.getContainingClass(); GrMethod newMethod = (GrMethod) method.copy(); newMethod = (GrMethod) psiClass.addAfter(newMethod, method); StringBuilder buffer = new StringBuilder(); buffer.append("\n"); if (method.isConstructor()) { buffer.append("this"); } else { if (!PsiType.VOID.equals(method.getReturnType())) { buffer.append("return "); } buffer.append( GrChangeSignatureUtil.getNameWithQuotesIfNeeded( grInfo.getNewName(), method.getProject())); } generateParametersForDelegateCall(grInfo, method, buffer); final GrCodeBlock codeBlock = GroovyPsiElementFactory.getInstance(method.getProject()) .createMethodBodyFromText(buffer.toString()); newMethod.setBlock(codeBlock); newMethod.getModifierList().setModifierProperty(PsiModifier.ABSTRACT, false); CodeStyleManager.getInstance(method.getProject()).reformat(newMethod); return processPrimaryMethodInner(grInfo, method, null); }
@Override public void visitAnnotationArrayInitializer(GrAnnotationArrayInitializer arrayInitializer) { final GrAnnotationNameValuePair nameValuePair = PsiTreeUtil.getParentOfType( arrayInitializer, GrAnnotationNameValuePair.class, true, GrDefaultAnnotationValue.class); if (nameValuePair != null) { final PsiClass annot = ResolveUtil.resolveAnnotation(arrayInitializer); if (annot == null) return; final String name = nameValuePair.getName(); if (name == null) return; final PsiMethod[] attrs = annot.findMethodsByName(name, false); if (attrs.length > 0) { PsiType type = attrs[0].getReturnType(); while (type instanceof PsiArrayType) type = ((PsiArrayType) type).getComponentType(); if (type != null && isAcceptableAnnotationValueType(type)) { myResult = createSimpleSubTypeResult(type); } } } else { final GrAnnotationMethod method = PsiTreeUtil.getParentOfType(arrayInitializer, GrAnnotationMethod.class); assert method != null; PsiType type = method.getReturnType(); int count = 1; PsiElement parent = arrayInitializer.getParent(); while (parent instanceof GrAnnotationArrayInitializer) { count++; parent = parent.getParent(); } while (type instanceof PsiArrayType && count > 0) { type = ((PsiArrayType) type).getComponentType(); count--; } if (type != null && isAcceptableAnnotationValueType(type)) { myResult = createSimpleSubTypeResult(type); } } }
@Override public void visitAnnotationNameValuePair(GrAnnotationNameValuePair nameValuePair) { if (myExpression.equals(nameValuePair.getValue())) { final PsiClass annot = ResolveUtil.resolveAnnotation(nameValuePair.getParent()); if (annot != null) { final String name = nameValuePair.getName(); if (name != null) { final PsiMethod[] attrs = annot.findMethodsByName(name, false); if (attrs.length > 0) { PsiType type = attrs[0].getReturnType(); while (type instanceof PsiArrayType) type = ((PsiArrayType) type).getComponentType(); if (type != null && isAcceptableAnnotationValueType(type)) { myResult = createSimpleSubTypeResult(type); } } } else { final PsiMethod[] valueAttr = annot.findMethodsByName("value", false); boolean canHaveSimpleExpr = valueAttr.length > 0; final PsiMethod[] methods = annot.getMethods(); for (PsiMethod method : methods) { if (!("value".equals(method.getName()) || method instanceof PsiAnnotationMethod && ((PsiAnnotationMethod) method).getDefaultValue() != null)) { canHaveSimpleExpr = false; } } if (canHaveSimpleExpr) { PsiType type = valueAttr[0].getReturnType(); while (type instanceof PsiArrayType) type = ((PsiArrayType) type).getComponentType(); if (type != null && isAcceptableAnnotationValueType(type)) { myResult = createSimpleSubTypeResult(type); } } } } } }