jadx.core.utils.exceptions.JadxException Java Examples

The following examples show how to use jadx.core.utils.exceptions.JadxException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RegionMakerVisitor.java    From Box with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode() || mth.getBasicBlocks().isEmpty()) {
		return;
	}
	RegionMaker rm = new RegionMaker(mth);
	RegionStack state = new RegionStack(mth);

	// fill region structure
	mth.setRegion(rm.makeRegion(mth.getEnterBlock(), state));

	if (!mth.isNoExceptionHandlers()) {
		IRegion expOutBlock = rm.processTryCatchBlocks(mth);
		if (expOutBlock != null) {
			mth.getRegion().add(expOutBlock);
		}
	}
	postProcessRegions(mth);
}
 
Example #2
Source File: RegionMakerVisitor.java    From Box with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode() || mth.getBasicBlocks().isEmpty()) {
		return;
	}
	RegionMaker rm = new RegionMaker(mth);
	RegionStack state = new RegionStack(mth);

	// fill region structure
	mth.setRegion(rm.makeRegion(mth.getEnterBlock(), state));

	if (!mth.isNoExceptionHandlers()) {
		IRegion expOutBlock = rm.processTryCatchBlocks(mth);
		if (expOutBlock != null) {
			mth.getRegion().add(expOutBlock);
		}
	}
	postProcessRegions(mth);
}
 
Example #3
Source File: DeboxingVisitor.java    From Box with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode()) {
		return;
	}
	boolean replaced = false;
	for (BlockNode blockNode : mth.getBasicBlocks()) {
		List<InsnNode> insnList = blockNode.getInstructions();
		int count = insnList.size();
		for (int i = 0; i < count; i++) {
			InsnNode insnNode = insnList.get(i);
			if (insnNode.getType() == InsnType.INVOKE) {
				InsnNode replaceInsn = checkForReplace(((InvokeNode) insnNode));
				if (replaceInsn != null) {
					BlockUtils.replaceInsn(blockNode, i, replaceInsn);
					replaced = true;
				}
			}
		}
	}
	if (replaced) {
		ConstInlineVisitor.process(mth);
	}
}
 
Example #4
Source File: RegionMakerVisitor.java    From jadx with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode() || mth.getBasicBlocks().isEmpty()) {
		return;
	}
	RegionMaker rm = new RegionMaker(mth);
	RegionStack state = new RegionStack(mth);

	// fill region structure
	mth.setRegion(rm.makeRegion(mth.getEnterBlock(), state));

	if (!mth.isNoExceptionHandlers()) {
		IRegion expOutBlock = rm.processTryCatchBlocks(mth);
		if (expOutBlock != null) {
			mth.getRegion().add(expOutBlock);
		}
	}
	postProcessRegions(mth);
}
 
Example #5
Source File: ReSugarCode.java    From Box with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode()) {
		return;
	}
	InsnRemover remover = new InsnRemover(mth);
	for (BlockNode block : mth.getBasicBlocks()) {
		remover.setBlock(block);
		List<InsnNode> instructions = block.getInstructions();
		int size = instructions.size();
		for (int i = 0; i < size; i++) {
			process(mth, instructions, i, remover);
		}
		remover.perform();
	}
}
 
Example #6
Source File: ResourcesLoader.java    From Box with Apache License 2.0 6 votes vote down vote up
public static <T> T decodeStream(ResourceFile rf, ResourceDecoder<T> decoder) throws JadxException {
	try {
		ZipRef zipRef = rf.getZipRef();
		if (zipRef == null) {
			File file = new File(rf.getName());
			try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
				return decoder.decode(file.length(), inputStream);
			}
		} else {
			try (ZipFile zipFile = new ZipFile(zipRef.getZipFile())) {
				ZipEntry entry = zipFile.getEntry(zipRef.getEntryName());
				if (entry == null) {
					throw new IOException("Zip entry not found: " + zipRef);
				}
				if (!ZipSecurity.isValidZipEntry(entry)) {
					return null;
				}
				try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry))) {
					return decoder.decode(entry.getSize(), inputStream);
				}
			}
		}
	} catch (Exception e) {
		throw new JadxException("Error decode: " + rf.getName(), e);
	}
}
 
Example #7
Source File: DeboxingVisitor.java    From jadx with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode()) {
		return;
	}
	boolean replaced = false;
	for (BlockNode blockNode : mth.getBasicBlocks()) {
		List<InsnNode> insnList = blockNode.getInstructions();
		int count = insnList.size();
		for (int i = 0; i < count; i++) {
			InsnNode insnNode = insnList.get(i);
			if (insnNode.getType() == InsnType.INVOKE) {
				InsnNode replaceInsn = checkForReplace(((InvokeNode) insnNode));
				if (replaceInsn != null) {
					BlockUtils.replaceInsn(mth, blockNode, i, replaceInsn);
					replaced = true;
				}
			}
		}
	}
	if (replaced) {
		ConstInlineVisitor.process(mth);
	}
}
 
Example #8
Source File: ResourcesLoader.java    From jadx with Apache License 2.0 6 votes vote down vote up
public static <T> T decodeStream(ResourceFile rf, ResourceDecoder<T> decoder) throws JadxException {
	try {
		ZipRef zipRef = rf.getZipRef();
		if (zipRef == null) {
			File file = new File(rf.getName());
			try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
				return decoder.decode(file.length(), inputStream);
			}
		} else {
			try (ZipFile zipFile = new ZipFile(zipRef.getZipFile())) {
				ZipEntry entry = zipFile.getEntry(zipRef.getEntryName());
				if (entry == null) {
					throw new IOException("Zip entry not found: " + zipRef);
				}
				if (!ZipSecurity.isValidZipEntry(entry)) {
					return null;
				}
				try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry))) {
					return decoder.decode(entry.getSize(), inputStream);
				}
			}
		}
	} catch (Exception e) {
		throw new JadxException("Error decode: " + rf.getName(), e);
	}
}
 
Example #9
Source File: InputFile.java    From Box with Apache License 2.0 6 votes vote down vote up
private static List<Path> loadFromJar(Path jar) throws DecodeException {
	JavaToDex j2d = new JavaToDex();
	try {
		LOG.info("converting to dex: {} ...", jar.getFileName());
		List<Path> pathList = j2d.convert(jar);
		if (pathList.isEmpty()) {
			throw new JadxException("Empty dx output");
		}
		if (LOG.isDebugEnabled()) {
			LOG.debug("result dex files: {}", pathList);
		}
		return pathList;
	} catch (Exception e) {
		throw new DecodeException("java class to dex conversion error:\n " + e.getMessage(), e);
	} finally {
		if (j2d.isError()) {
			LOG.warn("dx message: {}", j2d.getDxErrors());
		}
	}
}
 
Example #10
Source File: PrepareForCodeGen.java    From jadx with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	List<BlockNode> blocks = mth.getBasicBlocks();
	if (blocks == null) {
		return;
	}
	for (BlockNode block : blocks) {
		if (block.contains(AFlag.DONT_GENERATE)) {
			continue;
		}
		removeInstructions(block);
		checkInline(block);
		removeParenthesis(block);
		modifyArith(block);
	}
	moveConstructorInConstructor(mth);
}
 
Example #11
Source File: OverrideMethodVisitor.java    From jadx with Apache License 2.0 6 votes vote down vote up
@Override
public boolean visit(ClassNode cls) throws JadxException {
	RootNode root = cls.root();
	List<ArgType> superTypes = collectSuperTypes(cls);
	for (MethodNode mth : cls.getMethods()) {
		if (mth.isConstructor()) {
			continue;
		}
		String signature = mth.getMethodInfo().makeSignature(false);
		List<IMethodDetails> overrideList = collectOverrideMethods(root, superTypes, signature);
		if (!overrideList.isEmpty()) {
			mth.addAttr(new MethodOverrideAttr(overrideList));
			fixMethodReturnType(mth, overrideList, superTypes);
			fixMethodArgTypes(mth, overrideList, superTypes);
		}
	}
	return true;
}
 
Example #12
Source File: InputFile.java    From Box with Apache License 2.0 6 votes vote down vote up
private static List<Path> loadFromJar(Path jar) throws DecodeException {
	JavaToDex j2d = new JavaToDex();
	try {
		LOG.info("converting to dex: {} ...", jar.getFileName());
		List<Path> pathList = j2d.convert(jar);
		if (pathList.isEmpty()) {
			throw new JadxException("Empty dx output");
		}
		if (LOG.isDebugEnabled()) {
			LOG.debug("result dex files: {}", pathList);
		}
		return pathList;
	} catch (Exception e) {
		throw new DecodeException("java class to dex conversion error:\n " + e.getMessage(), e);
	} finally {
		if (j2d.isError()) {
			LOG.warn("dx message: {}", j2d.getDxErrors());
		}
	}
}
 
Example #13
Source File: JavaToDex.java    From Box with Apache License 2.0 6 votes vote down vote up
public List<Path> convert(Path jar) throws JadxException {
	try (ByteArrayOutputStream out = new ByteArrayOutputStream();
			ByteArrayOutputStream errOut = new ByteArrayOutputStream()) {
		DxContext context = new DxContext(out, errOut);
		Path dir = FileUtils.createTempDir("jar-to-dex-");
		DxArgs args = new DxArgs(
				context,
				dir.toAbsolutePath().toString(),
				new String[] { jar.toAbsolutePath().toString() });
		int result = new Main(context).runDx(args);
		dxErrors = errOut.toString(CHARSET_NAME);
		if (result != 0) {
			throw new JadxException("Java to dex conversion error, code: " + result);
		}
		List<Path> list = new ArrayList<>();
		try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) {
			for (Path child : ds) {
				list.add(child);
				child.toFile().deleteOnExit();
			}
		}
		return list;
	} catch (Exception e) {
		throw new JadxException("dx exception: " + e.getMessage(), e);
	}
}
 
Example #14
Source File: ResourcesLoader.java    From Box with Apache License 2.0 6 votes vote down vote up
public static <T> T decodeStream(ResourceFile rf, ResourceDecoder<T> decoder) throws JadxException {
	try {
		ZipRef zipRef = rf.getZipRef();
		if (zipRef == null) {
			File file = new File(rf.getName());
			try (InputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {
				return decoder.decode(file.length(), inputStream);
			}
		} else {
			try (ZipFile zipFile = new ZipFile(zipRef.getZipFile())) {
				ZipEntry entry = zipFile.getEntry(zipRef.getEntryName());
				if (entry == null) {
					throw new IOException("Zip entry not found: " + zipRef);
				}
				if (!ZipSecurity.isValidZipEntry(entry)) {
					return null;
				}
				try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry))) {
					return decoder.decode(entry.getSize(), inputStream);
				}
			}
		}
	} catch (Exception e) {
		throw new JadxException("Error decode: " + rf.getName(), e);
	}
}
 
Example #15
Source File: ReSugarCode.java    From Box with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode()) {
		return;
	}
	InsnRemover remover = new InsnRemover(mth);
	for (BlockNode block : mth.getBasicBlocks()) {
		remover.setBlock(block);
		List<InsnNode> instructions = block.getInstructions();
		int size = instructions.size();
		for (int i = 0; i < size; i++) {
			process(mth, instructions, i, remover);
		}
		remover.perform();
	}
}
 
Example #16
Source File: JadxCLIArgs.java    From jadx with Apache License 2.0 6 votes vote down vote up
private boolean process(JCommanderWrapper<JadxCLIArgs> jcw) {
	if (printHelp) {
		jcw.printUsage();
		return false;
	}
	if (printVersion) {
		System.out.println(JadxDecompiler.getVersion());
		return false;
	}
	try {
		if (threadsCount <= 0) {
			throw new JadxException("Threads count must be positive, got: " + threadsCount);
		}
		LogHelper.setLogLevelFromArgs(this);
	} catch (JadxException e) {
		System.err.println("ERROR: " + e.getMessage());
		jcw.printUsage();
		return false;
	}
	return true;
}
 
Example #17
Source File: DeboxingVisitor.java    From Box with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode()) {
		return;
	}
	boolean replaced = false;
	for (BlockNode blockNode : mth.getBasicBlocks()) {
		List<InsnNode> insnList = blockNode.getInstructions();
		int count = insnList.size();
		for (int i = 0; i < count; i++) {
			InsnNode insnNode = insnList.get(i);
			if (insnNode.getType() == InsnType.INVOKE) {
				InsnNode replaceInsn = checkForReplace(((InvokeNode) insnNode));
				if (replaceInsn != null) {
					BlockUtils.replaceInsn(blockNode, i, replaceInsn);
					replaced = true;
				}
			}
		}
	}
	if (replaced) {
		ConstInlineVisitor.process(mth);
	}
}
 
Example #18
Source File: JavaToDex.java    From Box with Apache License 2.0 6 votes vote down vote up
public List<Path> convert(Path jar) throws JadxException {
	try (ByteArrayOutputStream out = new ByteArrayOutputStream();
			ByteArrayOutputStream errOut = new ByteArrayOutputStream()) {
		DxContext context = new DxContext(out, errOut);
		Path dir = FileUtils.createTempDir("jar-to-dex-");
		DxArgs args = new DxArgs(
				context,
				dir.toAbsolutePath().toString(),
				new String[] { jar.toAbsolutePath().toString() });
		int result = new Main(context).runDx(args);
		dxErrors = errOut.toString(CHARSET_NAME);
		if (result != 0) {
			throw new JadxException("Java to dex conversion error, code: " + result);
		}
		List<Path> list = new ArrayList<>();
		try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) {
			for (Path child : ds) {
				list.add(child);
				child.toFile().deleteOnExit();
			}
		}
		return list;
	} catch (Exception e) {
		throw new JadxException("dx exception: " + e.getMessage(), e);
	}
}
 
Example #19
Source File: PrepareForCodeGen.java    From Box with Apache License 2.0 6 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	List<BlockNode> blocks = mth.getBasicBlocks();
	if (blocks == null) {
		return;
	}
	for (BlockNode block : blocks) {
		if (block.contains(AFlag.DONT_GENERATE)) {
			continue;
		}
		removeInstructions(block);
		checkInline(block);
		removeParenthesis(block);
		modifyArith(block);
	}
	moveConstructorInConstructor(mth);
}
 
Example #20
Source File: MethodInlineVisitor.java    From jadx with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (canInline(mth) && mth.getBasicBlocks().size() == 2) {
		BlockNode returnBlock = mth.getBasicBlocks().get(1);
		if (returnBlock.contains(AFlag.RETURN) || returnBlock.getInstructions().isEmpty()) {
			BlockNode firstBlock = mth.getBasicBlocks().get(0);
			inlineMth(mth, firstBlock, returnBlock);
		}
	}
}
 
Example #21
Source File: ReturnVisitor.java    From jadx with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	// remove useless returns in void methods
	if (mth.isVoidReturn()) {
		DepthRegionTraversal.traverse(mth, new ReturnRemoverVisitor());
	}
}
 
Example #22
Source File: ReturnVisitor.java    From Box with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	// remove useless returns in void methods
	if (mth.getReturnType().equals(ArgType.VOID)) {
		DepthRegionTraversal.traverse(mth, new ReturnRemoverVisitor());
	}
}
 
Example #23
Source File: ClassModifier.java    From jadx with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(ClassNode cls) throws JadxException {
	for (ClassNode inner : cls.getInnerClasses()) {
		visit(inner);
	}
	if (isEmptySyntheticClass(cls)) {
		cls.add(AFlag.DONT_GENERATE);
		return false;
	}
	removeSyntheticFields(cls);
	cls.getMethods().forEach(ClassModifier::removeSyntheticMethods);
	cls.getMethods().forEach(ClassModifier::removeEmptyMethods);
	cls.getMethods().forEach(ClassModifier::cleanInsnsInAnonymousConstructor);
	return false;
}
 
Example #24
Source File: ProcessVariables.java    From Box with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode() || mth.getSVars().isEmpty()) {
		return;
	}

	List<CodeVar> codeVars = collectCodeVars(mth);
	if (codeVars.isEmpty()) {
		return;
	}
	checkCodeVars(mth, codeVars);
	// TODO: reduce code vars by name if debug info applied (need checks for variable scopes)

	// collect all variables usage
	CollectUsageRegionVisitor usageCollector = new CollectUsageRegionVisitor();
	DepthRegionTraversal.traverse(mth, usageCollector);
	Map<SSAVar, VarUsage> ssaUsageMap = usageCollector.getUsageMap();
	if (ssaUsageMap.isEmpty()) {
		return;
	}

	Map<CodeVar, List<VarUsage>> codeVarUsage = mergeUsageMaps(codeVars, ssaUsageMap);

	for (Entry<CodeVar, List<VarUsage>> entry : codeVarUsage.entrySet()) {
		declareVar(mth, entry.getKey(), entry.getValue());
	}
}
 
Example #25
Source File: ProcessVariables.java    From jadx with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode() || mth.getSVars().isEmpty()) {
		return;
	}
	removeUnusedResults(mth);

	List<CodeVar> codeVars = collectCodeVars(mth);
	if (codeVars.isEmpty()) {
		return;
	}
	checkCodeVars(mth, codeVars);
	// TODO: reduce code vars by name if debug info applied (need checks for variable scopes)

	// collect all variables usage
	CollectUsageRegionVisitor usageCollector = new CollectUsageRegionVisitor();
	DepthRegionTraversal.traverse(mth, usageCollector);
	Map<SSAVar, VarUsage> ssaUsageMap = usageCollector.getUsageMap();
	if (ssaUsageMap.isEmpty()) {
		return;
	}

	Map<CodeVar, List<VarUsage>> codeVarUsage = mergeUsageMaps(codeVars, ssaUsageMap);

	for (Entry<CodeVar, List<VarUsage>> entry : codeVarUsage.entrySet()) {
		declareVar(mth, entry.getKey(), entry.getValue());
	}
}
 
Example #26
Source File: FixAccessModifiers.java    From jadx with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(ClassNode cls) throws JadxException {
	if (respectAccessModifiers) {
		return true;
	}
	int newVisFlag = fixClassVisibility(cls);
	if (newVisFlag != -1) {
		changeVisibility(cls, newVisFlag);
	}
	return true;
}
 
Example #27
Source File: SSATransform.java    From Box with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode()) {
		return;
	}
	process(mth);
}
 
Example #28
Source File: FallbackModeVisitor.java    From Box with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode()) {
		return;
	}
	for (InsnNode insn : mth.getInstructions()) {
		if (insn == null) {
			continue;
		}
		// remove 'exception catch' for instruction which don't throw any exceptions
		CatchAttr catchAttr = insn.get(AType.CATCH_BLOCK);
		if (catchAttr != null) {
			switch (insn.getType()) {
				case RETURN:
				case IF:
				case GOTO:
				case MOVE:
				case MOVE_EXCEPTION:
				case ARITH: // ??
				case NEG:
				case CONST:
				case CONST_STR:
				case CONST_CLASS:
				case CMP_L:
				case CMP_G:
					catchAttr.getTryBlock().removeInsn(mth, insn);
					break;

				default:
					break;
			}
		}
	}
}
 
Example #29
Source File: EnumVisitor.java    From Box with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(ClassNode cls) throws JadxException {
	if (!convertToEnum(cls)) {
		AccessInfo accessFlags = cls.getAccessFlags();
		if (accessFlags.isEnum()) {
			cls.setAccessFlags(accessFlags.remove(AccessFlags.ACC_ENUM));
			cls.addAttr(AType.COMMENTS, "'enum' modifier removed");
		}
	}
	return true;
}
 
Example #30
Source File: InitCodeVariables.java    From Box with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(MethodNode mth) throws JadxException {
	if (mth.isNoCode()) {
		return;
	}
	initCodeVars(mth);
}