Customised Emailable report is not generating in TestNG - java
I'm running my automation test scripts which are created using TestNg Framework. Now I need to generate report and send mail management.. I'm using below code to generate report but it is not working.
Java code to generate report:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.testng.IReporter;
import org.testng.IResultMap;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.log4testng.Logger;
import org.testng.reporters.util.StackTraceTools;
import org.testng.xml.XmlSuite;
/**
* Reported designed to render self-contained HTML top down view of a testing
* suite.
*
* #author Paul Mendelson
* #since 5.2
* #version $Revision$
*/
public class EmailableReporter implements IReporter {
private static final Logger L = Logger.getLogger(EmailableReporter.class);
// ~ Instance fields ------------------------------------------------------
private PrintWriter m_out;
private int m_row;
private int m_methodIndex;
private int m_rowTotal;
// ~ Methods --------------------------------------------------------------
/** Creates summary of the run */
public void generateReport(List<XmlSuite> xml, List<ISuite> suites, String outdir) {
try {
m_out = createWriter(outdir);
}
catch (IOException e) {
L.error("output file", e);
return;
}
startHtml(m_out);
generateSuiteSummaryReport(suites);
generateMethodSummaryReport(suites);
generateMethodDetailReport(suites);
endHtml(m_out);
m_out.flush();
m_out.close();
}
protected PrintWriter createWriter(String outdir) throws IOException {
return new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir, "email-report.html"))));
}
/** Creates a table showing the highlights of each test method with links to the method details */
protected void generateMethodSummaryReport(List<ISuite> suites) {
m_methodIndex = 0;
m_out.println("<a id=\"summary\"></a>");
startResultSummaryTable("passed");
for (ISuite suite : suites) {
if(suites.size()>1) {
titleRow(suite.getName(), 4);
}
Map<String, ISuiteResult> r = suite.getResults();
for (ISuiteResult r2 : r.values()) {
ITestContext test = r2.getTestContext();
resultSummary(test.getFailedConfigurations(), test.getName(), "failed", " (configuration methods)");
resultSummary(test.getFailedTests(), test.getName(), "failed", "");
resultSummary(test.getSkippedConfigurations(), test.getName(), "skipped", " (configuration methods)");
resultSummary(test.getSkippedTests(), test.getName(), "skipped", "");
resultSummary(test.getPassedTests(), test.getName(), "passed", "");
}
}
m_out.println("</table>");
}
/** Creates a section showing known results for each method */
protected void generateMethodDetailReport(List<ISuite> suites) {
m_methodIndex = 0;
for (ISuite suite : suites) {
Map<String, ISuiteResult> r = suite.getResults();
for (ISuiteResult r2 : r.values()) {
if (r.values().size() > 0) {
m_out.println("<h1>" + r2.getTestContext().getName() + "</h1>");
}
resultDetail(r2.getTestContext().getFailedConfigurations(), "failed");
resultDetail(r2.getTestContext().getFailedTests(), "failed");
resultDetail(r2.getTestContext().getSkippedConfigurations(), "skipped");
resultDetail(r2.getTestContext().getSkippedTests(), "skipped");
resultDetail(r2.getTestContext().getPassedTests(), "passed");
}
}
}
/**
* #param tests
*/
private void resultSummary(IResultMap tests, String testname, String style, String details) {
if (tests.getAllResults().size() > 0) {
StringBuffer buff = new StringBuffer();
String lastc = "";
int mq = 0;
int cq = 0;
for (ITestNGMethod method : getMethodSet(tests)) {
m_row += 1;
m_methodIndex += 1;
String cname = method.getTestClass().getName();
if (mq == 0) {
titleRow(testname + " — " + style + details, 4);
}
if (!cname.equalsIgnoreCase(lastc)) {
if (mq > 0) {
cq += 1;
m_out.println("<tr class=\"" + style
+ (cq % 2 == 0 ? "even" : "odd") + "\">" + "<td rowspan=\""
+ mq + "\">" + lastc + buff);
}
mq = 0;
buff.setLength(0);
lastc = cname;
}
Set<ITestResult> result_set = tests.getResults(method);
long end = Long.MIN_VALUE;
long start = Long.MAX_VALUE;
for (ITestResult ans : tests.getResults(method)) {
if (ans.getEndMillis() > end) {
end = ans.getEndMillis();
}
if (ans.getStartMillis() < start) {
start = ans.getStartMillis();
}
}
mq += 1;
if (mq > 1) {
buff.append("<tr class=\"" + style + (cq % 2 == 0 ? "odd" : "even")
+ "\">");
}
buff.append("<td><a href=\"#m" + m_methodIndex + "\">"
+ qualifiedName(method) + "</a></td>" + "<td class=\"numi\">"
+ result_set.size() + "</td><td class=\"numi\">" + (end - start)
+ "</td></tr>");
}
if (mq > 0) {
cq += 1;
m_out.println("<tr class=\"" + style + (cq % 2 == 0 ? "even" : "odd")
+ "\">" + "<td rowspan=\"" + mq + "\">" + lastc + buff);
}
}
}
/** Starts and defines columns result summary table */
private void startResultSummaryTable(String style) {
tableStart(style);
m_out.println("<tr><th>Class</th>"
+ "<th>Method</th><th># of<br/>Scenarios</th><th>Time<br/>(Msecs)</th></tr>");
m_row = 0;
}
private String qualifiedName(ITestNGMethod method) {
String addon = "";
if (method.getGroups().length > 0
&& !"basic".equalsIgnoreCase(method.getGroups()[0])) {
addon = " (" + method.getGroups()[0] + ")";
}
return method.getMethodName() + addon;
}
private void resultDetail(IResultMap tests, final String style) {
if (tests.getAllResults().size() > 0) {
int row = 0;
for (ITestNGMethod method : getMethodSet(tests)) {
row += 1;
m_methodIndex += 1;
String cname = method.getTestClass().getName();
m_out.println("<a id=\"m" + m_methodIndex + "\"></a><h2>" + cname + ":"
+ method.getMethodName() + "</h2>");
int rq = 0;
Set<ITestResult> resultSet = tests.getResults(method);
for (ITestResult ans : resultSet) {
rq += 1;
Object[] parameters = ans.getParameters();
boolean hasParameters = parameters != null && parameters.length > 0;
if (hasParameters) {
if (rq == 1) {
tableStart("param");
m_out.print("<tr>");
for (int x = 1; x <= parameters.length; x++) {
m_out
.print("<th style=\"padding-left:1em;padding-right:1em\">Parameter #"
+ x + "</th>");
}
m_out.println("</tr>");
}
m_out.print("<tr" + (rq % 2 == 0 ? " class=\"stripe\"" : "") + ">");
for (Object p : parameters) {
m_out
.println("<td style=\"padding-left:.5em;padding-right:2em\">"
+ (p != null ? p.toString() : "null") + "</td>");
}
m_out.println("</tr>");
}
List<String> msgs = Reporter.getOutput(ans);
boolean hasReporterOutput = msgs.size() > 0;
Throwable exception=ans.getThrowable();
boolean hasThrowable = exception!=null;
if (hasReporterOutput||hasThrowable) {
String indent = " style=\"padding-left:3em\"";
if (hasParameters) {
m_out.println("<tr" + (rq % 2 == 0 ? " class=\"stripe\"" : "")
+ "><td" + indent + " colspan=\"" + parameters.length + "\">");
}
else {
m_out.println("<div" + indent + ">");
}
if (hasReporterOutput) {
if(hasThrowable)
m_out.println("<h3>Test Messages</h3>");
for (String line : msgs) {
m_out.println(line + "<br/>");
}
}
if(hasThrowable) {
boolean wantsMinimalOutput = ans.getStatus()==ITestResult.SUCCESS;
if(hasReporterOutput)
m_out.println("<h3>"
+(wantsMinimalOutput?"Expected Exception":"Failure")
+"</h3>");
generateExceptionReport(exception,method);
}
if (hasParameters) {
m_out.println("</td></tr>");
}
else {
m_out.println("</div>");
}
}
if (hasParameters) {
if (rq == resultSet.size()) {
m_out.println("</table>");
}
}
}
m_out.println("<p class=\"totop\">back to summary</p>");
}
}
}
protected void generateExceptionReport(Throwable exception,ITestNGMethod method) {
generateExceptionReport(exception, method, exception.getLocalizedMessage());
}
private void generateExceptionReport(Throwable exception,ITestNGMethod method,String title) {
m_out.println("<p>" + escape(title) + "</p>");
StackTraceElement[] s1= exception.getStackTrace();
Throwable t2= exception.getCause();
if(t2 == exception) {
t2= null;
}
int maxlines= Math.min(100,StackTraceTools.getTestRoot(s1, method));
for(int x= 0; x <= maxlines; x++) {
m_out.println((x>0 ? "<br/>at " : "") + escape(s1[x].toString()));
}
if(maxlines < s1.length) {
m_out.println("<br/>" + (s1.length-maxlines) + " lines not shown");
}
if(t2 != null) {
generateExceptionReport(t2, method, "Caused by " + t2.getLocalizedMessage());
}
}
private static String escape(String string) {
if(null == string) return string;
return string.replaceAll("<", "<").replaceAll(">", ">");
}
/**
* #param tests
* #return
*/
private Collection<ITestNGMethod> getMethodSet(IResultMap tests) {
Set r = new TreeSet<ITestNGMethod>(new TestSorter<ITestNGMethod>());
r.addAll(tests.getAllMethods());
return r;
}
public void generateSuiteSummaryReport(List<ISuite> suites) {
tableStart("param");
m_out.print("<tr><th>Test</th>");
tableColumnStart("Methods<br/>Passed");
tableColumnStart("Scenarios<br/>Passed");
tableColumnStart("# skipped");
tableColumnStart("# failed");
tableColumnStart("Total<br/>Time");
tableColumnStart("Included<br/>Groups");
tableColumnStart("Excluded<br/>Groups");
m_out.println("</tr>");
NumberFormat formatter = new DecimalFormat("#,##0.0");
int qty_tests = 0;
int qty_pass_m = 0;
int qty_pass_s = 0;
int qty_skip = 0;
int qty_fail = 0;
long time_start = Long.MAX_VALUE;
long time_end = Long.MIN_VALUE;
for (ISuite suite : suites) {
if (suites.size() > 1) {
titleRow(suite.getName(), 7);
}
Map<String, ISuiteResult> tests = suite.getResults();
for (ISuiteResult r : tests.values()) {
qty_tests += 1;
ITestContext overview = r.getTestContext();
startSummaryRow(overview.getName());
int q = getMethodSet(overview.getPassedTests()).size();
qty_pass_m += q;
summaryCell(q,Integer.MAX_VALUE);
q = overview.getPassedTests().size();
qty_pass_s += q;
summaryCell(q,Integer.MAX_VALUE);
q = getMethodSet(overview.getSkippedTests()).size();
qty_skip += q;
summaryCell(q,0);
q = getMethodSet(overview.getFailedTests()).size();
qty_fail += q;
summaryCell(q,0);
time_start = Math.min(overview.getStartDate().getTime(), time_start);
time_end = Math.max(overview.getEndDate().getTime(), time_end);
summaryCell(formatter.format(
(overview.getEndDate().getTime() - overview.getStartDate().getTime()) / 1000.)
+ " seconds", true);
summaryCell(overview.getIncludedGroups());
summaryCell(overview.getExcludedGroups());
m_out.println("</tr>");
}
}
if (qty_tests > 1) {
m_out.println("<tr class=\"total\"><td>Total</td>");
summaryCell(qty_pass_m,Integer.MAX_VALUE);
summaryCell(qty_pass_s,Integer.MAX_VALUE);
summaryCell(qty_skip,0);
summaryCell(qty_fail,0);
summaryCell(formatter.format((time_end - time_start) / 1000.) + " seconds", true);
m_out.println("<td colspan=\"2\"> </td></tr>");
}
m_out.println("</table>");
}
private void summaryCell(String[] val) {
StringBuffer b = new StringBuffer();
for (String v : val) {
b.append(v + " ");
}
summaryCell(b.toString(),true);
}
private void summaryCell(String v,boolean isgood) {
m_out.print("<td class=\"numi"+(isgood?"":"_attn")+"\">" + v + "</td>");
}
private void startSummaryRow(String label) {
m_row += 1;
m_out.print("<tr" + (m_row % 2 == 0 ? " class=\"stripe\"" : "")
+ "><td style=\"text-align:left;padding-right:2em\">" + label
+ "</td>");
}
private void summaryCell(int v,int maxexpected) {
summaryCell(String.valueOf(v),v<=maxexpected);
m_rowTotal += v;
}
/**
*
*/
private void tableStart(String cssclass) {
m_out.println("<table cellspacing=0 cellpadding=0"
+ (cssclass != null ? " class=\"" + cssclass + "\""
: " style=\"padding-bottom:2em\"") + ">");
m_row = 0;
}
private void tableColumnStart(String label) {
m_out.print("<th class=\"numi\">" + label + "</th>");
}
private void titleRow(String label, int cq) {
m_out.println("<tr><th colspan=\"" + cq + "\">" + label + "</th></tr>");
m_row = 0;
}
protected void writeStyle(String[] formats,String[] targets) {
}
/** Starts HTML stream */
protected void startHtml(PrintWriter out) {
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<title>TestNG: Unit Test</title>");
out.println("<style type=\"text/css\">");
out.println("table caption,table.info_table,table.param,table.passed,table.failed {margin-bottom:10px;border:1px solid #000099;border-collapse:collapse;empty-cells:show;}");
out.println("table.info_table td,table.info_table th,table.param td,table.param th,table.passed td,table.passed th,table.failed td,table.failed th {");
out.println("border:1px solid #000099;padding:.25em .5em .25em .5em");
out.println("}");
out.println("table.param th {vertical-align:bottom}");
out.println("td.numi,th.numi,td.numi_attn {");
out.println("text-align:right");
out.println("}");
out.println("tr.total td {font-weight:bold}");
out.println("table caption {");
out.println("text-align:center;font-weight:bold;");
out.println("}");
out.println("table.passed tr.stripe td,table tr.passedodd td {background-color: #00AA00;}");
out.println("table.passed td,table tr.passedeven td {background-color: #33FF33;}");
out.println("table.passed tr.stripe td,table tr.skippedodd td {background-color: #cccccc;}");
out.println("table.passed td,table tr.skippedodd td {background-color: #dddddd;}");
out.println("table.failed tr.stripe td,table tr.failedodd td,table.param td.numi_attn {background-color: #FF3333;}");
out.println("table.failed td,table tr.failedeven td,table.param tr.stripe td.numi_attn {background-color: #DD0000;}");
out.println("tr.stripe td,tr.stripe th {background-color: #E6EBF9;}");
out.println("p.totop {font-size:85%;text-align:center;border-bottom:2px black solid}");
out.println("div.shootout {padding:2em;border:3px #4854A8 solid}");
out.println("</style>");
out.println("</head>");
out.println("<body>");
}
/** Finishes HTML stream */
protected void endHtml(PrintWriter out) {
out.println("</body></html>");
}
// ~ Inner Classes --------------------------------------------------------
/** Arranges methods by classname and method name */
private class TestSorter<T extends ITestNGMethod> implements Comparator {
// ~ Methods -------------------------------------------------------------
/** Arranges methods by classname and method name */
public int compare(Object o1, Object o2) {
int r = ((T) o1).getTestClass().getName().compareTo(((T) o2).getTestClass().getName());
if (r == 0) {
r = ((T) o1).getMethodName().compareTo(((T) o2).getMethodName());
}
return r;
}
}
}
My testng.xml is:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1">
<listeners>
<listener class-name="com.company.common.EmailableReporter" />
</listeners>
<test name="SanityTest">
<classes>
<class name="com.company.tests.PresenceTest" />
</classes>
</test>
</suite>
Can somebody help me to make it work.
Related
array output showing up as null
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package freewill_project; import java.util.Arrays; import javax.swing.JOptionPane; /** * * #author /// */ public class Testing { public static void main(String[] args) { int batas = Integer.parseInt(JOptionPane.showInputDialog("Batas Siswa")); String[] nama = new String[batas]; String mapel1, mapel2; int nilai1[] = new int[batas]; int nilai2[] = new int[batas]; int total1 = 0, total2 = 0, Ttotal; int kkm1, kkm2, k=0; String[] tlulus1 = new String[nama.length]; String[] lulus1 = new String[nama.length]; String[] tlulus2 = new String[nama.length]; String[] lulus2 = new String[nama.length]; int jumlah = 0; mapel1 = JOptionPane.showInputDialog("Masukan Mapel 1"); mapel2 = JOptionPane.showInputDialog("Masukan Mapel 2"); kkm1 = (Integer.parseInt(JOptionPane.showInputDialog("Input KKM 1"))); kkm2 = (Integer.parseInt(JOptionPane.showInputDialog("Input KKM 2"))); for (int i = 0; i < nama.length; i++) { nama[i] = (JOptionPane.showInputDialog("Nama Siswa")); nilai1[i] = Integer.parseInt(JOptionPane.showInputDialog("Input Nilai")); total1 += nilai1[i]; if (nilai1[i] < kkm1) { tlulus1[i] = nama[i]; } else { nama[i] = lulus1[i]; } nilai2[i] = Integer.parseInt(JOptionPane.showInputDialog("Input Nilai")); total2 += nilai2[i]; if (nilai2[i] < kkm2) { nama[i] = tlulus2[i]; } else { nama[i] = lulus2[i]; } } Ttotal = total1 + total2; int rata = Ttotal / 2; int max1 = nilai1[0]; int min1 = nilai1[0]; int max2 = nilai2[0]; int min2 = nilai2[0]; for (int i = 0; i < 2; i++) { //for untuk output max, min, jumlah, rata arrays if (max1 < nilai1[i]) { max1 = nilai1[i]; } else if (min1 > nilai1[i]) { min1 = nilai1[i]; } if (max2 < nilai2[i]) { max2 = nilai2[i]; } else if (min2 > nilai2[i]) { min2 = nilai2[i]; } } System.out.println("Nilai Rata: " + rata + "\nNilai Tertinggi1: " + max1 + "\nNilai Terendah1: " + min2 + "\nNilai Tertinggi2: " + max2 + "\nNilai Terendah2: " + min2 + "\nSiswa Lulus1: " + Arrays.toString(lulus1) + "\nSiswa Tidak Lulus1: " + Arrays.toString(tlulus1) + "\nSiswa Lulus2: " + Arrays.toString(lulus2) + "\nSiswa Tidak Lulus2: " + Arrays.toString(tlulus2)); } } if the nilai is less than the kkm, then an element of the array in nama will be copied to tlulus, else an element in nama will be copied to lulus, however the output shows both lulus and tlulus as null the output of lulus and tlulus should contain elements of nama, it showed up as null instead, I tried several fixes but then I get Arrays.outofbounds exception, any solution?
Your code } else { nama[i] = lulus1[i]; } does not seem to match your specification: else an element in nama will be copied to lulus Shouldn't it rather be } else { lulus1[i] = nama[i]; } ? Your code can lead to nama inputs being overwritten with null.
get the values of the attribute userparameters from active directory
I need to get the values of the userparameters attribute. I use this code to connect to ldap, and with filters get the information I need. The problem is that userparameters contains values of several types, and you can not get them in the usual way, otherwise you will see this: {userparameters=userParameters: PCtxCfgPresent????CtxCfgFlags1????CtxShadow????.CtxMaxDisconnectionTime????CtxMaxIdleTime????*CtxMinEncryptionLevel? In this question in c #, the solution to my problem is described, IADsTSUserEx saves considerable time and makes the code less. Is there a similar solution on java? After much searching, I did not find anything. Thank you.
I finally managed to get my piece of Java code running to fulfill this need. It's a direct port from this Powershell script : https://gist.github.com/HarmJ0y/08ee1824aa555598cff5efa4c5c96cf0 import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.security.Security; import java.util.Hashtable; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.directory.Attribute; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import javax.naming.ldap.Control; import javax.naming.ldap.InitialLdapContext; import javax.naming.ldap.PagedResultsControl; import javax.naming.ldap.PagedResultsResponseControl; public class LDAP { public static void main(String[] args) { Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); InitialLdapContext ldapContext = null; Hashtable ldapEnv = new Hashtable(11); ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); ldapEnv.put(Context.PROVIDER_URL, "ldaps://HOST:636"); ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl"); ldapEnv.put(Context.SECURITY_PRINCIPAL, "ADMIN"); ldapEnv.put(Context.SECURITY_CREDENTIALS, "PASSWORD"); ldapEnv.put("java.naming.ldap.attributes.binary", "objectGUID"); byte[] cookie = null; String baseName = "BASENAME"; try { ldapContext = new InitialLdapContext(ldapEnv, new Control[] { new PagedResultsControl(1, Control.NONCRITICAL) }); SearchControls ctls = new SearchControls(); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); ctls.setCountLimit(1); String[] attrs = new String[] { "objectGUID", "cn", "userParameters" }; ctls.setReturningAttributes(attrs); NamingEnumeration<SearchResult> e = ldapContext.search(baseName, "(&(objectclass=user)(cn=USERCN))", ctls); if (e.hasMore()) { SearchResult sr = e.next(); Attribute attr = sr.getAttributes().get("cn"); if (attr.get() != null) { NamingEnumeration<?> neAttr = attr.getAll(); while (neAttr.hasMoreElements()) { String val = neAttr.next().toString(); System.out.println(val); } } attr = sr.getAttributes().get("userParameters"); if (attr.get() != null) { NamingEnumeration<?> neAttr = attr.getAll(); while (neAttr.hasMoreElements()) { String v = neAttr.next().toString(); System.out.println("-------------"); byte[] val = v.getBytes(StandardCharsets.UTF_16LE); ByteBuffer buffer = ByteBuffer.wrap(val); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.position(96); char signature = buffer.getChar(); System.out.println("Signature : " + signature); if (signature == 'P') { int nbAttrs = (int) buffer.getChar(); System.out.println("nbAttrs : " + nbAttrs); for (int i = 0; i < nbAttrs; i++) { System.out.println("\n---- Attribute " + i + " ----"); int nameLength = (int) buffer.getChar(); System.out.println("nameLength : " + nameLength); int valueLength = (int) buffer.getChar(); System.out.println("valueLength : " + valueLength); int type = (int) buffer.getChar(); System.out.println("type : " + type); byte[] attrNameTab = new byte[nameLength]; buffer.get(attrNameTab); String attrName = new String(attrNameTab, StandardCharsets.UTF_16LE); System.out.println("attrName : " + attrName); byte[] attrValue = new byte[valueLength]; buffer.get(attrValue); if (attrName.matches("CtxCfgPresent|CtxCfgFlags1|CtxCallBack|CtxKeyboardLayout|CtxMinEncryptionLevel|CtxNWLogonServer|CtxMaxConnectionTime|CtxMaxDisconnectionTime|CtxMaxIdleTime|CtxShadow")) { String valueStr = ""; for (byte b : attrValue) { valueStr += (char) b; } Integer valueInt = Integer.parseUnsignedInt(valueStr, 16); System.out.println("attrValue : " + valueInt); if (attrName.matches("CtxShadow")) { switch (valueInt) { case 0x0: System.out.println(" -> Disable"); break; case 0x1000000: System.out.println(" -> EnableInputNotify"); break; case 0x2000000: System.out.println(" -> EnableInputNoNotify"); break; case 0x3000000: System.out.println(" -> EnableNoInputNotify"); break; case 0x4000000: System.out.println(" -> EnableNoInputNoNotify"); break; } } else if (attrName.matches("CtxCfgFlags1")) { // this field is represented as a bitmask for (CtxCfgFlagsBitValues en : CtxCfgFlagsBitValues.values()) { if (en.isBit(valueInt)) { System.out.println(" -> " + en.name()); } } } else if (attrName.matches("CtxMaxConnectionTime|CtxMaxDisconnectionTime|CtxMaxIdleTime")) { for (CtxCfgFlagsTimeValues en : CtxCfgFlagsTimeValues.values()) { if (en.getValue() == valueInt.intValue()) { System.out.println(" -> " + en.name()); } } } } else if (attrName.matches("CtxWFHomeDirDrive.*|CtxWFHomeDir.*|CtxWFHomeDrive.*|CtxInitialProgram.*|CtxWFProfilePath.*|CtxWorkDirectory.*|CtxCallbackNumber.*")) { String str = new String(attrValue, StandardCharsets.US_ASCII); String valueStr = convertHexToString(str); valueStr = valueStr.substring(0, valueStr.length() - 1); if (attrName.matches(".*W$")) { // handle wide strings valueStr = new String(new String(valueStr.getBytes(), StandardCharsets.US_ASCII).getBytes(),StandardCharsets.UTF_16LE); valueStr = valueStr.substring(0, valueStr.length() - 1); } System.out.println("attrValue : " + valueStr); } } } } } Control[] controls = ldapContext.getResponseControls(); if (controls != null) { for (int i = 0; i < controls.length; i++) { if (controls[i] instanceof PagedResultsResponseControl) { PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i]; cookie = prrc.getCookie(); } else { // Handle other response controls (if any) } } } ldapContext.setRequestControls(new Control[] { new PagedResultsControl(1, cookie, Control.CRITICAL) }); } while (cookie != null); } catch (Exception e) { e.printStackTrace(); } finally { if (ldapContext != null) { try { ldapContext.close(); } catch (Exception e1) { } } } } public static String convertHexToString(String hex) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < hex.length() - 1; i += 2) { // grab the hex in pairs String output = hex.substring(i, (i + 2)); // convert hex to decimal int decimal = Integer.parseInt(output, 16); // convert the decimal to character sb.append((char) decimal); } return sb.toString(); } private static enum CtxCfgFlagsBitValues { INHERITCALLBACK(0x08000000), INHERITCALLBACKNUMBER(0x04000000), INHERITSHADOW(0x02000000), INHERITMAXSESSIONTIME(0x01000000), INHERITMAXDISCONNECTIONTIME(0x00800000), INHERITMAXIDLETIME(0x00400000), INHERITAUTOCLIENT(0x00200000), INHERITSECURITY(0x00100000), PROMPTFORPASSWORD(0x00080000), RESETBROKEN(0x00040000), RECONNECTSAME(0x00020000), LOGONDISABLED(0x00010000), AUTOCLIENTDRIVES(0x00008000), AUTOCLIENTLPTS(0x00004000), FORCECLIENTLPTDEF(0x00002000), DISABLEENCRYPTION(0x00001000), HOMEDIRECTORYMAPROOT(0x00000800), USEDEFAULTGINA(0x00000400), DISABLECPM(0x00000200), DISABLECDM(0x00000100), DISABLECCM(0x00000080), DISABLELPT(0x00000040), DISABLECLIP(0x00000020), DISABLEEXE(0x00000010), WALLPAPERDISABLED(0x00000008), DISABLECAM(0x00000004); private int mask; CtxCfgFlagsBitValues(int mask) { this.mask = mask; } public boolean isBit(int val) { return ((val & mask) == mask); } } private static enum CtxCfgFlagsTimeValues { ONE_MINUTE(1625948160), FIVE_MINUTES(-527236096), TEN_MINUTES(-1071183616), FIFTEEN_MINUTES(-1598354176), THIRTY_MINUTES(1081547520), ONE_HOUR(-2131872256), TWO_HOURS(14511360), THREE_HOURS(-2134137856), ONE_DAY(6039045), TWO_DAYS(12078090); private int value; CtxCfgFlagsTimeValues(int value) { this.value = value; } public int getValue() { return value; } } }
Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
import java.util.Random; import java.util.StringTokenizer; public class FortuneCookie { private String subjList; private String objList; private String verbList; private int sWords = 0; private int oWords = 0; private int vWords = 0; private Random random = new Random(); public FortuneCookie() { subjList = "i#You#He#She#It#They"; objList = "me#you#him#her#it#them"; verbList = "hate#love#deny#find#hear#forgive#hurt#win#teach"; } public void setSubject(String subj) { subjList = subj; } public void setObjectList(String obj) { objList = obj; } public void setVerbList(String verb) { verbList = verb; } public String genFortuneMsg() { String v = " "; String o = " "; String s = " "; StringTokenizer st1 = new StringTokenizer(subjList, "#"); StringTokenizer st2 = new StringTokenizer(objList, "#"); StringTokenizer st3 = new StringTokenizer(verbList, "#"); while (st1.hasMoreTokens()) { s = st1.nextToken(); sWords = st1.countTokens(); int no = random.nextInt(sWords); if (no == sWords) { break; } } while (st2.hasMoreTokens()) { o = st2.nextToken(); oWords = st2.countTokens(); int no2 = random.nextInt(oWords); if (no2 == oWords) { break; } } while (st3.hasMoreTokens()) { v = st3.nextToken(); vWords = st3.countTokens(); int no3 = random.nextInt(vWords); if (no3 == vWords) { break; } } String gen = s + " " + v + " " + o; return gen; } public void print() { System.out.println("Tokens"); System.out.println("Subject List:" + subjList + " count = " + sWords); System.out.println("verb List:" + verbList + " count = " + vWords); System.out.println("object List:" + objList + " count = " + oWords); } } Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive at java.util.Random.nextInt(Random.java:388) at FortuneCookie.genFortuneMsg(FortuneCookie.java:42) at FortuneCookieTest.main(FortuneCookieTest.java:6)
Your case is not negative it is zero. From the docs of countToken method /** * Calculates the number of times that this tokenizer's * <code>nextToken</code> method can be called before it generates an * exception. The current position is not advanced. * In a while loop when your count token return zero, you run into an exception. Well that error message should be reformatted to negative or zero. Add 1 to your result or check for zero. Should work.
Android SQLite cursor returns 33 results, but debug only outputs 17
Hi folks I've got a strange cade. I'm trying to debug the SQLite DB in an app. If I do a query SELECT * from table I get 33 results, but if I iterate over the cursor it ends at result #17. Here's my debug class (the method in question is public static void WriteToFile(Cursor cursor, String query , String tables, String uri)) : package com.s2u.android.ps; import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Environment; import android.util.Log; import com.s2u.android.ps.BO.App; import com.s2u.android.ps.BO.AppMember; import com.s2u.android.ps.datamodel.DatabaseManager; import com.s2u.android.ps.networkApis.AndroidLog; import com.s2u.android.ps.networkApis.AppConfig; public class DebugToFile { private static String TAG = "DebugToFile"; private static File path = new File(Environment.getExternalStorageDirectory() + "/ps_debug"); public static void WriteToFile(String lines , String tag) { WriteToFile(lines , tag , "txt"); } public static void WriteToFile(String lines , String tag , String ext) { if (!Validate("WriteToFile(String lines)")) return; File file = new File(path, tag + "_" + GetDateStampTens() + "." + ext); try { FileWriter fw = new FileWriter(file.getAbsolutePath() , true); PrintWriter pw = new PrintWriter(fw); pw.println(GetDateStamp() + " - " + lines); pw.println(); pw.flush(); pw.close(); //Log.e(TAG, "WriteToFile(String lines) - " + file.toString()); } catch (Exception e) { Log.e(TAG, "WriteToFile(String lines) failed!", e); } } public static void WriteToFileAppMember(ArrayList<AppMember> appMembers , String tag) { if (!Validate("WriteToFile(AppMember)")) return; File file = new File(path, tag + "_" + GetDateStampTens() + ".csv"); try { FileWriter fw = new FileWriter(file.getAbsolutePath() , true); PrintWriter pw = new PrintWriter(fw); pw.println(GetDateStamp() + " - " + "AppMembers"); boolean doOnce = true; for(com.s2u.android.ps.BO.AppMember appMember : appMembers) { if (doOnce) { doOnce = false; pw.println(appMember.getCsvLabels()); } pw.println(appMember.getCsvString()); } pw.println(); pw.flush(); pw.close(); //Log.e(TAG, "WriteToFile(String lines) - " + file.toString()); } catch (Exception e) { Log.e(TAG, "WriteToFile(String lines) failed!", e); } } public static void WriteToFileAppMember(List<AppMember> appMembers , String tag) { if (!Validate("WriteToFile(AppMember)")) return; File file = new File(path, tag + "_" + GetDateStampTens() + ".csv"); try { FileWriter fw = new FileWriter(file.getAbsolutePath() , true); PrintWriter pw = new PrintWriter(fw); pw.println(GetDateStamp() + " - " + "AppMembers"); boolean doOnce = true; for(com.s2u.android.ps.BO.AppMember appMember : appMembers) { if (doOnce) { doOnce = false; pw.println(appMember.getCsvLabels()); } pw.println(appMember.getCsvString()); } pw.println(); pw.flush(); pw.close(); //Log.e(TAG, "WriteToFile(String lines) - " + file.toString()); } catch (Exception e) { Log.e(TAG, "WriteToFile(String lines) failed!", e); } } public static void WriteToFileApps(List<App> apps , String tag) { if (!Validate("WriteToFile(AppMember)")) return; File file = new File(path, tag + "_" + GetDateStampTens() + ".csv"); try { FileWriter fw = new FileWriter(file.getAbsolutePath() , true); PrintWriter pw = new PrintWriter(fw); pw.println(GetDateStamp() + " - " + "App objects"); boolean doOnce = true; for(com.s2u.android.ps.BO.App app : apps) { if (doOnce) { doOnce = false; pw.println(app.getCsvLabels()); } pw.println(app.getCsvString()); } pw.println(); pw.flush(); pw.close(); //Log.e(TAG, "WriteToFile(String lines) - " + file.toString()); } catch (Exception e) { Log.e(TAG, "WriteToFile(String lines) failed!", e); } } public static void WriteToFile(Cursor cursor, String query , String tables, String uri) { if (!Validate("WriteToFile(cursor)")) return; File file = new File(path, uri + "_" + GetDateStampTens() + ".csv"); try { FileWriter fw = new FileWriter(file.getAbsolutePath(), true); PrintWriter pw = new PrintWriter(fw); int resultCount = cursor.getCount(); pw.println("time: " + GetDateStamp()); pw.println("tables: " + tables); pw.println("query: " + query); pw.println("result count: " + Integer.toString(resultCount)); int row = 0; String labels = "row,"; int startPosition = cursor.getPosition(); cursor.moveToPosition(-1); while (cursor.moveToNext()) { int colCount = cursor.getColumnCount(); row++; if (row >= resultCount) { pw.println("Error! rows >= cursor count -- at row : " + Integer.toString(row) ); break; } StringBuilder line = new StringBuilder(512); if (colCount <= 0) pw.println("Empty row?"); for(int i = 0; i < colCount; i++) { if (row == 1) { labels += cursor.getColumnName(i) + "[" + GetCursorFieldTypeString(cursor, i) + "]"; if (i < colCount - 1) labels += ","; } if (i == 0) line.append(Integer.toString(row) + ","); line.append(GetCursorString(cursor, i)); if (i < colCount - 1) { line.append(","); } } if (row == 1) pw.println(labels); pw.println(line.toString()); cursor.moveToNext(); if (row > 100) { pw.println("max rows output - stopped at row: " + Integer.toString(row)); break; } } pw.println("END"); pw.println(); pw.flush(); pw.close(); //Log.e(TAG, "WriteToFile(cursor) - " + file.toString()); cursor.moveToPosition(startPosition); } catch (Exception e) { Log.e(TAG, "WriteToFile(cursor) failed!", e); } } private static boolean Validate(String methodName) { if (!AppConfig.isTestBuild()) { Log.i(TAG, methodName + " - this is not a test build!"); return false; } if (!isExternalStorageWritable()) { AndroidLog.e(TAG, methodName + " - external storage not accessible"); return false; } if (!path.exists()) { path.mkdir(); if (!path.exists()) { AndroidLog.e(TAG, methodName + " - directory doesn't exist and couldn't create it: " + path.toString()); return false; } } return true; } private static String GetDateStamp() { Calendar c = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-kk:mm:ss.SSS"); String date = df.format(c.getTime()); return date; } private static String GetDateStampTens() { String date = GetDateStamp(); date = date.substring(0,date.length() - 1) + "0"; return date; } private static boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; } private static String GetCursorString(Cursor cursor, Integer i) { String result = "undefined"; switch(cursor.getType(i)) { case Cursor.FIELD_TYPE_NULL: result = "NULL"; break; case Cursor.FIELD_TYPE_BLOB: result = "BLOB length: " + Integer.toString(cursor.getBlob(i).length); break; case Cursor.FIELD_TYPE_FLOAT: result = Float.toString(cursor.getFloat(i)); break; case Cursor.FIELD_TYPE_INTEGER: result = Integer.toString(cursor.getInt(i)); break; case Cursor.FIELD_TYPE_STRING: result = cursor.getString(i); break; default: result = "undefined cursor value type(" + Integer.toString(cursor.getType(i)) + ") -- try getString: " + cursor.getString(i); } result.replace("", " "); return result; } private static String GetCursorFieldTypeString(Cursor cursor, Integer i) { String result = "UNK"; switch(cursor.getType(i)) { case Cursor.FIELD_TYPE_NULL: result = "NULL"; break; case Cursor.FIELD_TYPE_BLOB: result = "BLOB"; break; case Cursor.FIELD_TYPE_FLOAT: result = "F"; break; case Cursor.FIELD_TYPE_INTEGER: result = "INT"; break; case Cursor.FIELD_TYPE_STRING: result = "STR"; break; default: result = "UNK(" + Integer.toString(cursor.getType(i)) + ") "; } return result; } public static String AppListTypeToString(int appListType) { if (appListType == 0) return "kAppListMain"; else if (appListType == 1) return "kAppListProfile"; else if (appListType == 2) return "kAppListPromoted"; return "unknown list type int: " + Integer.toString(appListType); } public static void DumpDatabaseToFiles(DatabaseManager db) { SQLiteDatabase readableDb = db.getReadableDatabase(); DumpDatabaseToFiles(readableDb); } public static void DumpDatabaseToFiles(SQLiteDatabase db) { if (!Validate("DumpDatabaseToFiles")) return; Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); if (c.getCount() <= 0) { WriteToFile("table name count: " + Integer.toString(c.getCount()) , "dbdump_err"); c.close(); return; } //AndroidLog.i(TAG , "DumpDB table count: " + Integer.toString(c.getCount())); List<String> tableNames = new ArrayList<String>(); if (c.moveToFirst()) { while(!c.isAfterLast()) { tableNames.add(c.getString(c.getColumnIndex("name"))); c.moveToNext(); } } c.close(); for (int i = 0; i < tableNames.size(); i++) { String table = tableNames.get(i); c = db.rawQuery("SELECT * FROM " + table + " LIMIT 100 ", null); WriteToFile(c, "all" , table, table); c.close(); } } } The output csv file is: tables: app - from AppDAO.bulkInsertApp query: SELECT * FROM app result count: 33 row,_id[INT],packageName[STR],appName[STR],iconUrl1[STR],iconUrl2[NULL],publisher[STR],publisherEmail[NULL],price[INT],currency[STR],version[STR],category[STR],releaseDate[NULL],updatedOn[NULL],hasTried[INT],promo_url[NULL],promoParam[NULL],promoValueKey[NULL] 1,8192,com.shared2you.android.powerslyde,Powerslyde,https://lh5.ggpht.com/1qigt9Zz7oh5kTFiIS9ukJljVTm7W-Ur34XzcaQhFjc9GlMzATJ-ATRwYB6gxQhscHEU=w300,NULL,Shared2you, Inc.,NULL,0,, 1.08 ,Lifestyle,NULL,NULL,1,NULL,NULL,NULL 2,8219,com.android.providers.downloads.ui,com.android.providers.downloads.ui,NULL,NULL,NULL,NULL,NULL,,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL 3,8225,com.google.android.apps.maps,Maps,https://lh3.ggpht.com/JW-F0fkeBHpKyh8lDcyQ7CveTRynYGByVBH9hUqnJxw4x64ORhoFJISdOWhekULemw0=w300,NULL,Google Inc.,NULL,0,, Varies with devic,Travel & Local,NULL,NULL,1,NULL,NULL,NULL 4,8231,com.android.vending,com.android.vending,NULL,NULL,NULL,NULL,NULL,,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL 5,8246,com.google.android.apps.magazines,Google Play Newsstand,https://lh5.ggpht.com/rowOPaiODov-bNG7rnD6awPZwLnOc7Vzab-29GpfvB6jfE8DhOR42owBqAmLUXj-W2sI=w300,NULL,Google Inc.,NULL,0,, 3.1.0 ,News & Magazines,NULL,NULL,1,NULL,NULL,NULL 6,8248,com.google.android.gm,Gmail,https://lh4.ggpht.com/Ebn-CW55BnkwG7ng5nuGpijVpJeabTa-uPijd4keKbHpedz29SvDj3EZkfr20ZZzznE=w300,NULL,Google Inc.,NULL,0,, Varies with devic,Communication,NULL,NULL,1,NULL,NULL,NULL 7,8250,com.google.android.music,Google Play Music,https://lh6.ggpht.com/5opWBg-m6yFcjWzJz1LlT05YIf2Alyiy9YtpQm1f6U42LXWmCvB54M1zEkV9-hCaoTc=w300,NULL,Google Inc.,NULL,0,, Varies with devic,Music & Audio,NULL,NULL,1,NULL,NULL,NULL 8,8253,com.google.android.videos,Google Play Movies & TV,https://lh5.ggpht.com/fFPQTALNNU4xflvbazvbwPL5o4X3a_CqYHUWIh4FXmfU78aSSuP1OMkGXhXouxXzWPov=w300,NULL,Google Inc.,NULL,0,, Varies with devic,Media & Video,NULL,NULL,1,NULL,NULL,NULL 9,8312,com.android.chrome,Chrome Browser - Google,https://lh6.ggpht.com/lum4KYB0TtgvR-8vRMUZ_JhRnMQ4YqBIR0yjspc4ETsM9iJ8-4YHZ0s0HO9i0ez_=w300,NULL,Google Inc.,NULL,0,, Varies with devic,Communication,NULL,NULL,1,NULL,NULL,NULL 10,8316,com.google.android.calendar,Google Calendar,https://lh5.ggpht.com/qgUPYBPSTb61cPrijI9YXV3BEy00t5bhoBugDpEXTdEsQEv9B9-j8_ZDs_ClQzPbskc=w300,NULL,Google Inc.,NULL,0,, 201308023 ,Productivity,NULL,NULL,1,NULL,NULL,NULL 11,8433,com.estrongs.android.pop,ES File Explorer File Manager,https://lh5.ggpht.com/P31CiAbF5UMC1wbJxv2sPT4tSLLqfqUZPp8N0ATEaA0ZeMxXv_NjVDiswVKjeUUSS2w=w300,NULL,ES APP Group,NULL,0,, Varies with devic,Productivity,NULL,NULL,1,NULL,NULL,NULL 12,8867,com.devhd.feedly,Feedly,https://lh4.ggpht.com/rkouDgWbT3WNztDRa5QvnN8SatDK3zeHHwOMHZbiu2Vlf3-9hLlmH89W9gJpGEtxo3U=w300,NULL,Feedly Team,NULL,0,, 18.1.2 ,News & Magazines,NULL,NULL,1,NULL,NULL,NULL 13,8917,com.google.android.email,com.google.android.email,NULL,NULL,NULL,NULL,NULL,,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL 14,12113,com.google.android.play.games,Google Play Games,https://lh5.ggpht.com/tkg8ndU21RjzO5WSz7JRpYJ35P-oDTm0md2sNwvVoBtQ0kE_ORHhorrzQWcjVTevxP8_=w300,NULL,Google Inc.,NULL,0,, 1.1.04 ,Entertainment,NULL,NULL,1,NULL,NULL,NULL 15,87853,com.google.android.apps.docs.editors.sheets,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL 16,87862,com.google.android.apps.photos,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL 17,87867,com.umfersolutions.eatthiszombies,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,1,NULL,NULL,NULL END Thanks!
You are advancing the cursor position two times, one in while (cursor.moveToNext()) and the other one at the end of the loop in pw.println(line.toString()); cursor.moveToNext(); Thats why you always will get half of the results, since at the end you move it one position, and then at then when checking the while condition it will advance again, so its reading position 0, then position 2, then 4...and so on...
Duplicate cursor.moveToNext() in the loop: while (cursor.moveToNext()) { ... pw.println(line.toString()); cursor.moveToNext(); ... }
JPA wrapper to convert unchecked exceptions into checked exceptions?
Before I wander off and re-create the wheel, does anyone know of a JPA wrapper that turns the unchecked exceptions JPA throws into checked exceptions? Not looking for an argument about why I should not want checked exceptions, I do want them :-)
You can do 99% of rolling your own using the code below. It will leave you with two compile errors due to the members of a Collection needing to be encapsulated, but it does take you past the point where it is no longer worth automating. package so; import javax.persistence.*; import java.io.File; import java.io.FileWriter; import java.lang.reflect.*; public class CheckedPersistenceMaker { static final String PACKAGE = "stackoverflow.javax.persistence"; static final String PACKAGE_DIR = PACKAGE.replace('.', '/'); static Class<?>[] CLASSES = new Class<?>[] { Cache.class, EntityManager.class, EntityManagerFactory.class, EntityTransaction.class, Parameter.class, PersistenceUnitUtil.class, PersistenceUtil.class, Persistence.class, Query.class, Tuple.class, TupleElement.class, TypedQuery.class }; private static String getName(Class<?> c) { String name = c.getName(); for(Class<?> p:CLASSES) { if (p.equals(c)) return PACKAGE + ".Checked" + name.substring(name.lastIndexOf('.') + 1); } return c.getName(); } static void generateWrapper(Class<?> c) throws Exception { String name = c.getName(); TypeVariable<?>[] genType = c.getTypeParameters(); String varNames = ""; if (genType != null && genType.length != 0) { StringBuilder b = new StringBuilder(); b.append("<"); for(int i = 0;i < genType.length;i++) { if (i > 0) b.append(","); b.append(genType[i].getName()); } b.append(">"); varNames = b.toString(); } name = "Checked" + name.substring(name.lastIndexOf('.') + 1); File javaFile = new File(PACKAGE_DIR + "/" + name + ".java"); javaFile.getParentFile().mkdirs(); FileWriter w = new FileWriter(javaFile); w.write("package " + PACKAGE + ";\n"); w.write("public class " + name + varNames + " {\n"); w.write(" private final " + c.getName() + varNames + " wrapped;\n\n "); w.write(name + "(" + c.getName() + varNames + " original) {\nwrapped=original;\n}\n\n"); w.write(" public " + c.getName() + varNames + " getOriginal() { return wrapped; }\n\n"); Method[] ms = c.getDeclaredMethods(); for(Method m:ms) { if (!Modifier.isPublic(m.getModifiers())) continue; w.write(" "); String s = m.toGenericString(); s = s.replace(" abstract ", " "); s = s.replace(c.getName() + ".", ""); String q = s.substring(0, s.indexOf('(')); if (q.indexOf('<',10) != -1) q = q.substring(0, q.indexOf('<',10)); boolean needsTranslate = false; for(Class<?> cc:CLASSES) { String ccn = cc.getName(); if (q.indexOf(ccn) != -1) needsTranslate = true; String ccc = ccn.replace(cc.getPackage().getName() + ".", PACKAGE + ".Checked"); s = s.replace(ccn, ccc); } int pc = 0; int p = s.indexOf('('); if (s.charAt(p + 1) != ')') { StringBuilder b = new StringBuilder(s); int g = 0; char ch; do { ch = b.charAt(p); switch (ch) { case '<': { g++; break; } case '>': { g--; break; } case ',': case ')': { if (g == 0) { String pa = " p" + pc; b.insert(p, pa); pc++; p += pa.length(); } break; } } p++; } while( ch != ')' ); s = b.toString(); } w.write(s); w.write(" throws CheckedPersistenceException"); Class<?>[] excs = m.getExceptionTypes(); for(Class<?> e:excs) { w.write(", " + e.getName()); } w.write(" {\n try {\n "); Class<?> ret = m.getReturnType(); if (!ret.equals(Void.TYPE)) { w.write("return "); if (needsTranslate) { String retName = ret.getName(); retName = retName.replace(c.getPackage().getName() + ".", PACKAGE + ".Checked"); w.write("new " + retName + "("); } } if (Modifier.isStatic(m.getModifiers())) { w.write(c.getName() + "." + m.getName() + "("); } else { w.write("wrapped." + m.getName() + "("); } Class<?> paramTypes[] = m.getParameterTypes(); for(int i = 0;i < pc;i++) { if (i > 0) w.write(','); boolean isChecked = false; for(int j=0;j<CLASSES.length;j++) { if( CLASSES[j].equals(paramTypes[i]) ) isChecked=true; } w.write("p" + i); if(isChecked) w.write(".getOriginal()"); } w.write(')'); if (needsTranslate) w.write(')'); w.write(";\n } catch ( javax.persistence.PersistenceException e ) { throw new CheckedPersistenceException(e); }\n }\n\n"); } w.write("}\n"); w.close(); } public static void main(String[] args) throws Exception { for(Class<?> c:CLASSES) { generateWrapper(c); } } }