Call a class for Minecraft Recipes - java

I am having a small issue calling a Class file into my main class
I want to have a separate class to contain all my recipes, but i need to call it up in the common class.
The Recipe is called Recipes.
How would i call that class to load?
The functions loads on #PreInit
#EventHandler
public void preInit(FMLPreInitializationEvent event) { }
Should i change this to Init or #PostInit, as the blocks are all created in #PreInit and #Init is completely empty ?

you can just instanciate this class and use as you want.
Recipes recipes = new Recipes();
in this case is not necessary a injection
you can still get it with the constructor like:
Class YourClass{
//do what you need here, for example instanciate the class and execute something.
public YourClass(){
}
}
You can also create a static class if you prefer.

Related

how to redefine class before bean initialize in spring project

I hava A Class in another jar and i use it in B bean.
Now i want to add log for method in A Class. How can i do this in my project without fix the jar.
My mind:
use ApplicationListener to redefine class before bean init.
do something in onApplicationEvent() to redefine the A class. // this is my question.
I know that can use asm or other tool to fix bytecode. I hava see instrument and do not find solution . https://docs.oracle.com/javase/8/docs/api/java/lang/instrument/Instrumentation.html
How to obtain instance of Instrumentation in Java
A class.
public class A {
public void find(){
System.out.println("aaa");
//i want to add log here.
}
B bean
#Service public class B {
public A get(){
return new A();
}
ApplicationListener
#Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() != null){
return;
}
// redefine class
}
then when i use b.get().find() it will print the log i add.
find my solution.
using javassist modify the class. and load it (using CtClass.toClass())in advance.
https://www.javassist.org/tutorial/tutorial.html

How can I assign different Scheduling time in different classes of java web application

As mentioned below I have three classes I want to set a scheduler using quartz api in my class which is given below.
class Excel{
public void readFile(File file){
//here we place some logic
}
}
class Xml{
public void readFile(File file){
//here we place some logic
}
}
class Text{
public void readFile(File file){
//here we place some logic
}
}
After this I make one servlet class which control the scheduler
in class level I want to create above three class object in controller class after creating these class object I want to set time like Excel class readFile(File file) method execute every 3 hour,
Xml class readFile(File file)method execute every 5 hour and Text class readFile(File file) method execute every 7 hour
public class Myschedular imlements Job{
public void execute(JobExecutionContext context){
//here i want to call all these three class method .How can i do this.
}
}
//I don`t want to write any code repeated in this class
you can use the annotation #Scheduled (spring) on each single (class) job.
The documentation is here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

How do I make an instance available or accessible from anywhere in my android application

I'm new in android and I have a question.
I want to have just one instance of a class in my whole android program so that it's attributes won't change during the program, but I also want to call it's methods in all my activities.
With some search I realized that I can pass my object via implementing class as Serializable or Parcelable. I did this but I got following error:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object
java.io.NotSerializableException: microsoft.aspnet.signalr.client.hubs.HubConnection
as you see one of my class attributes is HubConnection which is in microsoft package and i can't make it Serializable.
What can I do to pass the object of SignalR class to another activities? And what are my options?
public class SignalR implements Serializable {
private HubConnection connection;
private HubProxy proxy;
//some methods
}
If you want a single instance of YourCustomClass throughout the application, you can do by keeping a reference of your custom class object in YourApplication class.
Create a class and extends it to Application class. Create setter and getter method in your application class to access the your custom class instance. Now you can access the your custom class instance from any where within your app and you don't have to pass the instance between activities.
public class YourApplicationClass extends Application{
private YourCustomClass yourCustomClass;
public YourCustomClass getYourCustomClass() {
if (yourCustomClass == null) {
yourCustomClass = new YourCustomClass();
}
return yourCustomClass;
}
public void setYourCustomClass(YourCustomClass yourCustomClass) {
this.yourCustomClass = yourCustomClass;
}
}
Don't forget to put android:name="YourApplicationClass" in your manifest file.
<application
......
android:name=".YourApplicationClass"
....... >
Now to access the object from your activity, say MainActivity you would write something like -
#override
protected void onCreate(Bundle savedInstanceState) {
YourApplicationClass app = (YourApplicationClass) getApplication();
YourCustomClass yourCustomInstance = app.getYourCustomClass();
}
If your goal is to have a single instance of your class globally accessible from all your activities, you do not want to be passing it around with bundles. Rather use a singleton pattern.
If you do need to use the bundles for some other reason, use Parcelable instead of Serializable, it is meant to be faster. There is a pattern to follow with creating a parcelable. Your best bet is to copy-paste the answer from here, and change the constructor, parcelling, and unparcelling.

Applying static methods to data stored in a subclass

I'm working on a class to access information about a class library stored in a Jar file and run actions customized for each class library. It contains methods to load localized strings, etc. from the Jar file.
public class CodeBundle {
public static String str(String id) {
...
}
...
}
I need to be able to know which library I am trying to load information from, so I want to be able to use subclasses representing each library, for example:
public class JAppFramework extends CodeBundle {
...
}
So now in code that is part of the JApp Framework, I want to be able to call JAppFramework.str("...") to load a string from the JApp Framework resource bundle. And if I have another library, such as TestLibrary, I want to be able to call TestLibrary.str("...") to load a string from Test Library's resource bundle. What I did was have a method defined in CodeBundle called getFile() which would return the Jar file from which the library had been loaded. Then the str() method would use that to load the localized string.
The problem is that the static methods in CodeBundle can only access data stored in the CodeBundle class, not in any of its subclasses, as far as I know.
For various reasons, I can't use "getClass().getResource(name)" to load resources.
Is there any way to do what I'm trying to do?
You may try and use singletons:
public abstract class CodeBundle { // or even an interface
public abstract String str(String id);
}
public final class JAppFramework extends CodeBundle {
private static final CodeBundle INSTANCE = new JAppFramework();
// private constructor
private JAppFramework() {
// whatever
}
// get the instance
public static CodeBundle getInstance() { return INSTANCE; }
// Implement str() here
}
// Create other singletons as needed
In your code:
CodeBundle bundle = JAppFramework.getInstance();
bundle.str(whatever);
Of course, this is an ultra simplistic example. Put whatever fields/methods/constructors/constructor arguments are needed in CodeBundle -- which cannot be instantiated since it is abstract.

About use Stubs - Java

I'm readin http://xunitpatterns.com/Test%20Stub.html and have some questions about the use of stubs, for example, in the code shown on the page the author creates a class called TimeProviderTestStub.java for use in test code. I have some doubts about this line in the test code:
TimeDisplay sut = new TimeDisplay();
// Test Double installation
sut.setTimeProvider(tpStub);
Do I need modify my class(SUT) to recieve one object TimeProviderTestSub?
Both the stub and the real class are supposed to implement some interface, i.e. ITimeProvider, and setTimeProvider() should take this interface as its parameter. The interface must expose all methods that the SUT needs to interact with the object, since TimeDisplay can now only use the object through the ITimeProvider interface (which allows us to use a stub instead of the real object in our tests).
In the example, the SUT (TimeDisplay) seems to only need the getTime() method, so the interface should only contain that method:
public interface ITimeProvider {
Calendar getTime();
}
The declaration of the stub should be
public class TimeProviderTestStub implements ITimeProvider { ... }
and the declaration of the real class should be
public class TimeProvider implements ITimeProvider { ... }
Finally, the SUT must change its setter method to accept the interface:
public void setTimeProvider(ITimeProvider timeProvider) { ... }
and also change its internal timeProvider field to be of the type ITimeProvider.
If you do not control the code of the real class (so that you cannot make it implement the interface), you can create an adapter class which wraps the real class and implements the interface.

Categories

Resources