{"id":24901746,"url":"https://github.com/antonsjava/tostringer","last_synced_at":"2025-03-27T18:19:51.639Z","repository":{"id":57732426,"uuid":"85050368","full_name":"antonsjava/tostringer","owner":"antonsjava","description":"Helper library for structured tostrings","archived":false,"fork":false,"pushed_at":"2021-12-15T20:21:25.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-01T21:17:26.514Z","etag":null,"topics":["java","tostring"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antonsjava.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-15T08:55:55.000Z","updated_at":"2021-12-15T20:21:28.000Z","dependencies_parsed_at":"2022-09-10T20:31:12.695Z","dependency_job_id":null,"html_url":"https://github.com/antonsjava/tostringer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonsjava%2Ftostringer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonsjava%2Ftostringer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonsjava%2Ftostringer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonsjava%2Ftostringer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antonsjava","download_url":"https://codeload.github.com/antonsjava/tostringer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245898314,"owners_count":20690466,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","tostring"],"created_at":"2025-02-01T21:17:35.337Z","updated_at":"2025-03-27T18:19:51.615Z","avatar_url":"https://github.com/antonsjava.png","language":"Java","readme":"\n# ToStringer\n\nToStringer is helper class for generating structured toString representations\nfor classes you don't have in control. It is possible to use ToStringer also for \nyour own classes. If you want to have simple toStrings and only wants to have \nstructured output only for some debug purposes. \n\n## ClassProperty\n\nThis class represents class porperty and ability to resolve the property from \ngiven object.\n\nThere are provided two implementations MethodClassProperty and FieldClassProperty\nwhich resolves properties using public members of java class.\n\n## ClassPropertyResolver\n\nResolves list of class properties for given class. There is provided implementation \nFieldScanClassPropertyResolver which scan class for public properties or public getters \nfor thyis properties.\n\nClass implementation limits this scanning only for classes which names start by \nspecified list of prefixes.\n\n## ToStringerFactory\n\nThe class produces commonly used ToStringer instamces. \n\n```java\npublic class PrettyPrinter {\n    private static ToStringer tostringer = \n\t\tToStringerFactory.simpleField(\"org.foo.client\", \"org.foo.server\");\n    \n    public static String toString(Object object) {\n        return tostringer.toString(object);\n    }    \n}\n```\n\n## Example\n\nThis example shows how to create class PrettyPrint which uses ToStringer for classes \nfrom tvo packages and for Element class.\n\n```java\n\npublic class PrettyPrinter {\n    private static ToStringer tostringer = new ToStringer(\n        new ClassPropertyResolver[] {\n            new FieldScanClassPropertyResolver(\n                \"org.foo.client.message\"\n                , \"org.foo.client.responses\"\n            )\n            , new ElementClassPropertyResolver()\n        }\n    );\n    \n    public static String toString(Object object) {\n        return tostringer.toString(object);\n    }    \n\n    public static class ElementClassPropertyResolver implements ClassPropertyResolver {\n\n        public boolean isSupportedClass(Class type) {\n            if(type == null) return false;\n            return Element.class.isAssignableFrom(type);\n        }\n        \n        private static ClassProperty[] props = new ClassProperty[]{\n            new ElementClassProperty(\"element\")\n            , new ElementClassProperty(\"attrs\")\n            , new ElementClassProperty(\"value\")\n            , new ElementClassProperty(\"subelement\")\n        };\n\n        public ClassProperty[] properties(Class type) {\n            return props;\n        }\n    }\n    \n    public static class ElementClassProperty extends ClassProperty {\n        public ElementClassProperty(String name) {\n            super(name);\n        }\n\n        @Override\n        public Object valueFrom(Object o) {\n            if(o == null) return null;\n            if(!(o instanceof Element)) return null;\n            Element elem = (Element)o;\n            String name = getName();\n            if(\"element\".equals(name)) {\n                return elem.getLocalName();\n            } else if(\"attrs\".equals(name)) {\n                NamedNodeMap nnmap = elem.getAttributes();\n                ...\n            } else if(\"subelement\".equals(name)) {\n                NodeList nl = elem.getChildNodes();\n                ...\n            } else if(\"value\".equals(name)) {\n                NodeList nl = elem.getChildNodes();\n                ...\n\t\t\t\t\t\t\t\t\t\t\t\t} else {\n                return null;\n            }\n        }\n\n    }\n}\n```\n## ToStackTracer\n\nThe class produces shorter stack traces. It is usefull whne you want to see \nonly own packages in stack trace.\n\n```java\npublic class PrettyPrinter {\n    private static ToStackTracer tostacktracer = \n\t\tToStackTracer.instance(\"org.foo\");\n    \n    public static String st(Throwable t) {\n        return tostacktracer.ts(t);\n    }    \n}\n```\nit produces stack traces like this where are visible org.foo packages and two line context\n```\n18:54:28 ERR - Unable to call get request to http://localhost:8080/mocka - /client_exist/6608271230 because of org.springframework.web.client.HttpClientErrorException: 404 null\n\tat org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)\n\tat org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)\n\tat org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)\n\tat ...\n\tat org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)\n\tat org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600)\n\tat org.foo.integration.common.rest.JsonRestClient.getRequest(JsonRestClient.java:48)\n\tat org.foo.integration.proj.client.impl.RestProjClient.clientExist(RestProjClient.java:53)\n\tat org.foo.integration.proj.controller.ProjController.sendApplication(ProjController.java:106)\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat ...\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat org.foo.integration.common.web.RRDumpFilter.doFilterInternal(RRDumpFilter.java:69)\n\tat org.foo.integration.common.web.RRDumpFilter.doFilterInternal(RRDumpFilter.java:54)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat ... \n```\n\n\n## Maven usage\n\n```\n   \u003cdependency\u003e\n      \u003cgroupId\u003eio.github.antonsjava\u003c/groupId\u003e\n      \u003cartifactId\u003etostringer\u003c/artifactId\u003e\n      \u003cversion\u003eLASTVERSION\u003c/version\u003e\n   \u003c/dependency\u003e\n```\n\n## OSGI usage (Karaf)\n\n```\n   bundle:install mvn:com.github.antonsjava/tostringer/1.2\n   bundle:start com.github.antonsjava.tostringer/1.2.0\n```\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantonsjava%2Ftostringer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantonsjava%2Ftostringer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantonsjava%2Ftostringer/lists"}