[please note this may require AS3 + Java knowledge]
Background Information:
I'm trying to build a game using Java + Pulpcore, but I'm fairly new to the scene. The game I'm building could possibly be more performance intensive than I thought, and I know Java would solve my problems with this, but there are a couple questions I have dealing with strict-types, etc.
Here is my code in AS3:
Main.as3
import org.tbmb.champions.Container;
import org.tbmb.zombies.ZContainer;
public class Main extends MovieClip {
// ******* temporary properties ******* /
private var blunder:Container = null;
// ******* ******* /
public function Main() {
init(); // initialize objects
}
public function init():void {
blunder = new Container(Blunder as Class);
} // end of class
}
Container.as3
package org.tbmb.champions {
import flash.display.MovieClip;
public class Container extends MovieClip {
public function Container(champ:*) {
} // end of constructor
} // end of class
} // end of package
Blunder.as3
package org.tbmb.champions.blunder {
import flash.display.MovieClip;
public class Blunder extends MovieClip {
public function Blunder() {
} // end of constructor
} // end of class
} // end of constructor
1.) How would I rewrite in Java?
blunder = new Container(Blunder as Class);
2.) How would I be able to accept any Classes in Java for the above line within my Container class?
public function Container(champ:*) {
I need to do this because I'm sending different champion classes (depending on what the user picks) to a containing class that will hold all their other classes (health, etc). I need my Container class to accept any Class rather than just one; what type would I use?
Here is what I have in Java so far:
ZomboPulp.java (Main Class)
import pulpcore.scene.Scene2D;
import org.tnpfk.champions.Container;
import org.tnpfk.champions.blunder.Blunder;
import pulpcore.sprite.FilledSprite;
import pulpcore.image.Colors;
public class ZomboPulp extends Scene2D {
FilledSprite background = new FilledSprite(Colors.WHITE);
Container container = null; // Container that contain's blunder,
// and all his objects (health, mana, etc)
public void load() {
this.initScreen(); // initialize main screen.
this.initObjects(); // initialize our objects.
} // end of load();
public void initScreen() {
add(background);
} // end of initScreen();
public void initObjects() {
container = new Container(Blunder as Class); // ERROR HERE
} // end of initObjects();
}
Container.java
package org.tnpfk.champions;
public class Container {
public Container(Object champ) {
} // end of constructor
} // end of class
Sorry for the lengthy post, and thanks for any help. By the way, I did check StackOverflow; and Google, but I was unable to find anything about this.
Thanks,
jvmpulp
Alrighty! I have no experience with PulpCore, but I do know both AS3 and Java, so I think I can answer your question. First off, I guess I don't 100% understand what you need to do with the champ object in the Container class, and I really don't understand why you were doing Blunder as Class instead of just passing an instance of Blunder. Either way, here's what I'd recommend with what you have as of now:
public void initObjects() {
container = new Container(Blunder.class);
}
As you can see, you can get a Class instance just by getting the class property of any class. Now, you have the right idea with using Object as the type for the Container constructor for any type. However, using Object is bad practice (use method overloading/more specific types instead), and it's not even required here. Getting the class property will always be of type Class, even though they represent different classes. So, rewrite the constructor as this:
public Container(Class champ) {
}
Then, do whatever you need to do with the class.
Now, that's basically a direct port, but it seems some of the things you're doing are bad practice. The whole system of passing a Class object seems irrelevant and unnecessary; why not just pass an instance of the object? For example, like so:
public class Container {
protected Champion champ;
public Container(Champion champ) {
this.champ = champ;
}
}
Now, make Champion an abstract class that contains the common methods for all the champions:
public abstract class Champion {
protected Something something;
abstract Something getSomething();
}
(Obviously, the variable/method shown here are just examples.) Then, have your individual Champion classes subclass Champion:
public class Blunder extends Champion {
public Something getSomething() {
return this.something;
}
}
Etc. Then, finally, do this:
public void initObjects() {
container = new Container(new Blunder());
}
Obviously, this is a basic example, and you don't have to take my advice. But it would probably be easier to do than the system you already had in AS3.
Related
For two utility classes with the same names, which contain only static methods, I proceeded as follows:
Simply imported the first
Created an instance of the second class.
Example:
package util1;
public class Utility {
public static void method() {
System.out.println("First Utility. static method");
}
}
package util2;
public class Utility {
public static void method() {
System.out.println("Second Utility. static method");
}
}
import util1.Utility;
public class Component {
private static final util2.Utility anotherUtility = new util2.Utility();
public static void usedByReflection() {
Utility.method();
anotherUtility.method();
}
}
Now I don't need to write a full second util-class name for invoke its methods, but maybe I did not foresee something...?
P.S:
The methods of the class Component are called through a reflection by a certain BlackBox. All the multithread-safe features are in BlackBox.
UPD: I have found better trick:
import util1.Utility;
public class Component {
private static final util2.Utility anotherUtility = null; // There are some changes
public static void usedByReflection() {
Utility.method();
anotherUtility.method();
}
}
Now I dont create new object, but is it possible to use it without any bugs?
IMO, this is confusing and could much more clearly be handled by something like:
public class CombinedUtilityComponent {
public static void usedByReflection() {
util1.Utility.method();
util2.Utility.method();
}
}
Or, better yet, in your code you can just fully qualify the class names and they become unique names without any confusing tricks.
Yes, this works. I wouldn't do it, though.
You're calling a static method as if it were an instance method. anotherUtility.method() has a useless reference to anotherUtility.
You also have an unnecessary instantiation of util2.Utility. This technique wouldn't work if the default constructor were disabled.
Today while coding I began a deep dive into the world of reflection. I have messed with it in small bits and pieces before but never to this extent and no matter where I look I can find no answer to my question and so here I am! Currently what I am trying to do is use reflection with a class whose constructor requests a parameter but for ease of use I wish to use the super class of the parameter.
Here is the code causing problems with some explanations:
this.listener = (MyListener) listenerClass.getConstructor(MyAppState.class).newInstance(this);
The thing is that MyAppState is the class that all of my appstates extend from and each listener takes in its own specific AppState that extends MyAppState but has extra features different from each other. What I need to know is what I can put in my .getConstructor() to specify that the class I am passing in is the super of the parameter it wants.
Here is a theoretical example of the code:
this.listener = (MyListener) listenerClass.getConstructor(Class extends MyAppState.class).newInstance(this);
So is this possible or should I just work with my code to have a second constructor that accepts the MyAppState class, or something else along those lines.
Also, sorry if this is off topic but to prevent problems in the future, I am being told this question is subjective. Is there any way to word future questions to not be as subjective or is the method to figure out whether the question is subjective just a bit flawed?
[EDIT] As requested, a few more examples related to the question:
public MyAppState(Node screen, Class listenerClass)
{
this.screen = screen;
try
{
this.listener = (MyListener) listenerClass.getConstructor(MyAppState.class).newInstance(this);
}
catch (Exception e)
{
Logger.getLogger(MyAppState.class.getName()).log(Level.SEVERE, "The listener for the {0} appstate could not be created using reflection.", new Object[]{this.getClass().getName()});
System.exit(-1);
}
}
Above is the full parent class's constructor, the said class extends one more class but it contains no constructor and so I am not sure if it is needed. If it is please feel free to ask for it.
public class OptionsMenuState extends MyAppState
{
public OptionsMenuState()
{
super(new Node("Options Screen"), OptionsMenuStateListener.class);
}
That is one class and its constructor with pieces cut off to keep it short.
public class MainMenuState extends MyAppState
{
public MainMenuState()
{
super(new Node("Start Screen"), MainMenuStateListener.class);
}
Here is another class and its constructor.
[EDIT] As suggested, I created a program that roughly mimics what I am attempting to do.
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
//This is what I want to do but I don't know how to have java allow me to pass in Greeting.
createObject(SimpleSentence.class);
createObject(DifferentSentence.class);
}
public static void createObject(Class theClass)
{
theClass.getConstructor(Greeting.class).newInstance(new Hello());
}
class SimpleSentence
{
Hello firstWord;
public SimpleSentence(Hello word)
{
firstWord = word;
}
}
class DifferentSentence
{
Howdy firstWord;
public DifferentSentence(Howdy word)
{
firstWord = word;
}
}
class Greeting
{
}
class Hello extends Greeting
{
}
class Howdy extends Greeting
{
}
}
Short answer is you can't. getConstructor(Class<?>...parametertypes) is very explicit because it needs to match exactly zero or one constructor only.
You will need to go through all the constructors to find one, that has a parameter that is assignable from MyAppState
e.g. outline:
final Constructor<?>[] ctrs = listenerClass.getConstructors();
for (Constructor<?> constructor : ctrs) {
if (constructor.getParameterTypes()[0].isAssignableFrom(MyAppState.class)) {
// use this one?
}
}
I have the following code class Agent.java :
public class Agent {
Helper helper ;
private class SpecificBehaviour extends Behaviour{
private Apple a;
public SpecificBehaviour(Apple a){
setApple(a);
}
public void setApple(Apple a){
this.a=a;
}
public Apple getApple(){
return a;
}
}
public void someMethod(){
helper = new Helper(this);
}
}
In the Helper.java ( another class within the same package) I would like to access the getApple() method. did some search and found this link
I am wondering if there is a better/ easier way of doing this ?
There are at least two issues here:
Helper doesn't know of the existence of SpecificBehaviour, because it's a private class. It could potentially know about the Behaviour class, which you haven't given any details of. If getApple() is declared in Behaviour, and if Behaviour is visible to Helper, then the visibility part needn't be a problem.
Helper will need a reference to an instance of SpecificBehaviour, which means you'll need to instantiate SpecificBehaviour. For that, you'll also need an instance of Agent, because SpecificBehaviour is an inner class. It's not clear whether you have such an instance.
Basically I think the presence of a private inner class is adding confusion here. If you're reasonably new to Java, I'd strongly recommend sticking to top-level classes for the moment. They have a few subtleties around them, and it's best to try to learn one thing at a time.
If this doesn't help, please give more context - your question is quite vague at the moment. Where do you want to use getApple within Helper? Should part of the state of Helper be a reference to an instance of SpecificBehaviour, or should it be a method parameter? Have you created an instance of Agent? What does Behaviour look like? You may find that in the course of answering these questions one at a time, you're better able to figure out the problem for yourself.
- Use Composition principle to get the access to the getApple() method.
Eg:
public class Agent {
Apple a = new Apple(); // Agent class has a reference of type Apple.
.....
.....
}
- Second way would be to make the getApple() method static in Apple class, and then access it from Agent class using the Class name with . (dot) operator.
Eg:
public class Agent {
public void go(){
Apple.getApple();
}
.....
.....
}
You need to ask the Agent object you are passing to the Helper for the instance of the private class SpecificBehaviour. This is the way it works. Encapsulation remember.
Jon Skeet stated that and I completely agree on it:
Helper will need a reference to an instance of SpecificBehaviour,
which means you'll need to instantiate SpecificBehaviour. For that,
you'll also need an instance of Agent, because SpecificBehaviour is an
inner class. It's not clear whether you have such an instance.
Actually, you can understand how weird your try is by testing the sample code below:
Agent.java
public class Agent
{
private class SpecificBehaviour
{
public String toString()
{
return "specific behaviour";
}
}
public Class getInner()
{
return SpecificBehaviour.class;
}
}
Helper.java
public class Helper
{
public static void main(String[] args)
{
try
{
Agent agent = new Agent();
System.out.println(agent.getInner().newInstance().toString());
}
catch (InstantiationException e) { e.printStackTrace(); }
catch (IllegalAccessException e) { e.printStackTrace(); }
}
}
The code above just compiles fine. And let's see what the output is:
java.lang.InstantiationException: Agent$SpecificBehaviour
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at Helper.main(Helper.java:5)
I was wondering if anyone had a pattern that would help me achieve the following:
We have a JPA entity called Employee and on it there is a setLineManager method. We also have a separate updateLineStructureService, which is a Spring-managed service bean. We want to try and ensure that this setLineManager method can only be called from updateLineStructureService and not directly from any other class.
Is there a way to allow the service access to this method without exposing it to any other classes? I am aware that I could give the method package level access and put the service in the same package as Employee, but that will not fit our package structure so I would prefer not to do that. I am also aware that I could make the method private and just access it through reflection in this one place, but I do not like that solution at all.
Any ideas?
You can inspect the stacktrace (using Throwable#getStackTrace()) and see if it contains the allowed method on specified position.
In the following code snippet, System.PrivateEmployee is not visible outside the System class. Thus effectively privateMethod is private and can only be called from within the System class. Since System.PrivateEmployee extends System.PublicEmployee it can be used outside the System class as System.PublicEmployee
public class System
{
public static interface PublicEmployee { void publicMethod ( ) ; }
private static interface PrivateEmployee extends PublicEmployee { void privateMethod ( ) ; }
}
Use an inner class only available to the other service class:
public class Employee
{
static {
LineStructureService.registerEmployeeHelper(new EmployeeHelper() {
#Override
public void setLineManager(Employee emp, Object foo) {
emp.setLineManager(foo);
}
});
}
public static void init() {}
private void setLineManager(Object foo) { }
}
public class LineStructureService
{
private static volatile EmployeeHelper _helper;
static {
// ensure that Employee class is loaded and helper registered
Employee.init();
}
public static synchronized void registerEmployeeHelper(EmployeeHelper helper) {
_helper = helper;
}
public void doSomething(Employee emp)
{
// now this class can call setLineManager on Employee
_helper.setLineManager(emp, blah);
}
public interface EmployeeHelper {
public void setLineManager(Employee emp, Object foo);
}
}
The only way that a class can access private methods of other classes is with inner classes. If that is not an option, this can't be done.
One approach is to make two forms of Employee.
"BasicEmployee" has all the methods except setLineManager(). "ExtendedEmployee" extends BasicEmployee and adds a public void setLineManager(). (I'm assuming these are classes, but they could also be interfaces instead) Underneath the hood, everything is really a FullEmployee (for clarity, you could make BasicEmployee abstract). But, in the code, in all the classes except UpdateLineStructureService, you declare it as a BasicEmployee. Only in UpdateLineStructureService is it declared as a FullEmployee. So, only UpdateLineStructureService has easy access to setLineManager()
Now, a rogue coder could always cast their BasicEmployee to an ExtendedEmployee to access setLineManager(), so this isn't totally secure. But it's a reasonable pattern to limit access.
You could use AOP (e.g. AspectJ or CDI) to intercept the call to setLineManager(); if the caller is updateLineStructureService() call the method; if not do nothing, or raise an exception or whatever.
I don't know how to do this (yes sorry, should be a must to know maybe).
It would be easy to explain with an example :
public class PageMenuLogin extends Container {
public PageMenuLogin() {
final ITextField login_user = this.createTextField();
login_user.setName("");
final ITextField login_pass = this.createTextField();
login_pass.setName("");
final IButton button = this.createButton();
button.setLabel("Login");
button.setActionHandler(new IActionHandler() {
public ClientActions onAction() throws Exception {
// I NEED TO UPDATE THE CONTAINER ITSELF HERE.
// HOW CAN I CALL "this"?
return null;
}
});
}
}
As you can see in the example, i need to call the "this" when i'm into that method.
I think everybody in java but markzzz know how to do this :)
PageMenuLogin.this?
when you create a object like this
button.setActionHandler(new IActionHandler() {
public ClientActions onAction() throws Exception {
// I NEED TO UPDATE THE CONTAINER ITSELF HERE.
// HOW CAN I CALL "this"?
return null;
}
});
You created a anonymous class(a class without name), take a look in binary class file dir, you'll see some class has name like PageMenuLogin$1.class, PageMenuLogin$2.class ... they are binary code of anonymous class. A anonymous class, Inner class can access its parent(top level) class object using syntax just as Tom's answer.
PageMenuLogin.this