-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from deathcap/guava10
Relocate Guava at build and runtime for backward compatibility. Closes GH-5
- Loading branch information
Showing
4 changed files
with
253 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
src/main/java/io/github/deathcap/bukkit2sponge/plugin/DefaultShader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
|
||
// based on http://svn.apache.org/viewvc/maven/plugins/tags/maven-shade-plugin-2.3/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java?revision=1590870&view=markup | ||
// and http://svn.apache.org/viewvc/maven/plugins/tags/maven-shade-plugin-2.3/src/main/java/org/apache/maven/plugins/shade/relocation/SimpleRelocator.java?revision=1590870&view=co | ||
|
||
package io.github.deathcap.bukkit2sponge.plugin; | ||
|
||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
|
||
import java.io.IOException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.commons.Remapper; | ||
import org.objectweb.asm.commons.RemappingClassAdapter; | ||
|
||
|
||
public class DefaultShader | ||
{ | ||
private RelocatorRemapper remapper; | ||
|
||
|
||
public DefaultShader( String inPrefix, String outPrefix) throws IOException | ||
{ | ||
this.remapper = new RelocatorRemapper(inPrefix, outPrefix); | ||
|
||
} | ||
|
||
public byte[] getRemappedClass( byte[] is) | ||
throws IOException | ||
{ | ||
|
||
ClassReader cr = new ClassReader( is ); | ||
ClassWriter cw = new ClassWriter( cr, 0 ); | ||
|
||
ClassVisitor cv = new RemappingClassAdapter( cw, remapper ) | ||
{ | ||
/* | ||
@Override | ||
public void visitSource( final String source, final String debug ) | ||
{ | ||
if ( source == null ) | ||
{ | ||
super.visitSource( source, debug ); | ||
} | ||
else | ||
{ | ||
final String fqSource = pkg + source; | ||
final String mappedSource = remapper.map( fqSource ); | ||
final String filename = mappedSource.substring( mappedSource.lastIndexOf( '/' ) + 1 ); | ||
super.visitSource( filename, debug ); | ||
} | ||
} | ||
*/ | ||
}; | ||
|
||
try | ||
{ | ||
cr.accept( cv, ClassReader.EXPAND_FRAMES ); | ||
} | ||
catch ( Throwable ise ) | ||
{ | ||
throw new RuntimeException( ise ) ; | ||
} | ||
|
||
byte[] renamedClass = cw.toByteArray(); | ||
|
||
return renamedClass; | ||
} | ||
|
||
/** | ||
* @author Jason van Zyl | ||
*/ | ||
|
||
public class RelocatorRemapper | ||
extends Remapper | ||
{ | ||
|
||
private final Pattern classPattern = Pattern.compile( "(\\[*)?L(.+);" ); | ||
private final String inPrefix; | ||
private final String outPrefix; | ||
|
||
public RelocatorRemapper(String inPrefix, String outPrefix) | ||
{ | ||
this.inPrefix = inPrefix; | ||
this.outPrefix = outPrefix; | ||
} | ||
|
||
@Override | ||
public Object mapValue( Object object ) | ||
{ | ||
if ( object instanceof String ) | ||
{ | ||
String name = (String) object; | ||
String value = name; | ||
|
||
String prefix = ""; | ||
String suffix = ""; | ||
|
||
Matcher m = classPattern.matcher( name ); | ||
if ( m.matches() ) | ||
{ | ||
prefix = m.group( 1 ) + "L"; | ||
suffix = ";"; | ||
name = m.group( 2 ); | ||
} | ||
|
||
value = map( name ); | ||
|
||
return value; | ||
} | ||
|
||
return super.mapValue( object ); | ||
} | ||
|
||
@Override | ||
public String map( String name ) | ||
{ | ||
if (name.startsWith(inPrefix)) { | ||
return outPrefix + name; | ||
} else { | ||
return name; | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/main/java/io/github/deathcap/bukkit2sponge/plugin/ShinyClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.github.deathcap.bukkit2sponge.plugin; | ||
|
||
import com.google.common.io.ByteStreams; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.JarURLConnection; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.security.CodeSigner; | ||
import java.security.CodeSource; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ShinyClassLoader extends URLClassLoader { | ||
|
||
private static DefaultShader shader = null; | ||
|
||
private final static String com_google_common = new String(new char[] {'c','o','m','/','g','o','o','g','l','e','/','c','o','m','m','o','n',}); | ||
private final static String inPrefix = com_google_common; | ||
private final static String outPrefix = "io/github/deathcap/bukkit2sponge/libs/guava17/"; | ||
|
||
static { | ||
try { | ||
shader = new DefaultShader(inPrefix, outPrefix); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
public ShinyClassLoader(URL[] urls, ClassLoader parent) throws IOException { | ||
super(urls, parent); | ||
} | ||
|
||
private byte[] relocateBytecode(byte[] bytes) throws IOException { | ||
return shader.getRemappedClass(bytes); | ||
} | ||
|
||
@Override | ||
protected Class<?> findClass(String name) throws ClassNotFoundException { | ||
String path = name.replace('.', '/').concat(".class"); | ||
URL url = this.findResource(path); | ||
if (url == null) { | ||
throw new ClassNotFoundException("Unable to find class resource " + name + " at " + path); | ||
} | ||
|
||
InputStream stream = null; | ||
byte[] bytecode = null; | ||
try { | ||
stream = url.openStream(); | ||
bytecode = ByteStreams.toByteArray(stream); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
throw new ClassNotFoundException("Unable to open or read stream for class " + name + ": " + ex.getLocalizedMessage()); | ||
} | ||
|
||
CodeSource codeSource = null; | ||
try { | ||
JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection(); | ||
URL jarURL = jarURLConnection.getJarFileURL(); | ||
codeSource = new CodeSource(jarURL, new CodeSigner[0]); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
throw new ClassNotFoundException("Unable to read code source for class " + name + ": " + ex.getLocalizedMessage()); | ||
} | ||
|
||
byte[] remappedBytecode; | ||
try { | ||
remappedBytecode = relocateBytecode(bytecode); | ||
} catch (IOException ex) { | ||
ex.printStackTrace(); | ||
//throw new ClassNotFoundException("Unable to relocate class " + name + ": " + ex.getLocalizedMessage()); | ||
remappedBytecode = bytecode; | ||
} | ||
|
||
Class<?> result = this.defineClass(name, remappedBytecode, 0, remappedBytecode.length, codeSource); | ||
if (result != null) { | ||
this.resolveClass(result); | ||
} | ||
|
||
return result; | ||
} | ||
} |