Get formatted output from eclipse template variable - java

How can I make an eclipse java template that allows generating java code that eases the repeating part of registering code for a java method. Example:
Assume that the class description is like so:
class A{
public static void methodName(String s, int i, Object o) {
}
}
Now, what I want is to make a template that does something somewhat like this:
"${enclosing_type}.${enclosing_method}(" + ${variable1} + ", " + ${variable2} + ", " + ${variable3} + ")"
Given the available Eclipse variables I know, the idea would probably be:
"${enclosing_type}.${enclosing_method}(" + ${enclosing_method_arguments(" + \", \" + ")} + ")"
Where that argument would signal the glue to the join of each element of the enclosing_method_arguments. The result of the format would be:
"A.methodName(" + s + ", " + i + ", " + o + ")"
If there's an even better alternative, I'm open for suggestions.
This is meant to be used with a piece of code that is executed a LOT,
Unfortunately, String.format() (and related) "solution" is not an option here due to the requirement above and due to other inherited requirements with what I'm working on. It must generate that code in that format no matter what, unfortunately.
I'm open to any plugins that allow that and, if eclipse doesn't have it, I'm open to make a plugin myself... In case of me having to do a plugin please do show me the resources required to make it.

It should be possible to write a custom variable resolver. It is defined by org.eclipse.ui.editors.templates extension point
You could implement a custom resolver that points to Java context (its id is java defined in jdt.ui).
I don't know any plugins that would provide what you need out of the box.
If you are new to Eclipse plug-ins you might need to read the documentation on extending Eclipse. However, the task is really simple, so you should be able to do it after just a glimpse over the docs.

Related

JDA doesn't support anymore attachments with downloadToFile()?

I write discord bot with using JDA and i have one important question how to download attachments or work with them? Because my IntelliJ say "Deprecated API usage" for method like attachment.downloadToFile("name.png);
So now we shouldn't download users files send in message? Or how we should do it in good way? I search a lot of wiki from JDA and different posts, but everywhere i didn't see, a new option to handle this download files, becasue all methods to download, are "Deprecated API" even method like "attachment.retrieveInputStream().join()" retrieveInputStream its too not good way :(
Search a lot on wiki/others pages for more information but nothing found :(
The deprecation notice says this:
Deprecated.
Replaced by getProxy(), see FileProxy.downloadToFile(File)
This means you should use attachment.getProxy().downloadToFile(File) instead.
Example:
attachment.getProxy().downloadToFile(new File("myimage.png")).thenAccept(file -> {
System.out.println("Written to file " + file.getAbsolutePath() + ".");
});
Or using NIO instead:
attachment.getProxy().downloadToPath().thenAccept(path -> {
System.out.println("Written to file " + path + ". Total size: " + Files.size(path));
});

ArchUnit class link in violation messages

I've noticed that using standard ArchConditions display messages in the following format:
Class <full_class_path> does not <some_rule> in (<class_link>)
However, this is not the case for custom conditions that add violation messages to the event like:
events.add(SimpleConditionEvent.violated(item, item.getName() + " some message"));
With this, no link to the violating class is appended to the message automatically. I wonder what the first argument (correspondingObject) is actually used for then.
Is this a bug in the framework or am I missing something? Having these links are really useful. I've tried using the JavaDoc #link notation in the message string to no avail.
Most of the domain classes like JavaClass, JavaMember or JavaAccess implement the interface HasSourceCodeLocation, which contains the method getSourceCodeLocation to obtain the location in the source code. The returned object SourceCodeLocation can then be used to output the location via the toString method.
For example
events.add(SimpleConditionEvent.violated(item, item.getDescription() + " is violated in " + item.getSourceCodeLocation()));
would return
Class <org.example.Dummy> is violated in (Dummy.java:0)

How to call a reified Java interface from a class in Clojure? Call can't be resolved

I am trying to translate some Java code directly into Clojure on a raspberry pi. I am stuck at implementing an interface in a method call - addListener.
I have tried using reify, proxy, and deftype. With reify I have tried providing as many hints to the compiler as possible.
This is the original Java code:
myButton.addListener(new GpioPinListenerDigital() {
#Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
System.out.println(" --> GPIO PIN STATE CHANGE: " + event.getPin() + " = " + event.getState());
}
});
And this is my translated Clojure code:
(.addListener myButton
(reify GpioPinListenerDigital
(^void handleGpioPinDigitalStateChangeEvent [this ^GpioPinDigitalStateChangeEvent event]
(println (str " --> GPIO PIN STATE CHANGE: " (.getPin event) " = " (.getState event))))))
I always end up with the same error:
IllegalArgumentException No matching method found: addListener for class com.pi4j.io.gpio.impl.GpioPinImpl clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:79)
I am not familiar with writing Java for raspberry pi, but looking at the javadoc we see the following declarations:
public void addListener(GpioPinListener... listener);
public void addListener(List<? extends GpioPinListener> listeners);
Both are accepting a multitude of listeners, not just one. In the Java example you provided above, java compiler turns the single listener instance into a singelton vector transparently, and uses the first definition shown above.
The clojure compiler does not know how to do that, you have to help it. The question essentially boils down to how to call variadic java functions from clojure.
Without testing this, I believe the solution will be using clojure's into-array function, so something like the following:
(.addListener myButton
(into-array GpioPinListenerDigital
[(reify GpioPinListenerDigital
(^void handleGpioPinDigitalStateChangeEvent [this ^GpioPinDigitalStateChangeEvent event]
(println (str " --> GPIO PIN STATE CHANGE: " (.getPin event) " = " (.getState event)))))]))
You may have to twist this a little bit, but I believe that is the core problem you are facing.
Edit
Due to the second declaration above, another potential solution may simply be wrapping it in some regular list, such as a vector:
(.addListener myButton
[(reify GpioPinListenerDigital
(^void handleGpioPinDigitalStateChangeEvent [this ^GpioPinDigitalStateChangeEvent event]
(println (str " --> GPIO PIN STATE CHANGE: " (.getPin event) " = " (.getState event)))))])

How to convert raw html to something I can test with Selenium?

My team has created a CMS. When it's API is called by the client (using POST - with parameters), it responds with raw HTML, which is then injected into the client's page.
I am assigned to create automated testing specifically for the HTML (not the client page). On my computer I can save the HTML in a file and open with a browser to test it out locally.
To get the test to run on a build server or through Sauce Labs, I am trying to figure out a way to render the HTML so I can have my test framework grab a screenshot to be compared. My test framework is Java/Junit using Selenium bindings, and I use Applitools for screenshot comparison.
I looked into PhantomJS but got a bit lost in the JS world (I am much more comfortable with Java). Also it appears that these artifacts are quite dated in Maven. If this is suggested, I would really appreciate an example.
I have found topics related to posting to the http endpoint using the Junit approach (leveraging Rest Assured), but I am stuck on what to do with the HTML response and how to plug that into a Selenium test. Please, can anyone offer guidance or suggest a tool to do this?
You could use the data scheme to load the html:
driver.get("data:text/html;charset=utf-8," + URLEncoder.encode(pageHtml, "UTF-8"));
Though you may be limited by the length and it won't load the resources present in a separate folder.
Another way would be to execute the requests directly in the page and to then rewrite the whole page with the result.
Something like:
// set domain
driver.get("https://stackoverflow.com");
// navigate some HTML from a request
navigate(driver, "POST", "/search", "q=abcd");
public void navigate(driver, method, path, body) {
String JS_NAVIGATE_REQUEST =
"(function(method, path, body, callback){ " +
" var xhr = new XMLHttpRequest(); " +
" xhr.open(method, path, true); " +
" xhr.onloadend = function(){ " +
" document.write(xhr.responseText); " +
" document.close(); " +
" callback(); " +
" }; " +
" document.write(''); " +
" xhr.send(body); " +
"}).apply(window, arguments); " ;
((JavascriptExecutor)driver).executeAsyncScript(JS_NAVIGATE_REQUEST, method, path, body);
}
I would save the HTML into a file and use IE to display that file.
You can use badboy tool to capture the screenshots of the HTML files saved locally but this is not including selenium integration. It is just to have the baseline screenshots from the HTML saved locally.
Save all the responses in HTML with some predefined naming convention like SS_1,SS_2.
Open badboy and pass the path of 1 file in the browsing pane (at upper right) or use "Request" from tools (Drag & Drop) and provide the path of first file saved locally.
Put snapshot tool below it and configure to save snapshots.
Add variables ${iterator} and provide values
Double click on "Request" and change the file name suffix from SS_1 to ${iterator}
Now, configure step to run for each value of variables by double clicking on the step and selecting the second radio button (For each value of variable) .
Reference tool - Badboy

Liferay Log override

i want override the interface Log, because i use always this menssage:
private final static Log log = LogFactoryUtil.getLog(classess.class);
log.error("La cita " + cita.getIdCita() + " ha producido un excepcion en " + e.getClass() + " casuda por "
+ e.getCause() + ". Trace: " + e.getLocalizedMessage());
my idea is override log.error for get in for parameter the throwlable only and then print the message but i don't know how call to original error.
If I understood correctly, you want to introduce a new method (with 4 parameters) to the Log interface. This is completely discouraged, as your new interface would be utterly incompatible with the assumption that anybody else makes when using this interface (or when providing alternative implementations). You would basically maintain your own private fork of Liferay, largely incompatible with the rest of the world. And that only for a change in a lowly Log class.
Don't go there. It's not too bad to construct an error message like you do in the snippet that you include in your question.
If you have the same thing duplicated everywhere in your code and think it would be cleaner otherwise, encapsulate Liferay's logging within your own logging class and use that, delegating to Liferay's log in the end.
However, don't invest too much time in fancy logging. IMHO that problem has been tackled once and forever, and within an application context, you'd not be able to deliver significant enhancement to the logging world...

Categories

Resources