Skip to content

Commit

Permalink
dynamic class loading
Browse files Browse the repository at this point in the history
  • Loading branch information
witek committed Feb 22, 2018
1 parent c2542bf commit 46c28b2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/main/java/ilarkesto/base/Reflect.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,12 @@ public static Map<String, Object> readAllGetters(Object o, boolean ignoreExcepti
return ret;
}

public static Class getClass(String chartClassName) {
try {
return Class.forName(chartClassName);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}

}
39 changes: 30 additions & 9 deletions src/main/java/ilarkesto/io/DynamicClassLoader.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/*
* Copyright 2011 Witoslaw Koczewsi <[email protected]>, Artjom Kochtchi
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
* General Public License as published by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package ilarkesto.io;

import ilarkesto.base.Reflect;
import ilarkesto.base.Sys;
import ilarkesto.core.logging.Log;

import java.io.File;
Expand Down Expand Up @@ -46,7 +48,7 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {

// Long lastModified = typeModificationTimes.get(name);
// if (lastModified != null && lastModified == file.lastModified()) {
//
//
// }
//
// typeModificationTimes.put(name, file.lastModified());
Expand All @@ -61,4 +63,23 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
return super.loadClass(name);
}

public static <T> T newInstanceDevOnly(Class<T> type) {
return Sys.isDevelopmentMode() ? (T) newInstance(type.getName(), type) : Reflect.newInstance(type);
}

public static <T> T newInstance(Class<T> type) {
return (T) newInstance(type.getName(), type);
}

public static Object newInstance(String className, Class parentClassLoaderClass) {
ClassLoader loader = new DynamicClassLoader(parentClassLoaderClass.getClassLoader(), className);
Class type;
try {
type = loader.loadClass(className);
return type.newInstance();
} catch (Throwable ex) {
throw new RuntimeException(ex);
}
}

}

0 comments on commit 46c28b2

Please sign in to comment.