How to use findClass method of org.mockitoutil.ClassLoaders class

Best Mockito code snippet using org.mockitoutil.ClassLoaders.findClass

Source:Math_72_rank-2_new.java Github

copy

Full Screen

...72 super(urls, classLoader);73 this.privateCopyPrefixes = privateCopyPrefixes;74 }75 @Override76 public Class<?> findClass(String name) throws ClassNotFoundException {77 if(classShouldBePrivate(name)) return super.findClass(name);78 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);79 }80 private boolean classShouldBePrivate(String name) {81 for (String prefix : privateCopyPrefixes) {82 if (name.startsWith(prefix)) return true;83 }84 return false;85 }86 }87 public static class ExcludingURLClassLoaderBuilder extends ClassLoaders {88 private final ArrayList<String> privateCopyPrefixes = new ArrayList<String>();89 private final ArrayList<URL> codeSourceUrls = new ArrayList<URL>();90 public ExcludingURLClassLoaderBuilder without(String... privatePrefixes) {91 privateCopyPrefixes.addAll(asList(privatePrefixes));92 return this;93 }94 public ExcludingURLClassLoaderBuilder withCodeSourceUrls(String... urls) {95 codeSourceUrls.addAll(pathsToURLs(urls));96 return this;97 }98 public ExcludingURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {99 for (Class<?> clazz : classes) {100 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));101 }102 return this;103 }104 public ExcludingURLClassLoaderBuilder withCurrentCodeSourceUrls() {105 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));106 return this;107 }108 public ClassLoader build() {109 return new LocalExcludingURLClassLoader(110 jdkClassLoader(),111 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),112 privateCopyPrefixes113 );114 }115 }116 static class LocalExcludingURLClassLoader extends URLClassLoader {117 private final ArrayList<String> privateCopyPrefixes;118 public LocalExcludingURLClassLoader(ClassLoader classLoader, URL[] urls, ArrayList<String> privateCopyPrefixes) {119 super(urls, classLoader);120 this.privateCopyPrefixes = privateCopyPrefixes;121 }122 @Override123 public Class<?> findClass(String name) throws ClassNotFoundException {124 if(classShouldBePrivate(name)) throw new ClassNotFoundException("classes with prefix : " + privateCopyPrefixes + " are excluded");125 return super.findClass(name);126 }127 private boolean classShouldBePrivate(String name) {128 for (String prefix : privateCopyPrefixes) {129 if (name.startsWith(prefix)) return true;130 }131 return false;132 }133 }134 public static class InMemoryClassLoaderBuilder extends ClassLoaders {135 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();136 public InMemoryClassLoaderBuilder withParent(ClassLoader parent) {137 this.parent = parent;138 return this;139 }140 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {141 inMemoryClassObjects.put(name, classDefinition);142 return this;143 }144 public ClassLoader build() {145 return new InMemoryClassLoader(parent, inMemoryClassObjects);146 }147 }148 static class InMemoryClassLoader extends ClassLoader {149 public static final String SCHEME = "mem";150 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();151 public InMemoryClassLoader(ClassLoader parent, Map<String, byte[]> inMemoryClassObjects) {152 super(parent);153 this.inMemoryClassObjects = inMemoryClassObjects;154 }155 protected Class findClass(String name) throws ClassNotFoundException {156 byte[] classDefinition = inMemoryClassObjects.get(name);157 if (classDefinition != null) {158 return defineClass(name, classDefinition, 0, classDefinition.length);159 }160 throw new ClassNotFoundException(name);161 }162 @Override163 public Enumeration<URL> getResources(String ignored) throws IOException {164 return inMemoryOnly();165 }166 private Enumeration<URL> inMemoryOnly() {167 final Set<String> names = inMemoryClassObjects.keySet();168 return new Enumeration<URL>() {169 private final MemHandler memHandler = new MemHandler(InMemoryClassLoader.this);170 private final Iterator<String> it = names.iterator();171 public boolean hasMoreElements() {172 return it.hasNext();173 }174 public URL nextElement() {175 try {176 return new URL(null, SCHEME + ":" + it.next(), memHandler);177 } catch (MalformedURLException rethrown) {178 throw new IllegalStateException(rethrown);179 }180 }181 };182 }183 }184 public static class MemHandler extends URLStreamHandler {185 private InMemoryClassLoader inMemoryClassLoader;186 public MemHandler(InMemoryClassLoader inMemoryClassLoader) {187 this.inMemoryClassLoader = inMemoryClassLoader;188 }189 @Override190 protected URLConnection openConnection(URL url) throws IOException {191 return new MemURLConnection(url, inMemoryClassLoader);192 }193 private static class MemURLConnection extends URLConnection {194 private final InMemoryClassLoader inMemoryClassLoader;195 private String qualifiedName;196 public MemURLConnection(URL url, InMemoryClassLoader inMemoryClassLoader) {197 super(url);198 this.inMemoryClassLoader = inMemoryClassLoader;199 qualifiedName = url.getPath();200 }201 @Override202 public void connect() throws IOException { }203 @Override204 public InputStream getInputStream() throws IOException {205 return new ByteArrayInputStream(inMemoryClassLoader.inMemoryClassObjects.get(qualifiedName));206 }207 }208 }209 protected URL obtainClassPathOf(String className) {210 String path = className.replace('.', '/') + ".class";211 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();212 try {213 return new URL(url.substring(0, url.length() - path.length()));214 } catch (MalformedURLException e) {215 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);216 }217 }218 protected List<URL> pathsToURLs(String... codeSourceUrls) {219 return pathsToURLs(Arrays.asList(codeSourceUrls));220 }221 private List<URL> pathsToURLs(List<String> codeSourceUrls) {222 ArrayList<URL> urls = new ArrayList<URL>(codeSourceUrls.size());223 for (String codeSourceUrl : codeSourceUrls) {224 URL url = pathToUrl(codeSourceUrl);225 urls.add(url);226 }227 return urls;228 }229 private URL pathToUrl(String path) {230 try {231 return new File(path).getAbsoluteFile().toURI().toURL();232 } catch (MalformedURLException e) {233 throw new IllegalArgumentException("Path is malformed", e);234 }235 }236 public static class ReachableClassesFinder {237 private ClassLoader classLoader;238 private Set<String> qualifiedNameSubstring = new HashSet<String>();239 public ReachableClassesFinder(ClassLoader classLoader) {240 this.classLoader = classLoader;241 }242 public ReachableClassesFinder omit(String... qualifiedNameSubstring) {243 this.qualifiedNameSubstring.addAll(Arrays.asList(qualifiedNameSubstring));244 return this;245 }246 public Set<String> listOwnedClasses() throws IOException, URISyntaxException {247 Enumeration<URL> roots = classLoader.getResources("");248 Set<String> classes = new HashSet<String>();249 while(roots.hasMoreElements()) {250 URI uri = roots.nextElement().toURI();251 if (uri.getScheme().equalsIgnoreCase("file")) {252 addFromFileBasedClassLoader(classes, uri);253 } else if(uri.getScheme().equalsIgnoreCase(InMemoryClassLoader.SCHEME)) {254 addFromInMemoryBasedClassLoader(classes, uri);255 } else {256 throw new IllegalArgumentException(String.format("Given ClassLoader '%s' don't have reachable by File or vi ClassLoaders.inMemory", classLoader));257 }258 }259 return classes;260 }261 private void addFromFileBasedClassLoader(Set<String> classes, URI uri) {262 File root = new File(uri);263 classes.addAll(findClassQualifiedNames(root, root, qualifiedNameSubstring));264 }265 private void addFromInMemoryBasedClassLoader(Set<String> classes, URI uri) {266 String qualifiedName = uri.getSchemeSpecificPart();267 if(excludes(qualifiedName, qualifiedNameSubstring)) {268 classes.add(qualifiedName);269 }270 }271 private Set<String> findClassQualifiedNames(File root, File file, Set<String> packageFilters) {272 if(file.isDirectory()) {273 File[] files = file.listFiles();274 Set<String> classes = new HashSet<String>();275 for (File children : files) {276 classes.addAll(findClassQualifiedNames(root, children, packageFilters));277 }278 return classes;279 } else {280 if (file.getName().endsWith(".class")) {281 String qualifiedName = classNameFor(root, file);282 if (excludes(qualifiedName, packageFilters)) {283 return Collections.singleton(qualifiedName);284 }285 }286 }287 return Collections.emptySet();288 }289 private boolean excludes(String qualifiedName, Set<String> packageFilters) {290 for (String filter : packageFilters) {...

Full Screen

Full Screen

Source:Math_72_rank-1_new.java Github

copy

Full Screen

...72 super(urls, classLoader);73 this.privateCopyPrefixes = privateCopyPrefixes;74 }75 @Override76 public Class<?> findClass(String name) throws ClassNotFoundException {77 if(classShouldBePrivate(name)) return super.findClass(name);78 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);79 }80 private boolean classShouldBePrivate(String name) {81 for (String prefix : privateCopyPrefixes) {82 if (name.startsWith(prefix)) return true;83 }84 return false;85 }86 }87 public static class ExcludingURLClassLoaderBuilder extends ClassLoaders {88 private final ArrayList<String> privateCopyPrefixes = new ArrayList<String>();89 private final ArrayList<URL> codeSourceUrls = new ArrayList<URL>();90 public ExcludingURLClassLoaderBuilder without(String... privatePrefixes) {91 privateCopyPrefixes.addAll(asList(privatePrefixes));92 return this;93 }94 public ExcludingURLClassLoaderBuilder withCodeSourceUrls(String... urls) {95 codeSourceUrls.addAll(pathsToURLs(urls));96 return this;97 }98 public ExcludingURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {99 for (Class<?> clazz : classes) {100 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));101 }102 return this;103 }104 public ExcludingURLClassLoaderBuilder withCurrentCodeSourceUrls() {105 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));106 return this;107 }108 public ClassLoader build() {109 return new LocalExcludingURLClassLoader(110 jdkClassLoader(),111 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),112 privateCopyPrefixes113 );114 }115 }116 static class LocalExcludingURLClassLoader extends URLClassLoader {117 private final ArrayList<String> privateCopyPrefixes;118 public LocalExcludingURLClassLoader(ClassLoader classLoader, URL[] urls, ArrayList<String> privateCopyPrefixes) {119 super(urls, classLoader);120 this.privateCopyPrefixes = privateCopyPrefixes;121 }122 @Override123 public Class<?> findClass(String name) throws ClassNotFoundException {124 if(classShouldBePrivate(name)) throw new ClassNotFoundException("classes with prefix : " + privateCopyPrefixes + " are excluded");125 return super.findClass(name);126 }127 private boolean classShouldBePrivate(String name) {128 for (String prefix : privateCopyPrefixes) {129 if (name.startsWith(prefix)) return true;130 }131 return false;132 }133 }134 public static class InMemoryClassLoaderBuilder extends ClassLoaders {135 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();136 public InMemoryClassLoaderBuilder withParent(ClassLoader parent) {137 this.parent = parent;138 return this;139 }140 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {141 inMemoryClassObjects.put(name, classDefinition);142 return this;143 }144 public ClassLoader build() {145 return new InMemoryClassLoader(parent, inMemoryClassObjects);146 }147 }148 static class InMemoryClassLoader extends ClassLoader {149 public static final String SCHEME = "mem";150 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();151 public InMemoryClassLoader(ClassLoader parent, Map<String, byte[]> inMemoryClassObjects) {152 super(parent);153 this.inMemoryClassObjects = inMemoryClassObjects;154 }155 protected Class findClass(String name) throws ClassNotFoundException {156 byte[] classDefinition = inMemoryClassObjects.get(name);157 if (classDefinition != null) {158 return defineClass(name, classDefinition, 0, classDefinition.length);159 }160 throw new ClassNotFoundException(name);161 }162 @Override163 public Enumeration<URL> getResources(String ignored) throws IOException {164 return inMemoryOnly();165 }166 private Enumeration<URL> inMemoryOnly() {167 final Set<String> names = inMemoryClassObjects.keySet();168 return new Enumeration<URL>() {169 private final MemHandler memHandler = new MemHandler(InMemoryClassLoader.this);170 private final Iterator<String> it = names.iterator();171 public boolean hasMoreElements() {172 return it.hasNext();173 }174 public URL nextElement() {175 try {176 return new URL(null, SCHEME + ":" + it.next(), memHandler);177 } catch (MalformedURLException rethrown) {178 throw new IllegalStateException(rethrown);179 }180 }181 };182 }183 }184 public static class MemHandler extends URLStreamHandler {185 private InMemoryClassLoader inMemoryClassLoader;186 public MemHandler(InMemoryClassLoader inMemoryClassLoader) {187 this.inMemoryClassLoader = inMemoryClassLoader;188 }189 @Override190 protected URLConnection openConnection(URL url) throws IOException {191 return new MemURLConnection(url, inMemoryClassLoader);192 }193 private static class MemURLConnection extends URLConnection {194 private final InMemoryClassLoader inMemoryClassLoader;195 private String qualifiedName;196 public MemURLConnection(URL url, InMemoryClassLoader inMemoryClassLoader) {197 super(url);198 this.inMemoryClassLoader = inMemoryClassLoader;199 qualifiedName = url.getPath();200 }201 @Override202 public void connect() throws IOException { }203 @Override204 public InputStream getInputStream() throws IOException {205 return new ByteArrayInputStream(inMemoryClassLoader.inMemoryClassObjects.get(qualifiedName));206 }207 }208 }209 protected URL obtainClassPathOf(String className) {210 String path = className.replace('.', '/') + ".class";211 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();212 try {213 return new URL(url.substring(0, url.length() - path.length()));214 } catch (MalformedURLException e) {215 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);216 }217 }218 protected List<URL> pathsToURLs(String... codeSourceUrls) {219 return pathsToURLs(Arrays.asList(codeSourceUrls));220 }221 private List<URL> pathsToURLs(List<String> codeSourceUrls) {222 ArrayList<URL> urls = new ArrayList<URL>(codeSourceUrls.size());223 for (String codeSourceUrl : codeSourceUrls) {224 URL url = pathToUrl(codeSourceUrl);225 urls.add(url);226 }227 return urls;228 }229 private URL pathToUrl(String path) {230 try {231 return new File(path).getAbsoluteFile().toURI().toURL();232 } catch (MalformedURLException e) {233 throw new IllegalArgumentException("Path is malformed", e);234 }235 }236 public static class ReachableClassesFinder {237 private ClassLoader classLoader;238 private Set<String> qualifiedNameSubstring = new HashSet<String>();239 public ReachableClassesFinder(ClassLoader classLoader) {240 this.classLoader = classLoader;241 }242 public ReachableClassesFinder omit(String... qualifiedNameSubstring) {243 this.qualifiedNameSubstring.addAll(Arrays.asList(qualifiedNameSubstring));244 return this;245 }246 public Set<String> listOwnedClasses() throws IOException, URISyntaxException {247 Enumeration<URL> roots = classLoader.getResources("");248 Set<String> classes = new HashSet<String>();249 while(roots.hasMoreElements()) {250 URI uri = roots.nextElement().toURI();251 if (uri.getScheme().equalsIgnoreCase("file")) {252 addFromFileBasedClassLoader(classes, uri);253 } else if(uri.getScheme().equalsIgnoreCase(InMemoryClassLoader.SCHEME)) {254 addFromInMemoryBasedClassLoader(classes, uri);255 } else {256 throw new IllegalArgumentException(String.format("Given ClassLoader '%s' don't have reachable by File or vi ClassLoaders.inMemory", classLoader));257 }258 }259 return classes;260 }261 private void addFromFileBasedClassLoader(Set<String> classes, URI uri) {262 File root = new File(uri);263 classes.addAll(findClassQualifiedNames(root, root, qualifiedNameSubstring));264 }265 private void addFromInMemoryBasedClassLoader(Set<String> classes, URI uri) {266 String qualifiedName = uri.getSchemeSpecificPart();267 if(excludes(qualifiedName, qualifiedNameSubstring)) {268 classes.add(qualifiedName);269 }270 }271 private Set<String> findClassQualifiedNames(File root, File file, Set<String> packageFilters) {272 if(file.isDirectory()) {273 File[] files = file.listFiles();274 Set<String> classes = new HashSet<String>();275 for (File children : files) {276 classes.addAll(findClassQualifiedNames(root, children, packageFilters));277 }278 return classes;279 } else {280 if (file.getName().endsWith(".class")) {281 String qualifiedName = classNameFor(root, file);282 if (excludes(qualifiedName, packageFilters)) {283 return Collections.singleton(qualifiedName);284 }285 }286 }287 return Collections.emptySet();288 }289 private boolean excludes(String qualifiedName, Set<String> packageFilters) {290 for (String filter : packageFilters) {...

Full Screen

Full Screen

Source:Math_72_rank-2_old.java Github

copy

Full Screen

...50 super(urls, null);51 this.privateCopyPrefixes = privateCopyPrefixes;52 }53 @Override54 public Class<?> findClass(String name) throws ClassNotFoundException {55 if(classShouldBePrivate(name)) return super.findClass(name);56 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);57 }58 private boolean classShouldBePrivate(String name) {59 for (String prefix : privateCopyPrefixes) {60 if (name.startsWith(prefix)) return true;61 }62 return false;63 }64 }65 public static class ExcludingURLClassLoaderBuilder extends ClassLoaders {66 private final ArrayList<String> privateCopyPrefixes = new ArrayList<String>();67 private final ArrayList<URL> codeSourceUrls = new ArrayList<URL>();68 public ExcludingURLClassLoaderBuilder without(String... privatePrefixes) {69 privateCopyPrefixes.addAll(asList(privatePrefixes));70 return this;71 }72 public ExcludingURLClassLoaderBuilder withCodeSourceUrls(String... urls) {73 codeSourceUrls.addAll(pathsToURLs(urls));74 return this;75 }76 public ExcludingURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {77 for (Class<?> clazz : classes) {78 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));79 }80 return this;81 }82 public ExcludingURLClassLoaderBuilder withCurrentCodeSourceUrls() {83 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));84 return this;85 }86 public ClassLoader build() {87 return new LocalExcludingURLClassLoader(88 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),89 privateCopyPrefixes90 );91 }92 }93 static class LocalExcludingURLClassLoader extends URLClassLoader {94 private final ArrayList<String> privateCopyPrefixes;95 public LocalExcludingURLClassLoader(URL[] urls, ArrayList<String> privateCopyPrefixes) {96 super(urls, null);97 this.privateCopyPrefixes = privateCopyPrefixes;98 }99 @Override100 public Class<?> findClass(String name) throws ClassNotFoundException {101 if(classShouldBePrivate(name)) throw new ClassNotFoundException("classes with prefix : " + privateCopyPrefixes + " are excluded");102 return super.findClass(name);103 }104 private boolean classShouldBePrivate(String name) {105 for (String prefix : privateCopyPrefixes) {106 if (name.startsWith(prefix)) return true;107 }108 return false;109 }110 }111 public static class InMemoryClassLoaderBuilder extends ClassLoaders {112 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();113 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {114 inMemoryClassObjects.put(name, classDefinition);115 return this;116 }117 public ClassLoader build() {118 return new InMemoryClassLoader(inMemoryClassObjects);119 }120 }121 static class InMemoryClassLoader extends ClassLoader {122 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();123 public InMemoryClassLoader(Map<String, byte[]> inMemoryClassObjects) {124 this.inMemoryClassObjects = inMemoryClassObjects;125 }126 protected Class findClass(String name) throws ClassNotFoundException {127 byte[] classDefinition = inMemoryClassObjects.get(name);128 if (classDefinition != null) {129 return defineClass(name, classDefinition, 0, classDefinition.length);130 }131 throw new ClassNotFoundException(name);132 }133 }134 protected URL obtainClassPathOf(String className) {135 String path = className.replace('.', '/') + ".class";136 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();137 try {138 return new URL(url.substring(0, url.length() - path.length()));139 } catch (MalformedURLException e) {140 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);...

Full Screen

Full Screen

Source:Math_72_rank-1_old.java Github

copy

Full Screen

...50 super(urls, null);51 this.privateCopyPrefixes = privateCopyPrefixes;52 }53 @Override54 public Class<?> findClass(String name) throws ClassNotFoundException {55 if(classShouldBePrivate(name)) return super.findClass(name);56 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);57 }58 private boolean classShouldBePrivate(String name) {59 for (String prefix : privateCopyPrefixes) {60 if (name.startsWith(prefix)) return true;61 }62 return false;63 }64 }65 public static class ExcludingURLClassLoaderBuilder extends ClassLoaders {66 private final ArrayList<String> privateCopyPrefixes = new ArrayList<String>();67 private final ArrayList<URL> codeSourceUrls = new ArrayList<URL>();68 public ExcludingURLClassLoaderBuilder without(String... privatePrefixes) {69 privateCopyPrefixes.addAll(asList(privatePrefixes));70 return this;71 }72 public ExcludingURLClassLoaderBuilder withCodeSourceUrls(String... urls) {73 codeSourceUrls.addAll(pathsToURLs(urls));74 return this;75 }76 public ExcludingURLClassLoaderBuilder withCodeSourceUrlOf(Class<?>... classes) {77 for (Class<?> clazz : classes) {78 codeSourceUrls.add(obtainClassPathOf(clazz.getName()));79 }80 return this;81 }82 public ExcludingURLClassLoaderBuilder withCurrentCodeSourceUrls() {83 codeSourceUrls.add(obtainClassPathOf(ClassLoaders.class.getName()));84 return this;85 }86 public ClassLoader build() {87 return new LocalExcludingURLClassLoader(88 codeSourceUrls.toArray(new URL[codeSourceUrls.size()]),89 privateCopyPrefixes90 );91 }92 }93 static class LocalExcludingURLClassLoader extends URLClassLoader {94 private final ArrayList<String> privateCopyPrefixes;95 public LocalExcludingURLClassLoader(URL[] urls, ArrayList<String> privateCopyPrefixes) {96 super(urls, null);97 this.privateCopyPrefixes = privateCopyPrefixes;98 }99 @Override100 public Class<?> findClass(String name) throws ClassNotFoundException {101 if(classShouldBePrivate(name)) throw new ClassNotFoundException("classes with prefix : " + privateCopyPrefixes + " are excluded");102 return super.findClass(name);103 }104 private boolean classShouldBePrivate(String name) {105 for (String prefix : privateCopyPrefixes) {106 if (name.startsWith(prefix)) return true;107 }108 return false;109 }110 }111 public static class InMemoryClassLoaderBuilder extends ClassLoaders {112 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();113 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {114 inMemoryClassObjects.put(name, classDefinition);115 return this;116 }117 public ClassLoader build() {118 return new InMemoryClassLoader(inMemoryClassObjects);119 }120 }121 static class InMemoryClassLoader extends ClassLoader {122 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();123 public InMemoryClassLoader(Map<String, byte[]> inMemoryClassObjects) {124 this.inMemoryClassObjects = inMemoryClassObjects;125 }126 protected Class findClass(String name) throws ClassNotFoundException {127 byte[] classDefinition = inMemoryClassObjects.get(name);128 if (classDefinition != null) {129 return defineClass(name, classDefinition, 0, classDefinition.length);130 }131 throw new ClassNotFoundException(name);132 }133 }134 protected URL obtainClassPathOf(String className) {135 String path = className.replace('.', '/') + ".class";136 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();137 try {138 return new URL(url.substring(0, url.length() - path.length()));139 } catch (MalformedURLException e) {140 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);...

Full Screen

Full Screen

Source:ClassLoaders.java Github

copy

Full Screen

...45 super(urls, null);46 this.privateCopyPrefixes = privateCopyPrefixes;47 }48 @Override49 public Class<?> findClass(String name) throws ClassNotFoundException {50 if(classShouldBePrivate(name)) return super.findClass(name);51 throw new ClassNotFoundException("Can only load classes with prefix : " + privateCopyPrefixes);52 }53 private boolean classShouldBePrivate(String name) {54 for (String prefix : privateCopyPrefixes) {55 if (name.startsWith(prefix)) return true;56 }57 return false;58 }59 }60 public static class InMemoryClassLoaderBuilder extends ClassLoaders {61 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();62 public InMemoryClassLoaderBuilder withClassDefinition(String name, byte[] classDefinition) {63 inMemoryClassObjects.put(name, classDefinition);64 return this;65 }66 public ClassLoader build() {67 return new InMemoryClassLoader(inMemoryClassObjects);68 }69 }70 static class InMemoryClassLoader extends ClassLoader {71 private Map<String , byte[]> inMemoryClassObjects = new HashMap<String , byte[]>();72 public InMemoryClassLoader(Map<String, byte[]> inMemoryClassObjects) {73 this.inMemoryClassObjects = inMemoryClassObjects;74 }75 protected Class findClass(String name) throws ClassNotFoundException {76 byte[] classDefinition = inMemoryClassObjects.get(name);77 if (classDefinition != null) {78 return defineClass(name, classDefinition, 0, classDefinition.length);79 }80 throw new ClassNotFoundException(name);81 }82 }83 protected URL obtainClassPathOf(String className) {84 String path = className.replace('.', '/') + ".class";85 String url = ClassLoaders.class.getClassLoader().getResource(path).toExternalForm();86 try {87 return new URL(url.substring(0, url.length() - path.length()));88 } catch (MalformedURLException e) {89 throw new RuntimeException("Classloader couldn't obtain a proper classpath URL", e);...

Full Screen

Full Screen

findClass

Using AI Code Generation

copy

Full Screen

1import org.mockitoutil.ClassLoaders;2import java.lang.reflect.Method;3import java.lang.reflect.InvocationTargetException;4public class 1 {5 public static void main(String[] args) {6 try {7 ClassLoader loader = ClassLoaders.inMemoryClassLoader()8 .withClassDefinition("com.test.Hello", "package com.test; public class Hello { public static String sayHello() { return \"Hello World\"; } }")9 .build();10 Class<?> clazz = loader.loadClass("com.test.Hello");11 Method method = clazz.getMethod("sayHello");12 String result = (String) method.invoke(null);13 System.out.println(result);14 } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {15 e.printStackTrace();16 }17 }18}

Full Screen

Full Screen

findClass

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.creation.bytebuddy;2import java.lang.reflect.InvocationTargetException;3import java.lang.reflect.Method;4import org.junit.Test;5import org.mockito.internal.creation.bytebuddy.MockMethodInterceptor;6import org.mockitoutil.ClassLoaders;7public class MockMethodInterceptorTest {8 public void test() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {9 ClassLoader classLoader = ClassLoaders.inMemoryClassLoader().withClassDefinition(10 ClassFileExtraction.extract(MockMethodInterceptor.class))11 .build();12 Class<?> clazz = classLoader.loadClass(MockMethodInterceptor.class.getName());13 Method findClass = clazz.getDeclaredMethod("findClass", ClassLoader.class, String.class);14 findClass.setAccessible(true);15 findClass.invoke(null, classLoader, MockMethodInterceptorTest.class.getName());16 }17}18package org.mockito.internal.creation.bytebuddy;19import org.junit.Test;20public class MockMethodInterceptorTest {21 public void test() {22 }23}24package org.mockitoutil;25import java.io.File;26import java.io.FileOutputStream;27import java.io.IOException;28import java.io.InputStream;29import java.lang.reflect.Constructor;30import java.lang.reflect.InvocationTargetException;31import java.net.URL;32import java.net.URLClassLoader;33import java.util.ArrayList;34import java.util.List;35public class ClassLoaders {36 private static final String CLASS_EXTENSION = ".class";37 public static ClassLoader inMemoryClassLoader() {38 return new InMemoryClassLoader();39 }40 public static ClassLoader withClassPath(Class<?>... classes) {41 List<URL> urls = new ArrayList<URL>(classes.length);42 for (Class<?> clazz : classes) {43 URL resource = clazz.getResource(clazz.getSimpleName() + CLASS_EXTENSION);44 if (resource == null) {45 throw new IllegalArgumentException("Cannot find class " + clazz.getName() + " on classpath");46 }47 urls.add(resource);48 }49 return new URLClassLoader(urls.toArray(new URL[urls.size()]));50 }51 public static ClassLoader withClassDefinition(Class<?> clazz, byte[] classDefinition) {52 return new InMemoryClassLoader(new ClassDefinition(clazz, classDefinition));53 }54 public static ClassLoader withClassDefinition(Class<?> clazz, byte[] classDefinition, ClassLoader parent) {55 return new InMemoryClassLoader(parent, new ClassDefinition(clazz,

Full Screen

Full Screen

findClass

Using AI Code Generation

copy

Full Screen

1package org.mockitoutil;2import java.io.File;3import java.io.IOException;4import java.net.URL;5import java.net.URLClassLoader;6public class ClassLoaders {7 public static Class<?> findClass(String className, String classFileName) {8 ClassLoader classLoader = new URLClassLoader(new URL[] { new File(classFileName).toURI().toURL() });9 try {10 return classLoader.loadClass(className);11 } catch (ClassNotFoundException e) {12 throw new RuntimeException(e);13 } catch (IOException e) {14 throw new RuntimeException(e);15 }16 }17}18 at org.mockitoutil.ClassLoaders.findClass(ClassLoaders.java:14)19 at com.test.Main.main(Main.java:7)20 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)21 at java.security.AccessController.doPrivileged(Native Method)22 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)23 at java.lang.ClassLoader.loadClass(ClassLoader.java:306)24 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)25 at java.lang.ClassLoader.loadClass(ClassLoader.java:247)26 at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:375)27package org.mockitoutil;28import java.io.File;29import java.io.IOException;30import java.net.URL;31import java.net.URLClassLoader;32public class ClassLoaders {33 public static Class<?> findClass(String className, String classFileName) {34 ClassLoader classLoader = new URLClassLoader(new URL[] { new File(classFileName).toURI().toURL() });35 try {36 return classLoader.loadClass(className);37 } catch (ClassNotFoundException e) {38 throw new RuntimeException(e);39 } catch (IOException e) {40 throw new RuntimeException(e);41 }42 }43}44package com.test;45public class MyClass {46 public void printHello() {47 System.out.println("Hello");48 }49}

Full Screen

Full Screen

findClass

Using AI Code Generation

copy

Full Screen

1import java.lang.reflect.Method;2import java.util.Arrays;3import org.mockitoutil.ClassLoaders;4public class 1 {5 public static void main(String[] args) throws Exception {6 byte[] bytes = new byte[]{-54, -2, -70, -66, 0, 0, 0, 50, 0, 6, 1, 0, 5, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 1, 0, 15, 76, 105, 110, 101, 78, 111, 116, 70, 111, 117, 110, 100, 69, 120, 99, 101, 112, 116, 105, 111, 110, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 10, 84, 101, 115, 116, 46, 106, 97, 118, 97, 12, 0, 4, 0, 5, 1, 0, 10, 84, 101, 115, 116, 46, 99, 108, 97, 115, 115, 12, 0, 7, 0, 8, 1, 0, 4, 109, 97, 105, 110, 1, 0, 22, 40, 91, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful