ASM Code Error when main method is defined - java

I am getting the error:
Prints the ASM code to generate the given class.
Usage: ASMifier [-debug]
From what I gather this is because I do not have the main method declared but this is clearly defined in one of my classes: Could this be a build error?
Main Class
package com.exacttarget.client;
public class Main {
public static void main(String[] args) {
System.out.println("Test");
}
}
PropertiesUtil Class
package com.exacttarget.client;
import java.util.Properties;
public class PropertiesUtil {
private static PropertiesUtil _instance = new PropertiesUtil();
Properties properties;
public void init() {
try {
if (properties == null || properties.isEmpty()) {
java.io.InputStream inConfig = PropertiesUtil.class.getClassLoader().getResourceAsStream("props.xml");
properties.loadFromXML(inConfig);
//properties.load(inConfig);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
private PropertiesUtil() {
super();
properties = new Properties();
init();
}
public static PropertiesUtil getInstance() {
return _instance;
}
public String getProperty(String key) {
return properties.getProperty(key);
}
}
PWCBHandler Class
package com.exacttarget.client;
import java.io.IOException;
import javax.security.auth.callback.*;
import org.apache.ws.security.WSPasswordCallback;
public class PWCBHandler implements CallbackHandler {
public PWCBHandler() {
}
#Override
public void handle(Callback callbacks[]) throws IOException, UnsupportedCallbackException {
for(int i = 0; i < callbacks.length; i++)
if(callbacks[i] instanceof WSPasswordCallback) {
WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
pc.setIdentifier(PropertiesUtil.getInstance().getProperty("username"));
pc.setPassword(PropertiesUtil.getInstance().getProperty("password"));
//pc.setPassword("welcome#1");
} else {
throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
}
}
}

Related

Unable to serialize instance variable of a non-serializable superclass from the serializable subclass

New to this topic and right now I'm stuck at a brick wall. I have 2 classes, parent class: Controller.java and subclass: GreenhouseControls.java. I need to serialize a GreenhouseControls object but also an instance variable (eventList) from its superclass Controller.java.
My serialization happens when an inner class of GreenhouseControls.java throws a custom ControllerException, which is caught in the main method. Before terminating the program, the GreenhouseControls object should be saved (including the field from its superclass).
Why is a NotSerializableException thrown by the inner class WindowMalfunction of GreenhouseControls? Anyone have any ideas, as I am seriously stuck?
What I tried is the following:
Implement serializable on Controller.java. This is because if the superclass is serializable, then subclass is automatically serializable, however this throws java.io.NotSerializableException: GreenhouseControls$WindowMalfunction, (WindowMalfunction is the inner class that throws the initial exception to begin the serialization processs).
Implement serializable on GreenhouseControls.java and implement custom serialization by overriding writeObject() and readObject() to save the field from the superclass. This approach yet again throws the same exception as the approach 1.
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(super.eventList);
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
Object obj = in.readObject();
List<Event> x = cast(obj);
super.eventList = x;
}
Controller.java
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class Controller {
// THIS IS THE VARIABLE I NEED TO SAVE
protected List<Event> eventList = new ArrayList<Event>();
public void addEvent(Event c) {
eventList.add(c);
}
public void run() throws ControllerException {
while (eventList.size() > 0)
// Make a copy so you're not modifying the list
// while you're selecting the elements in it:
for (Event e : new ArrayList<Event>(eventList))
if (e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
}
public static void shutDown() { }
}
GreenhouseControls.java class (note I have removed the inner classes and other code from it and only left related info)
public class GreenhouseControls extends Controller implements Serializable {
private int errorcode = 0;
public class WindowMalfunction extends Event {
public WindowMalfunction(long delayTime) {
super(delayTime);
}
public void action() throws ControllerException {
windowok = false;
throw new ControllerException("Window malfunction");
}
public String toString() {
return "Window malfunction";
}
}
public class PowerOut extends Event {
public PowerOut(long delayTime) {
super(delayTime);
}
public void action() throws ControllerException {
poweron = false;
throw new ControllerException("Power out");
}
public String toString() {
return "Power out";
}
}
// Various other inner classes that extend event exist
public static void serializeObject(GreenhouseControls gc) {
FileOutputStream fileOut;
ObjectOutputStream out;
try {
fileOut = new FileOutputStream("dump.out");
out = new ObjectOutputStream(fileOut);
out.writeObject(gc);
System.out.println("WERRROR code: " + gc.getError());
out.close();
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(super.eventList);
}
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
in.defaultReadObject();
Object obj = in.readObject();
List<Event> x = cast(obj);
super.eventList = x;
}
#SuppressWarnings("unchecked")
public static <T extends List<?>> T cast(Object obj) {
return (T) obj;
}
public int getError() {
return errorcode;
}
public Fixable getFixable(int errorcode) {
switch (errorcode) {
case 1:
return new FixWindow();
case 2:
return new PowerOn();
default:
return null;
}
}
public static void main(String[] args) {
GreenhouseControls gc = null;
try {
String option = args[0];
String filename = args[1];
if (!(option.equals("-f")) && !(option.equals("-d"))) {
System.out.println("Invalid option");
printUsage();
}
// gc = new GreenhouseControls();
if (option.equals("-f")) {
gc = new GreenhouseControls();
gc.addEvent(gc.new Restart(0, filename));
}
gc.run();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid number of parameters");
printUsage();
} catch (ControllerException e) {
String errormsg;
if (e.getMessage().equals("Window malfunction")) {
gc.errorcode = 1;
errormsg = "Window malfunction event occurred Error code: " + gc.errorcode;
} else {
gc.errorcode = 2;
errormsg = "Power out event occurred Error code: " + gc.errorcode;
}
logError(errormsg);
serializeObject(gc);
gc.displayEventList();
shutDown();
}
}
}
Event.java
public abstract class Event {
private long eventTime;
protected final long delayTime;
public Event(long delayTime) {
this.delayTime = delayTime;
start();
}
public void start() { // Allows restarting
eventTime = System.currentTimeMillis() + delayTime;
}
public boolean ready() {
return System.currentTimeMillis() >= eventTime;
}
public abstract void action() throws ControllerException;
Event has to be Serializable too.
Change
public abstract class Event {
to
public abstract class Event implements Serializable {

JCodeModel ref not imported

I'm trying to create a class (MoMoTest.class) which extends a generic abstract class (MappingObject.class). Everything looks good, except that JCodeModel doesn't import the narrowed class (MoTest.class), although I created a JClass of it with codeModel.ref:
MappingObject.class:
package test;
public abstract class MappingObject<T> {
protected T dataObject;
public MappingObject( T dataObject ) {
this.dataObject = dataObject;
}
public abstract T getDataObject();
public abstract String getStandardFormat();
}
MoTest.class:
package test;
public class MoTest {
}
MappingObjectCreator.class:
package test;
import com.sun.codemodel.*;
import java.io.File;
import java.io.IOException;
public class MappingObjectCreator {
public JDefinedClass getMappingObject(JCodeModel codeModel, JPackage jPackage, Class<?> clazz) throws JClassAlreadyExistsException {
JClass ref = codeModel.ref(clazz); // Not imported in MoMoTest.class
JDefinedClass definedClass = jPackage._class("Mo" + ref.name());
JClass superClass = codeModel.ref(MappingObject.class).narrow(ref);
definedClass._extends(superClass);
JFieldRef dataObject = JExpr.ref("dataObject");
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
JVar param = constructor.param(ref, ref.name());
constructor.body().invoke("super").arg(param);
JMethod getDataObject = definedClass.method(JMod.PUBLIC, ref, "getDataObject");
getDataObject.annotate(codeModel.ref(Override.class));
getDataObject.body()._return(dataObject);
JMethod getStandardFormat = definedClass.method(JMod.PUBLIC, String.class, "getStandardFormat");
getStandardFormat.annotate(codeModel.ref(Override.class));
getStandardFormat.body()._return(dataObject.invoke("toString"));
return definedClass;
}
public void getMappingObject(Class clazz, String path) throws JClassAlreadyExistsException, IOException {
JCodeModel codeModel = new JCodeModel();
JPackage jPackage = codeModel._package(clazz.getPackage().getName());
getMappingObject(codeModel, jPackage, clazz);
codeModel.build(new File(path));
}
public static void main(String[] args) throws IOException, JClassAlreadyExistsException {
new MappingObjectCreator().getMappingObject(MoTest.class, "src/main/java");
}
}
Result (MoMoTest.class):
package test;
public class MoMoTest
extends MappingObject<test.MoTest>
{
public MoMoTest(test.MoTest MoTest) {
super(MoTest);
}
#Override
public test.MoTest getDataObject() {
return dataObject;
}
#Override
public String getStandardFormat() {
return dataObject.toString();
}
}
So why is Motest.class not imported in MomoTest.class ?
After modifying the parametername of the constructor of the generated class (MoMoTest.class) to first-letter-lower-case it somehow works:
MappingObjectCreator.class:
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
char[] refName = ref.name().toCharArray();
refName[0] = Character.toLowerCase(refName[0]);
JVar param = constructor.param(ref, new String(refName));
constructor.body().invoke("super").arg(param);
Result:
package test;
public class MoMoTest
extends MappingObject<MoTest>
{
public MoMoTest(MoTest moTest) {
super(moTest);
}
#Override
public MoTest getDataObject() {
return dataObject;
}
#Override
public String getStandardFormat() {
return dataObject.toString();
}
}

Custom EventListener check what abstract method the class is overriding

Hello so i got the following code:
Event Handler.java
package me.xenopyax.edla.watcher;
import static java.nio.file.StandardWatchEventKinds.*;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchEvent.Kind;
import java.util.ArrayList;
import java.util.List;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
public class EventHandler {
private Path folderPath = Paths.get(System.getProperty("user.dir") + "/Saved Games/Frontier Developments/Elite Dangerous");
private String watchFile;
private List<EventListener> listeners = new ArrayList<>();
public EventHandler() {
// We obtain the file system of the Path
FileSystem fileSystem = folderPath.getFileSystem();
// We create the new WatchService using the try-with-resources block
try (WatchService service = fileSystem.newWatchService()) {
// We watch for modification events
folderPath.register(service, ENTRY_MODIFY);
// Start the infinite polling loop
while (true) {
// Wait for the next event
WatchKey watchKey = service.take();
for (WatchEvent<?> watchEvent : watchKey.pollEvents()) {
// Get the type of the event
Kind<?> kind = watchEvent.kind();
if (kind == ENTRY_MODIFY) {
Path watchEventPath = (Path) watchEvent.context();
// Call this if the right file is involved
if (watchEventPath.toString().equals(watchFile)) {
//File has been modified, call event registered
}
}
}
if (!watchKey.reset()) {
// Exit if no longer valid
break;
}
}
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void registerListener(EventListener listener) {
listeners.add(listener);
}
}
and Main.java
package me.xenopyax.edla;
import java.io.File;
import me.xenopyax.edla.discord.EDLARPC;
import me.xenopyax.edla.watcher.EventHandler;
import me.xenopyax.edla.watcher.GameStartListener;
public class Main {
private EDLARPC edlarpc = new EDLARPC();
private File journalDir = new File(System.getProperty("user.home") + "/Saved Games/Frontier Developments/Elite Dangerous");
public static void main(String[] args) {
EventHandler handler = new EventHandler();
handler.registerListener(new GameStartListener());
}
public EDLARPC getRPC() {
return edlarpc;
}
public File getJournalDirectory() {
return journalDir;
}
and EventListener.java
package me.xenopyax.edla.watcher;
public abstract class EventListener {
public void onGameStart(){};
}
and GameStartListener.java
package me.xenopyax.edla.watcher;
public class GameStartListener extends EventListener {
#Override
public void onGameStart() {
}
}
Now my question is how do I call the abstract method from EventListener.java in EventHandler.java and how do i check in the ArrayList which methods are overridden? I am trying to create an EventHandler that listens to an file and when changes happen it looks up what changed and fires the approperiate abstract method from EventListener.java.
You can check a declaring class of a method if it's not your abstract class then the method was overridden.
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
class Main {
static abstract class EventListener {
public void onFileChanged() {
throw new NotImplementedException();
}
}
static class EventListenerNotImpl extends EventListener {
}
static class EventListenerImpl extends EventListener {
private String id;
public EventListenerImpl(String id) {
this.id = id;
}
public void onFileChanged() {
System.out.println(id + ":" + EventListenerImpl.class.getCanonicalName() + ".onFileChanged() was called");
}
}
static class EventHandler {
private List<EventListener> listeners = new ArrayList<>();
public void addListener(EventListener listener) {
listeners.add(listener);
}
private void propagateOnFileChangedEvent() {
listeners.forEach(l -> {
try {
Method m = l.getClass().getMethod("onFileChanged");
if (!m.getDeclaringClass().equals(EventListener.class)) {
l.onFileChanged();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
});
}
}
public static void main(String[] args) {
EventHandler handler = new EventHandler();
handler.addListener(new EventListenerImpl("listener-1"));
handler.addListener(new EventListenerNotImpl()); // Not will be invoked onFileChangedEvent
handler.addListener(new EventListenerImpl("listener-3"));
handler.propagateOnFileChangedEvent();
}
}
Output:
listener-1:Main.EventListenerImpl.onFileChanged() was called
listener-3:Main.EventListenerImpl.onFileChanged() was called

call the same method name from instances different type

I have some libraries from external company, I want to use this API. I try to implement calling this API, my logic should call the same method name. I have duplicate codes, I want to avoid to do this. I'm beginner and subjects like interfaces, polymorphism are little bit difficult to me.
public void modPeople(Object person)
{
if (person instanceof com.company.persontype1)
{
com.company.persontype1 fireman = (com.company.persontype1) person;
String name = fireman.getName();
if (name!=null ) {
...
fireman.set_name();
fireman.save();
}
permissions = fireman.get_Permissions();
...
permissions = fixperm (permissions);
fireman.set_Permissions();
};
if (person instanceof com.company.persontype2)
{
com.company.persontype2 nurse = (com.company.persontype2) person;
String name = nurse.getName();
if (name!=null ) {
...
nurse.set_name();
nurse.save();
}
permissions = nurse.get_Permissions();
...
permissions = fixperm (permissions);
nurse.set_Permissions();
};
}
First of all I should mention that the methodology which you requested in your question is called "Duck Typing". Generally this technology is possible in Java (see below the example) but it's not widely used in Java. There could be performance hits etc. It would be much better to introduce a proper inheritance/interface level instead.
Also the provided example don't deal with exceptions properly etc. It's just a quick and quite dirty "demostration of the technology". Feel free to adapt it for your needs.
It's Java7 (for multi-catch clauses, you may refactor this with ease).
ISomeIterface.java (it contains all common methods implemented by classes which are used in your "bad code"):
package org.test;
public interface ISomeInterface {
public String getName();
public void setName(String _name);
public void save();
// specify other common methods
}
ReflectCaller.java:
package org.test1;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import org.test.ISomeInterface;
public class ReflectCaller {
private final Method[] methods = ISomeInterface.class.getDeclaredMethods();
private final Map<Class<?>, Method[]> maps = new HashMap<Class<?>, Method[]>();
public void inspectClass(Class<?> _clazz) throws NoSuchMethodException, SecurityException {
final Method[] ms = new Method[methods.length];
int i = 0;
for(final Method m: methods) {
ms[i] = _clazz.getMethod(m.getName(), m.getParameterTypes());
i++;
}
maps.put(_clazz, ms);
}
public ISomeInterface wrapper(Object _obj) {
final Method[] ms = maps.get(_obj.getClass());
// To be replaced by guava's Preconditions.checkState()
if (ms == null)
throw new NoSuchElementException(String.format("Class %s is unregistered", _obj.getClass().getName()));
return new SomeInterfaceImpl(_obj, ms);
}
private static class SomeInterfaceImpl implements ISomeInterface {
private final Object obj;
private final Method[] ms;
public SomeInterfaceImpl(Object _obj, Method[] _ms) {
ms = _ms;
obj = _obj;
}
#Override
public String getName() {
try {
return (String) ms[0].invoke(obj);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
#Override
public void setName(String _name) {
try {
ms[1].invoke(obj, _name);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
#Override
public void save() {
try {
ms[2].invoke(obj);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
}
And test class ReflectTest.java. Notice that classes ReflectTest.Test and ReflectTest.Test2 has the same methods as ISomeInterface but don't implement it, they are completely independent from that interface and from each other.
package org.test2;
import org.test.ISomeInterface;
import org.test1.ReflectCaller;
public class ReflectTest {
private final ReflectCaller rc;
ReflectTest(Class ... _classes) throws NoSuchMethodException, SecurityException {
rc = new ReflectCaller();
for(final Class c: _classes)
rc.inspectClass(c);
}
void callSequence(Object _o) {
// this function demonstrates the sequence of method calls for an object which has "compliant" methods
ISomeInterface tw = rc.wrapper(_o);
tw.setName("boo");
System.out.printf("getName() = %s\n", tw.getName());
tw.save();
}
public static class Test {
public String getName() {
System.out.printf("%s.getName()\n", getClass().getName());
return "boo";
}
public void setName(String _name) {
System.out.printf("%s.setName(%s)\n", getClass().getName(), _name);
}
public void save() {
System.out.printf("%s.save()\n", getClass().getName());
}
}
public static class Test2 {
public String getName() {
System.out.printf("%s.getName()\n", getClass().getName());
return "boo2";
}
public void setName(String _name) {
System.out.printf("%s.setName(%s)\n", getClass().getName(), _name);
}
public void save() {
System.out.printf("%s.save()\n", getClass().getName());
}
}
public static void main(String[] args) {
ReflectTest rt;
try {
rt = new ReflectTest(Test.class, Test2.class);
} catch (NoSuchMethodException | SecurityException e) {
System.out.println(e);
System.exit(2);
return;
}
rt.callSequence(new Test());
rt.callSequence(new Test2());
}
}

JavaBeans Introspector does not correctly find property with interface hierarchy type

I read the JavaBeans specs but I found nowhere this behavior. Is it a bug ?
testPropertyType fails because expects Data class
testPropertyReadable succeed because DefaultBean.getMyData returning Data method exists
testPropertyWritable fails because no DefaultBean.setMyData(Data) method does not exists
Tested on JavaSE 6
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import org.junit.Test;
public class DefaultBeanTest {
#Test
public void testPropertyType()
throws Exception
{
final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
if ("class".equals(property.getName())) {
continue;
}
if ("myData".equals(property.getName())) {
if (!property.getPropertyType().equals(ModifiableData.class)) {
throw new AssertionError("expects " + ModifiableData.class + " but was "
+ property.getPropertyType());
}
}
}
}
#Test
public void testPropertyReadable()
throws Exception
{
final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
if ("class".equals(property.getName())) {
continue;
}
if ("myData".equals(property.getName())) {
if (property.getReadMethod() == null) {
throw new AssertionError("expects read method");
}
}
}
}
#Test
public void testPropertyWritable()
throws Exception
{
final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
if ("class".equals(property.getName())) {
continue;
}
if ("myData".equals(property.getName())) {
if (property.getWriteMethod() == null) {
throw new AssertionError("expects write method");
}
}
}
}
static interface Data {
}
static interface ModifiableData
extends Data {
}
static class DefaultData
implements ModifiableData {
}
static interface Bean {
Data getMyData();
}
static interface ModifiableBean
extends Bean {
ModifiableData getMyData();
void setMyData(
ModifiableData data);
}
static class DefaultBean
implements ModifiableBean {
#Override
public ModifiableData getMyData()
{
return this.data;
}
#Override
public void setMyData(
final ModifiableData data)
{
this.data = data;
}
private ModifiableData data;
}
}
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6852569
The good news are for Java 7

Categories