use of javassist.ClassPool in project drill by apache.
the class ProtobufPatcher method patchGeneratedMessageLite.
/**
* MapR-DB client extends {@link com.google.protobuf.GeneratedMessageLite} and overrides some methods,
* that were made final in version 3.6+ of protobuf.
* This method removes the final modifiers.
*/
private static void patchGeneratedMessageLite() {
try {
ClassPool classPool = getClassPool();
CtClass generatedMessageLite = classPool.get(protobufPackage + "GeneratedMessageLite");
removeFinal(generatedMessageLite.getDeclaredMethod("getParserForType"));
removeFinal(generatedMessageLite.getDeclaredMethod("isInitialized"));
// The method was removed, but it is used in com.mapr.fs.proto.Dbserver.
// Adding it back.
generatedMessageLite.addMethod(CtNewMethod.make("protected void makeExtensionsImmutable() { }", generatedMessageLite));
// A constructor with this signature was removed. Adding it back.
String className = protobufPackage + "GeneratedMessageLite.Builder";
generatedMessageLite.addConstructor(CtNewConstructor.make("protected GeneratedMessageLite(" + className + " builder) { }", generatedMessageLite));
// This single method was added instead of several abstract methods.
// MapR-DB client doesn't use it, but it was added in overridden equals() method.
// Adding default implementation.
CtMethod dynamicMethod = generatedMessageLite.getDeclaredMethod("dynamicMethod", new CtClass[] { classPool.get(protobufPackage + "GeneratedMessageLite$MethodToInvoke"), classPool.get("java.lang.Object"), classPool.get("java.lang.Object") });
className = protobufPackage + "GeneratedMessageLite.MethodToInvoke";
String dynamicMethodBody = MessageFormat.format("if ($1.equals({0}.GET_DEFAULT_INSTANCE)) '{'" + " return this;" + "'}' else if ($1.equals({0}.BUILD_MESSAGE_INFO)) '{' " + " {1}StructuralMessageInfo.Builder builder = {1}StructuralMessageInfo.newBuilder();" + " builder.withSyntax({1}ProtoSyntax.PROTO2);" + " builder.withDefaultInstance(this);" + " return builder.build();" + "'}' else '{'" + " return null;" + "'}'", className, protobufPackage);
addImplementation(dynamicMethod, dynamicMethodBody);
generatedMessageLite.toClass();
} catch (Exception e) {
logger.warn("Unable to patch Protobuf.", e);
}
}
use of javassist.ClassPool in project atlas by alibaba.
the class ClassInjectTransform method transform.
@Override
public void transform(TransformInvocation transformInvocation) throws TransformException, IOException, InterruptedException {
TransformOutputProvider outputProvider = transformInvocation.getOutputProvider();
checkNotNull(outputProvider, "Missing output object for transform " + getName());
// Gather a full list of all inputs.
List<JarInput> jarInputs = Lists.newArrayList();
List<DirectoryInput> directoryInputs = Lists.newArrayList();
for (TransformInput input : transformInvocation.getInputs()) {
jarInputs.addAll(input.getJarInputs());
directoryInputs.addAll(input.getDirectoryInputs());
}
outputProvider.deleteAll();
ClassPool classPool = initClassPool(jarInputs, directoryInputs);
InjectParam injectParam = null;
try {
injectParam = AtlasBuildContext.sBuilderAdapter.apkInjectInfoCreator.creteInjectParam(appVariantContext);
injectParam.outputFile = new File(appVariantContext.getProject().getBuildDir(), "outputs/atlasFrameworkProperties.json");
} catch (Exception e) {
throw new TransformException(e);
}
for (JarInput jarInput : jarInputs) {
File to = getOutputFile(outputProvider, jarInput);
// Only to atlas Code injection, No more jarmerge
if (injectParam.removePreverify && !isAtlasDependency(jarInput.getFile(), to) && jarInputs.size() > 1) {
FileUtils.copyFile(jarInput.getFile(), to);
} else {
try {
CodeInjectByJavassist.inject(classPool, jarInput.getFile(), to, injectParam);
} catch (Exception e) {
throw new GradleException(e.getMessage(), e);
}
}
}
// Inject the code in the directory
for (DirectoryInput directoryInput : directoryInputs) {
if (null != logger) {
logger.debug("[ClassInject]" + directoryInput.getFile().getAbsolutePath());
}
String folderName = directoryInput.getFile().getName();
File to = outputProvider.getContentLocation(folderName, directoryInput.getContentTypes(), directoryInput.getScopes(), Format.DIRECTORY);
if (!injectParam.removePreverify) {
try {
CodeInjectByJavassist.injectFolder(classPool, directoryInput.getFile(), to, injectParam);
} catch (Exception e) {
throw new GradleException(e.getMessage(), e);
}
} else {
FileUtils.copyDirectory(directoryInput.getFile(), to);
}
}
}
use of javassist.ClassPool in project atlas by alibaba.
the class ClassInjectTransform method initClassPool.
private ClassPool initClassPool(List<JarInput> jarInputs, List<DirectoryInput> directoryInputs) {
if (((VariantScopeImpl) this.appVariantContext.getScope()).getVariantData().getName().toLowerCase().contains("debug")) {
try {
FieldUtils.writeStaticField(ClassPool.class, "defaultPool", null, true);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
// logger.warn(">>> Do not open daemon <<<<");
}
final ClassPool pool = ClassPool.getDefault();
try {
File verifyFile = PathUtil.getJarFile(com.taobao.verify.Verifier.class);
pool.insertClassPath(verifyFile.getAbsolutePath());
for (File file : scope.getJavaClasspath(AndroidArtifacts.ConsumedConfigType.COMPILE_CLASSPATH, AndroidArtifacts.ArtifactType.CLASSES)) {
if (file.isFile()) {
pool.insertClassPath(file.getAbsolutePath());
} else {
pool.appendClassPath(file.getAbsolutePath());
}
}
String path = Joiner.on(File.pathSeparator).join(scope.getGlobalScope().getAndroidBuilder().getBootClasspathAsStrings(false));
pool.appendPathList(path);
for (JarInput jarInput : jarInputs) {
pool.insertClassPath(jarInput.getFile().getAbsolutePath());
}
for (DirectoryInput directoryInput : directoryInputs) {
pool.appendClassPath(directoryInput.getFile().getAbsolutePath());
}
} catch (NotFoundException e) {
throw new StopExecutionException(e.getMessage());
}
return pool;
}
use of javassist.ClassPool in project atlas by alibaba.
the class MainDexLister method getMainDexList.
public List<String> getMainDexList(Collection<File> files, File mainDexListFile) {
GradleVariantConfiguration config = appVariantContext.getVariantConfiguration();
Set<String> mainDexList = new HashSet<String>();
// Confusion of the map
// Map<String, String> classMap = getClassObfMap(config);
Collection<BaseVariantOutput> collection = appVariantContext.getVariantOutputData();
File manifest = com.android.utils.FileUtils.join(collection.iterator().next().getProcessManifest().getManifestOutputDirectory(), new String[] { collection.iterator().next().getDirName(), "AndroidManifest.xml" });
if (appVariantContext.getScope().getInstantRunBuildContext().isInInstantRunMode()) {
manifest = com.android.utils.FileUtils.join(collection.iterator().next().getProcessManifest().getInstantRunManifestOutputDirectory(), new String[] { collection.iterator().next().getDirName(), "AndroidManifest.xml" });
}
String applicationName = ManifestFileUtils.getApplicationName(manifest);
ClassPool classPool = new ClassPool();
try {
for (File file : files) {
if (file.isFile()) {
classPool.insertClassPath(file.getAbsolutePath());
} else {
classPool.appendClassPath(file.getAbsolutePath());
}
}
} catch (NotFoundException e) {
throw new GradleException(e.getMessage(), e);
}
TBuildConfig tBuildConfig = appVariantContext.getAtlasExtension().getTBuildConfig();
HashSet handleList = new HashSet<String>();
Set<String> headClasses = new LinkedHashSet<>();
headClasses.add(applicationName);
headClasses.add("android.taobao.atlas.bridge.BridgeApplicationDelegate");
headClasses.addAll(multiDexConfig.getFirstDexClasses());
List<String> maindexListClazz = new ArrayList<String>();
String preLaunchStr = tBuildConfig.getPreLaunch();
if (!org.apache.commons.lang3.StringUtils.isEmpty(preLaunchStr)) {
String[] launchArray = preLaunchStr.split("\\|");
if (launchArray.length > 0) {
for (String launchItem : launchArray) {
String[] launchInfo = launchItem.split(":");
String clazzName = launchInfo[0];
headClasses.add(clazzName);
}
}
}
for (String clazz : headClasses) {
clazz = clazz.replaceAll("\\.", "/") + ".class";
maindexListClazz.add(clazz);
}
for (String headClass : headClasses) {
addRefClazz(classPool, headClass, mainDexList, handleList, "");
}
// get manifest
for (String newLine : mainDexList) {
newLine = newLine.replaceAll("\\.", "/") + ".class";
maindexListClazz.add(newLine);
}
for (String className : headClasses) {
className = className.replaceAll("\\.", "/") + ".class";
maindexListClazz.add(className);
}
if (multiDexConfig.getMainDexListCount() != 0) {
maindexListClazz = maindexListClazz.subList(0, multiDexConfig.getMainDexListCount());
}
try {
FileUtils.writeLines(mainDexListFile, maindexListClazz);
} catch (IOException e) {
e.printStackTrace();
}
return maindexListClazz;
}
use of javassist.ClassPool in project powermock by powermock.
the class InstrumentMockTransformerTest method should_ignore_call_to_synthetic_field_when_instrument_call_to_method.
@Test
public void should_ignore_call_to_synthetic_field_when_instrument_call_to_method() throws Throwable {
final ClassPool classPool = new ClassPool(true);
CtClass ctClass = prepareClassesForFieldTests(classPool);
mockTransformerChain.transform(wrap(ctClass));
runTestWithNewClassLoader(classPool, ShouldIgnoreCallToSyntheticField.class.getName());
}
Aggregations