private String createNewVariableName(
   @NotNull PsiForStatement scope, PsiType type,
   @Nullable String containerName) {
   final Project project = scope.getProject();
   final JavaCodeStyleManager codeStyleManager =
     JavaCodeStyleManager.getInstance(project);
   @NonNls String baseName;
   if (containerName != null) {
     baseName = StringUtils.createSingularFromName(containerName);
   }
   else {
     final SuggestedNameInfo suggestions =
       codeStyleManager.suggestVariableName(
         VariableKind.LOCAL_VARIABLE, null, null, type);
     final String[] names = suggestions.names;
     if (names != null && names.length > 0) {
       baseName = names[0];
     }
     else {
       baseName = "value";
     }
   }
   if (baseName == null || baseName.isEmpty()) {
     baseName = "value";
   }
   return codeStyleManager.suggestUniqueVariableName(baseName, scope,
                                                     true);
 }
Exemplo n.º 2
0
 public static String cleanRefIdForPsiElement(PsiElement element) {
   final Language language = element.getLanguage();
   if (language instanceof JavaLanguage) {
     if (element instanceof PsiModifierList) {
       PsiAnnotation[] annotations = ((PsiAnnotationOwner) element).getAnnotations();
       for (PsiAnnotation annotation : annotations) {
         if (AnnoRefConfigSettings.getInstance(element.getProject())
             .getAnnoRefState()
             .ANNOREF_ANNOTATION_FQN
             .equals(annotation.getQualifiedName())) {
           String refKey =
               SQLRefNamingUtil.cleanAnnoRefForName(annotation.getContainingFile(), annotation);
           if (refKey != null) {
             return refKey;
           }
         }
       }
     }
     if (element instanceof PsiAnnotation) {
       final PsiAnnotation psiAnnotation = (PsiAnnotation) element;
       if (AnnoRefConfigSettings.getInstance(element.getProject())
           .getAnnoRefState()
           .ANNOREF_ANNOTATION_FQN
           .equals(psiAnnotation.getQualifiedName())) {
         String cleanedAnnoRef =
             SQLRefNamingUtil.cleanAnnoRefForName(
                 psiAnnotation.getContainingFile(), psiAnnotation);
         if (cleanedAnnoRef != null) {
           return cleanedAnnoRef;
         }
       }
       if (element instanceof PsiJavaToken
           && ((PsiJavaToken) element).getTokenType() == JavaTokenType.STRING_LITERAL) {
         final String refKey = StringUtils.cleanQuote(element.getText());
         if (refKey != null) {
           return refKey;
         }
       }
     }
   }
   if (language instanceof XMLLanguage) {
     if (SQLRefNamingUtil.isPropitiousXmlFile(element.getContainingFile())) {
       final boolean isXmlTokenValid =
           new FilterElementProcessor(
                   new XmlTokenTypeFilter(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN))
               .execute(element);
       if (isXmlTokenValid) {
         final String refKey = getSqlRefKey(element);
         if (refKey != null) {
           return refKey;
         }
       }
     }
   }
   return null;
 }
Exemplo n.º 3
0
 public static SQLRefReference isValidAnnoRef(PsiElement psiElement) {
   final Language language = psiElement.getLanguage();
   final Project project = psiElement.getProject();
   if (language instanceof JavaLanguage) {
     if (psiElement instanceof PsiModifierList) {
       PsiAnnotation[] annotations = ((PsiAnnotationOwner) psiElement).getAnnotations();
       for (PsiAnnotation annotation : annotations) {
         return getValidAnnoReference(annotation, project);
       }
     }
     if (psiElement instanceof PsiAnnotation) {
       return getValidAnnoReference((PsiAnnotation) psiElement, project);
     }
     if (psiElement instanceof PsiJavaToken
         && ((PsiJavaToken) psiElement).getTokenType() == JavaTokenType.STRING_LITERAL) {
       return getValidAnnoReference(StringUtils.cleanQuote(psiElement.getText()), project);
     }
   }
   if (language instanceof XMLLanguage) {
     if (SQLRefNamingUtil.isPropitiousXmlFile(psiElement.getContainingFile())) {
       final boolean isXmlTokenValid =
           new FilterElementProcessor(
                   new XmlTokenTypeFilter(XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN))
               .execute(psiElement);
       if (isXmlTokenValid) {
         final String sqlRefKey = getSqlRefKey(psiElement);
         if (sqlRefKey != null) {
           SQLRefReference sqlRefReferenceForID =
               ServiceManager.getService(psiElement.getProject(), SQLRefRepository.class)
                   .getSQLRefReferenceForID(sqlRefKey);
           if (sqlRefReferenceForID != null) {
             if (!sqlRefReferenceForID.getClassAnnoElements().isEmpty()) {
               return sqlRefReferenceForID;
             }
           }
         }
       }
     }
   }
   return null;
 }