Change lib source code without destroying it - java

So, as I found the patched source code for BodyEditorLoader.java, I am not able to do any changes in the .java file. How to I edit it without destroying the library? Thanks!

I hope to explain well, a simple way would be to create a new class, copy the contents of the BodyEditorLoader class, except class package name package aurelienribon.bodyeditor;, and rename public class BodyEditorLoader by the name of your class, for example MyBodyEditorLoader. example:
//package aurelienribon.bodyeditor; --> change or delete for your packege name
package com.tynibattles04.game; //--> in your case for example.
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MyBodyEditorLoader {
//...other code
public MyBodyEditorLoader(FileHandle file) {
//...other code
}
public MyBodyEditorLoader(String str) {
//...other code
}
//..other code
and used (and import, if you need it necessary):
MyBodyEditorLoader loader = new MyBodyEditorLoader(
Gdx.files.internal("tankA.json"));

Related

Kotlin to java conversion DefaultConstructorMarker is not public in kotlin.jvm.internal

I Downloaded a kotlin file from github and wanted to convert it to a java file. I did this by Tools>Kotlin>Show kotlin ByteCode and then Decompiling. The Kotlin File is below:
package io.pokemon.core
import io.pokemon.model.Card
import io.pokemon.util.random.Randomiser
import io.pokemon.viewmodel.Round
class RoundFactoryImpl(private val randomiser: Randomiser) : RoundFactory {
override fun buildRound(cards: List<Card>): Round = Round()
}
After Copying the Decompiled kotlin Code into a java file I got the error: DefaultConstructorMarker is not public in kotlin.jvm.internal; cannot be accessed from outside package.
I attempted to fix this by adding the code inside DefaultConstructorMarker However this failed to fix the error.
package io.pokemon.core;
import io.pokemon.model.Card;
import io.pokemon.util.random.Randomiser;
import io.pokemon.viewmodel.Round;
import java.util.List;
import kotlin.Metadata;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import org.jetbrains.annotations.NotNull;
#Metadata(
...
)
public final class RoundFactoryImpl implements RoundFactory {
private final Randomiser randomiser;
#NotNull
public Round buildRound(#NotNull List cards) {
Intrinsics.checkParameterIsNotNull(cards, "cards");
return new Round((List)null, (Card)null, 3, (DefaultConstructorMarker)null);
}
public RoundFactoryImpl(#NotNull Randomiser randomiser) {
super();
Intrinsics.checkParameterIsNotNull(randomiser, "randomiser");
this.randomiser = randomiser;
}
}

is there any way of replace this code of this way?

I' am trying to replace this code:
public void doThing(){
if(User.getInstance().isLoggin()){
.....
}
}
by this
#withUser
public void doThing(){
....
}
I saw about the interceptors and annotation, but I cannot do it.
Is this possible?
It is absolutely possible. Project Lombok provides the exact feature what you are looking for. You just need to create your own annotation by extending EclipseAnnotationHandler or JavacAnnotationHandler depending upon your requirement.
A sample annotation handler (WithUser) is given below for reference. If used on method, the entire method will become enclosed by if(false){...} block. Replace false with your own expression on Handler (HandleWithUser.java).
#WithUser
public void doThing(){
System.out.println("Hello World");
}
After annotation processing
public void doThing(){
if (false){
System.out.println("Hello World");
}
}
Setup Project Lombok
git clone https://github.com/rzwitserloot/lombok.git
cd lombok
ant eclipse
Open the Lombok project on Eclipse and create the following files.
WithUser.java
package lombok;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Target({ElementType.METHOD, ElementType.CONSTRUCTOR})
#Retention(RetentionPolicy.SOURCE)
public #interface WithUser {
}
HandleWithUser.java
package lombok.javac.handlers;
import static lombok.javac.handlers.JavacHandlerUtil.setGeneratedBy;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.IfStatement;
import org.eclipse.jdt.internal.compiler.ast.Statement;
import org.mangosdk.spi.ProviderFor;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCIf;
import com.sun.tools.javac.tree.JCTree.JCLiteral;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCStatement;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import org.eclipse.jdt.internal.compiler.ast.FalseLiteral;
import lombok.WithUser;
import lombok.core.AnnotationValues;
import lombok.javac.JavacAnnotationHandler;
import lombok.javac.JavacNode;
import lombok.javac.JavacTreeMaker;
#ProviderFor(JavacAnnotationHandler.class)
public class HandleWithUser extends JavacAnnotationHandler<WithUser>{
#Override
public void handle(AnnotationValues<WithUser> annotationValues, JCAnnotation ast, JavacNode annotationNode) {
JavacNode annotation = annotationNode.up();
JCMethodDecl method = (JCMethodDecl)annotation.get();
List<JCStatement> contents = (List<JCStatement>) method.body.stats;
JCTree source = annotation.get();
JavacTreeMaker maker = annotationNode.getTreeMaker();
JCLiteral falseLiteral = maker.Literal(false);
JCIf ifStatement = maker.If(falseLiteral, maker.Block(0, contents), null);
Context context = annotationNode.getContext();
JCStatement ifBlock = setGeneratedBy(ifStatement, source, context);
method.body.stats = List.of(ifBlock);
annotation.rebuild();
}
}
Generate lombok.jar
ant dist
Test
Make sure you are including lombok.jar in classpath
javac -cp lombok.jar Test.java
For more info, visit https://projectlombok.org/contributing/contributing

How to obtain all ProductTypes created in my commercetools project?

I need to obtain all the ProductType that have been defined in my commercetools project because I have to use the localized value of the "name" to perform a search in a file system.
Basically I need to use the JVM SDK to extract the list of ProductTypes and traverse it.
Can someone give me some clue how to achieve it?
Thanks in advance.
Yep it is pretty feasible using the jvm sdk, here is a code snippet of how to do it
package io.sphere.sdk.deletemeplese;
import io.sphere.sdk.producttypes.ProductType;
import io.sphere.sdk.producttypes.queries.ProductTypeQuery;
import io.sphere.sdk.queries.PagedQueryResult;
import io.sphere.sdk.test.IntegrationTest;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.assertj.core.api.Assertions.assertThat;
public class SomeIntegrationTest extends IntegrationTest {
#Test
public void test(){
final int PAGE_SIZE = 500;
final long totalProductTypes = client().executeBlocking(ProductTypeQuery.of().withLimit(0)).getTotal();
List<ProductType> allProductTypes = IntStream.range(0,(int)(totalProductTypes/PAGE_SIZE) +1)
.mapToObj(i->i)
.map(i -> ProductTypeQuery.of().withLimit(500).withOffset(i*PAGE_SIZE))
.map(client()::executeBlocking)
.map(PagedQueryResult::getResults)
.flatMap(List::stream)
.collect(Collectors.toList());
assertThat(allProductTypes).hasSize((int)totalProductTypes);
}
}
I hope this answers your question.
You could use the class QueryExecutionUtils.
CompletionStage<List<ProductType>> allProductTypesStage =
QueryExecutionUtils.queryAll(client, ProductTypeQuery.of());

Cannot find symbol Time in Java

I am a newbie in Java and writing a very simple program. It looks perfect to me.
I know that java.util.Date contains a Time object that can be used.
This link also provides some documentation on the same.
However when I run the program I get an error. Please help me figure out what's wrong here.
package timestuff;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.TimeZone;
public class MyDays {
private Time start;
private double temp;
public MyDays(final Time StartTime, final double Temperature) {
start = StartTime;
temp = Temperature;
}
}
I am getting the following error:
MyDays.java:15: error: cannot find symbol
private Time start;
^
symbol: class Time
location: class MyDays
You need to import time class .
import java.sql.Time
The javadoc doesn't say anything about a time class in java.util.Date
You import it.
import java.sql.Time

Why do we need to import a class if the parent already imports it

Say I have:
import android.os.Bundle;
import android.app.Activity;
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle b) {
}
}
and then I extend that class as such:
public class MyNewActivity extends MyActivity {
#Override
public void onCreate(Bundle b) {
}
}
If I don't include import android.os.Bundle; then MyNewActivity class won't compile, however it should already know about Bundle since the parent class already imports it. What gives?
In Java, the scope of the import is not the class that is declared, but the file in which the import is given. So, in a different file, you must still import what you need.
According to the JLS, Section 7.5,
An import declaration makes types or members available by their simple
names only within the compilation unit that actually contains the
import declaration.
That is, the scope of the import is the file within which it's located.

Categories

Resources