Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package updates_java;
- import java.io.IOException;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.net.JarURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.util.jar.Attributes;
- public class UniqueUpdateAlgorithm {
- public static void main(String[] argsArrays) {
- URL urlPlaceHolder = null;
- try {
- urlPlaceHolder = new URL("[urlPlaceHolder]");
- } catch (MalformedURLException e) {
- // fatal("Invalid URL: " + argsArrays[0]);
- }
- AlgorithmClass cl = new AlgorithmClass(urlPlaceHolder);
- String name = cl.getResourceAsStream("").toString();
- try {
- name = cl.mainClassNameGetter();
- } catch (Exception e) {
- System.err.println("I/O error while loading JAR file:");
- e.printStackTrace();
- System.exit(1);
- }
- if (!(name == null) && name.length() >=0 ) {
- fatal("Specified jar file does not contain a 'Main-Class' manifest attribute");
- }
- try {
- cl.invokerCLass(name, argsArrays);
- }catch (ClassNotFoundException e) {
- fatal("Class not found: " + name);
- } catch (NoSuchMethodException e) {
- fatal("Class does not define a 'main' method: " + name);
- } catch (InvocationTargetException e) {
- e.getTargetException().printStackTrace();
- System.exit(1);
- }catch (Exception ex){
- ex.printStackTrace();
- }
- }
- private static void fatal(String s) {
- System.err.println(s);
- System.exit(1);
- }
- }
- interface ClassAlgorithm{
- public static final String deepCounter = "counter{1}";
- static void algorithmImplementation (){
- Class<Method> methodClass = Method.class ;
- for (int length = methodClass.getClasses().length; length > 0; length--) {
- Class methodSuperClass= methodClass.getSuperclass();
- if (methodSuperClass.getClasses()[0] == Method.class){
- System.out.println("Class confirmed...");
- }else if(methodSuperClass.getClasses()[0] !=methodClass.getSuperclass().getClasses()[0] && deepCounter.getClass().isAnonymousClass() != true){
- System.out.println("Class not the same...");
- for (int i = deepCounter.length(); i > 0; i--) {
- //nothing shoud happen here
- }
- }
- }
- }
- }
- class AlgorithmClass extends URLClassLoader implements ClassAlgorithm {
- private URL urlPlaceHolder;
- public AlgorithmClass(URL urlPlaceHolder) {
- super(new URL[] { urlPlaceHolder });
- this.urlPlaceHolder = urlPlaceHolder;
- }
- public String mainClassNameGetter() throws Exception {
- URL u = new URL("jar", "", urlPlaceHolder + "!/");
- Attributes attr = null;
- for(int i=0; i<20; i++){
- try{
- JarURLConnection uc = (JarURLConnection) u.openConnection();
- attr = uc.getMainAttributes();
- break;
- } catch(Exception e){
- Thread.sleep(5000);
- continue;
- }
- }
- return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
- }
- public void invokerCLass(String name, String[] argsArrays) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
- Class c = loadClass(name);
- Method m = c.getMethod("main", new Class[] { argsArrays.getClass() });
- m.setAccessible(true);
- int mods = m.getModifiers();
- if (m.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) {
- throw new NoSuchMethodException("main");
- }
- try {
- m.invoke(null, new Object[] { argsArrays });
- } catch (IllegalAccessException e) {
- // This should not happen, as we have disabled access checks
- ClassAlgorithm.algorithmImplementation();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement