/** VOGen.bsh - a BeanShell macro for the jEdit text editor that create get and set methods for all variables. Limitations: The content to be processed must have matching brackets for the macro to work correctly. Copyright (C) 2004 Jia Zhiming jiazhimi@yahoo.com.cn This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ boolean JAVA_MODE = buffer.getMode().getName().equals("java"); void convert() { if (!JAVA_MODE) { Macros.error( view, "The edit mode is not java mode. You may want to save the file to a java file first." ); return; } /** * used to hold the content of modified file, initially hold the * original content before the last '}', when encounter the last * '}', it will merg the StringBuffer expend, which hold all the * generated get and set methods, then write the final '}' */ StringBuffer sb = new StringBuffer(); /** * hold the generated get and set methods with javadocs */ StringBuffer expend = new StringBuffer(); //whether inside a comment block boolean commentsBlock = false; //whether the expend portion has been merged into sb boolean printed = false; //The level of brackets, eg {{content}} numOfBlks of content will be 2 int numOfBlks = 0; //whether is inside any bracket boolean inside = false; String tempStr = null; //variable type used in get and set method signature String typ = null; //variable name used in get and set method signature String varName = null; String toWrite = null; int lineCnt = textArea.getLineCount(); String inLine = null; for (int i = 0; i < lineCnt; i++) { inLine = textArea.getLineText(i); toWrite = inLine + "\n"; inLine = inLine.trim(); if (inLine.startsWith("/*")) { if (inLine.endsWith("*/")) { commentsBlock = false; sb.append(toWrite); continue; } else { commentsBlock = true; sb.append(toWrite); continue; } } if (inLine.endsWith("*/")) { commentsBlock = false; sb.append(toWrite); continue; } else if (commentsBlock) { sb.append(toWrite); continue; } int occ = occurance(inLine, "{"); if (occ > 0) { inside = true; numOfBlks += occ; } int occ2 = occurance(inLine, "}"); if (occ2 > 0) { numOfBlks -= occ2; } if (inside == true && numOfBlks == 0 && printed == false) { sb.append(expend.toString()); sb.append(toWrite); printed = true; continue; } if(numOfBlks == 0) { sb.append(toWrite); } else if (commentsBlock) { sb.append(toWrite); } else if (numOfBlks > 1) { sb.append(toWrite); } else if (!inLine.endsWith(";")) { sb.append(toWrite); } else if (inLine.startsWith("//")) { sb.append(toWrite); } else if (inLine.startsWith("package")) { sb.append(toWrite); } else if (inLine.startsWith("import")) { sb.append(toWrite); } else if (inLine.indexOf("=") < 0) { tempStr = inLine.substring(0, (inLine.length() - 1)); tempStr = tempStr.trim(); StringTokenizer st = new StringTokenizer(tempStr, " "); int max = st.countTokens(); for (int k = 0; k < (max - 2); k++) { st.nextToken(); } typ = st.nextToken(); varName = st.nextToken(); String getMethod = "public " + typ + " " + "get" + varName.substring(0,1).toUpperCase() + varName.substring(1) + "()"; sb.append(toWrite); if (textArea.getText().indexOf(getMethod) == -1) { expend.append("\n\t/**\n"); expend.append("\t * Returns the value of " + varName + ".\n"); expend.append("\t */"); expend.append("\n\t" + getMethod + "\n"); expend.append("\t{\n\t\t return " + varName + "; \n\t}\n"); if (inLine.indexOf("final ") == -1 && inLine.indexOf("const ") == -1) { expend.append("\n\t/**\n"); expend.append("\t * Sets the value of " + varName + ".\n"); expend.append("\t * @param " + typ + " " + varName + " The value to assign to " + varName + ".\n"); expend.append("\t */"); expend.append("\n\tpublic void " + "set" + varName.substring(0,1).toUpperCase() + varName.substring(1) + "(" + typ + " " + varName + ")\n"); expend.append("\t{\n\t\t this." + varName + " = " + varName + "; \n\t}\n"); } } } else { tempStr = inLine.substring(0, (inLine.lastIndexOf("=") - 1)); tempStr = tempStr.trim(); StringTokenizer st = new StringTokenizer(tempStr, " "); int max = st.countTokens(); for (int k = 0; k < (max - 2); k++) { st.nextToken(); } typ = st.nextToken(); varName = st.nextToken(); String getMethod = "public " + typ + " " + "get" + varName.substring(0,1).toUpperCase() + varName.substring(1) + "()"; sb.append(toWrite); if (textArea.getText().indexOf(getMethod) == -1) { expend.append("\n\t/**\n"); expend.append("\t * Returns the value of " + varName + ".\n"); expend.append("\t */"); expend.append("\n\t" + getMethod + "\n"); expend.append("\t{\n\t\t return " + varName + "; \n\t}\n"); if (inLine.indexOf("final ") == -1 && inLine.indexOf("const ") == -1) { expend.append("\n\t/**\n"); expend.append("\t * Sets the value of " + varName + ".\n"); expend.append("\t * @param " + typ + " " + varName + " The value to assign to " + varName + ".\n"); expend.append("\t */"); expend.append("\n\tpublic void " + "set" + varName.substring(0,1).toUpperCase() + varName.substring(1) + "(" + typ + " " + varName + ")\n"); expend.append("\t{\n\t\t this." + varName + " = " + varName + "; \n\t}\n"); } } } } textArea.setText(sb.toString()); } /** * get the number of occurance of the String toFind in target String tStr */ int occurance(String tStr, String toFind) { int cnt = 0; int idx = -1; idx = tStr.indexOf(toFind); while (idx > -1) { cnt = cnt + 1; tStr = tStr.substring(idx + 1); idx = tStr.indexOf(toFind); } return cnt; } if( buffer.isReadOnly() ) Macros.error( view, "Buffer is read-only." ); else convert();