Skip to content

Commit

Permalink
Merge pull request #105 from FirstJavaMaster/convert-with-NPE-judge
Browse files Browse the repository at this point in the history
Convert with npe judge
  • Loading branch information
gejun123456 authored Apr 9, 2024
2 parents 46af617 + 70d8fb5 commit ea903d8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ public class CommonConstants {
public static final String GENERATE_GETTER_METHOD = "Generate all getter";

public static final String GENERATE_SETTER_METHOD_NO_DEAULT_VALUE = "";

public static final String INDENTATION_CHARACTER_TAB = "\t";
public static final String INDENTATION_CHARACTER_4_SPACE = " ";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

package com.bruce.intellijplugin.generatesetter.actions;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.bruce.intellijplugin.generatesetter.CommonConstants;
import com.bruce.intellijplugin.generatesetter.GetInfo;
import com.bruce.intellijplugin.generatesetter.Parameters;
Expand Down Expand Up @@ -52,13 +59,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* @author bruce ge
*/
Expand Down Expand Up @@ -252,6 +252,9 @@ private InsertDto getBaseInsertDto(String splitText,
insertText += generateStringForNoParam(generateName, methods,
splitText, importList, hasGuava);
} else {
insertText = splitText + "if (" + info.getParamName() + " == null) {"
+ indentText(splitText) + "return null;"
+ splitText + "}" + insertText;
insertText += generateStringForParam(generateName, methods,
splitText, importList, hasGuava, info);
}
Expand Down Expand Up @@ -381,29 +384,47 @@ private static GetInfo buildInfo(PsiParameter parameter,
return info;
}

/**
* 分析当前方法的缩进符, 并追加缩进
* 为保持原方法功能, 返回值的开头会增加一个"\n"
* -
* Analyze the current method's indentation character and append indentation.
* To maintain the original function of the method, a "\n" will be added to the beginning of the return value.
*
* @param method curr method
* @param document curr doc
* @return the indented text
*/
@NotNull
protected static String extractSplitText(PsiMethod method,
Document document) {
int startOffset = method.getTextRange().getStartOffset();
int lastLine = startOffset - 1;
String text = document.getText(new TextRange(lastLine, lastLine + 1));
boolean isTable = false;
while (!text.equals("\n")) {
if (text.equals('\t')) {
isTable = true;
}
lastLine--;
text = document.getText(new TextRange(lastLine, lastLine + 1));
}
String methodStartToLastLineText = document
.getText(new TextRange(lastLine, startOffset));
String splitText = "";
if (isTable) {
splitText += methodStartToLastLineText + "\t";
} else {
splitText = methodStartToLastLineText + " ";
}
return splitText;
protected static String extractSplitText(PsiMethod method, Document document) {
int methodStartOffset = method.getTextRange().getStartOffset();
int lineNumber = document.getLineNumber(methodStartOffset);
int lineStartOffset = document.getLineStartOffset(lineNumber);
String currIndentedText = document.getText(new TextRange(lineStartOffset, methodStartOffset));
return "\n" + indentText(currIndentedText);
}

/**
* 对文本进行缩进
* 本方法内部会判断原文本的缩进符是\t还是空格
* 增加的缩进如果是空格的话,默认使用4个空格
* -
* indent the text
* this method internally determines whether the original text's indentation character is a tab (\t) or a space.
* if the added indentation is spaces, the default is to use 4 spaces.
*
* @param text raw text
* @return the indented text
*/
@NotNull
protected static String indentText(String text) {
if (text == null) {
return CommonConstants.INDENTATION_CHARACTER_4_SPACE;
}
return text +
(text.contains(CommonConstants.INDENTATION_CHARACTER_TAB)
? CommonConstants.INDENTATION_CHARACTER_TAB
: CommonConstants.INDENTATION_CHARACTER_4_SPACE);
}

private void handleWithLocalVariable(PsiLocalVariable localVariable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public void setPriority(BigDecimal priority) {
}

public static Article convertFrom(User user){
if (user == null) {
return null;
}
Article article = new Article();
article.setId();
article.setUuuuuuu(user.getUuuuuuu());
Expand Down

0 comments on commit ea903d8

Please sign in to comment.