Search in sources :

Example 1 with Filter

use of com.shulie.instrument.simulator.api.filter.Filter in project LinkAgent by shulieTech.

the class ClassMatchBuilder method toEventWatchCondition.

private EventWatchCondition toEventWatchCondition() {
    final List<Filter> filters = new ArrayList<Filter>();
    final Filter filter = new ExtFilter() {

        @Override
        public boolean isIncludeSubClasses() {
            return isIncludeSubClasses;
        }

        @Override
        public boolean isIncludeBootstrap() {
            return isIncludeBootstrap;
        }

        @Override
        public boolean doClassNameFilter(String javaClassName) {
            return patternMatching(javaClassName, pattern, patternType);
        }

        @Override
        public boolean doClassFilter(ClassDescriptor classDescriptor) {
            if ((classDescriptor.getAccess() & getWithAccess()) != getWithAccess()) {
                return false;
            }
            if (!patternMatching(classDescriptor.getClassName(), pattern, patternType)) {
                return false;
            }
            if (!patternMatching(classDescriptor.getSuperClassTypeJavaClassName(), superPatterns, patternType)) {
                return false;
            }
            if (getHasInterfaceTypes().isNotEmpty()) {
                if (!getHasInterfaceTypes().patternWith(classDescriptor.getInterfaceTypeJavaClassNameArray())) {
                    return false;
                }
            }
            if (getHasAnnotationTypes().isNotEmpty()) {
                if (!getHasAnnotationTypes().patternWith(classDescriptor.getAnnotationTypeJavaClassNameArray())) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public List<BuildingForListeners> doMethodFilter(MethodDescriptor methodDescriptor) {
            // nothing to matching
            if (bfBehaviors.isEmpty()) {
                return Collections.EMPTY_LIST;
            }
            List<BuildingForListeners> list = new ArrayList<BuildingForListeners>();
            // matching any behavior
            for (final BehaviorMatchBuilder bfBehavior : bfBehaviors) {
                final boolean matches = matches(methodDescriptor, bfBehavior);
                if ((bfBehavior.isWithNot() && matches) || (!bfBehavior.isWithNot() && !matches)) {
                    continue;
                }
                list.addAll(bfBehavior.getListeners());
            }
            // non matched
            return list;
        }

        private boolean matches(MethodDescriptor methodDescriptor, BehaviorMatchBuilder bfBehavior) {
            if ((methodDescriptor.getAccess() & bfBehavior.getWithAccess()) != bfBehavior.getWithAccess()) {
                return false;
            }
            if (!patternMatching(methodDescriptor.getMethodName(), bfBehavior.getPattern(), patternType)) {
                return false;
            }
            if (!bfBehavior.getWithParameterTypes().matching(methodDescriptor.getParameterTypeJavaClassNameArray())) {
                return false;
            }
            if (bfBehavior.getHasExceptionTypes().isNotEmpty()) {
                if (!bfBehavior.getHasExceptionTypes().patternWith(methodDescriptor.getThrowsTypeJavaClassNameArray())) {
                    return false;
                }
            }
            if (bfBehavior.getHasAnnotationTypes().isNotEmpty()) {
                if (!bfBehavior.getHasAnnotationTypes().patternWith(methodDescriptor.getAnnotationTypeJavaClassNameArray())) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public List<BuildingForListeners> getAllListeners() {
            List<BuildingForListeners> listeners = new ArrayList<BuildingForListeners>();
            for (BehaviorMatchBuilder buildingForBehavior : bfBehaviors) {
                listeners.addAll(buildingForBehavior.getListeners());
            }
            return listeners;
        }
    };
    // filter
    filters.add(filter);
    return new EventWatchCondition() {

        @Override
        public Filter[] getOrFilterArray() {
            return filters.toArray(new Filter[0]);
        }
    };
}
Also used : ClassDescriptor(com.shulie.instrument.simulator.api.filter.ClassDescriptor) ExtFilter(com.shulie.instrument.simulator.api.filter.ExtFilter) Filter(com.shulie.instrument.simulator.api.filter.Filter) ExtFilter(com.shulie.instrument.simulator.api.filter.ExtFilter) ArrayList(java.util.ArrayList) MethodDescriptor(com.shulie.instrument.simulator.api.filter.MethodDescriptor)

Example 2 with Filter

use of com.shulie.instrument.simulator.api.filter.Filter in project LinkAgent by shulieTech.

the class JadModule method processExactMatch.

private CommandResponse processExactMatch(Set<Class<?>> matchedClasses, Set<Class<?>> withInnerClasses, boolean sourceOnly) {
    Class<?> c = matchedClasses.iterator().next();
    Set<Class<?>> allClasses = new HashSet<Class<?>>(withInnerClasses);
    allClasses.add(c);
    final Set<String> classNames = toClassName(allClasses);
    Filter filter = new MultiClassNameFilter(classNames, true);
    DumpResult dumpResult = null;
    try {
        dumpResult = moduleEventWatcher.dump(filter, new NoActionProgress());
        File classFile = dumpResult.getDumpResult().get(c.getName());
        String source = Decompiler.decompile(classFile.getAbsolutePath(), simulatorConfig.getSimulatorVersion());
        if (source != null) {
            source = pattern.matcher(source).replaceAll("");
        } else {
            return CommandResponse.failure("can't decompile class file.");
        }
        if (sourceOnly) {
            JadInfo jadInfo = new JadInfo();
            jadInfo.setSource(source);
            return CommandResponse.success(jadInfo);
        }
        JadInfo jadInfo = new JadInfo();
        jadInfo.setClassloader(drawClassLoader(c));
        jadInfo.setLocation(ClassUtils.getCodeSource(c.getProtectionDomain().getCodeSource()));
        jadInfo.setSource(source);
        return CommandResponse.success(jadInfo);
    } catch (Throwable t) {
        logger.error("SIMULATOR: jad: fail to decompile class: " + c.getName(), t);
        return CommandResponse.failure(t);
    } finally {
        if (dumpResult != null) {
            // 注销watchId
            moduleEventWatcher.delete(dumpResult.getWatchId());
        }
    }
}
Also used : MultiClassNameFilter(com.shulie.instrument.simulator.api.filter.MultiClassNameFilter) JadInfo(com.shulie.instrument.simulator.module.model.jad.JadInfo) Filter(com.shulie.instrument.simulator.api.filter.Filter) MultiClassNameFilter(com.shulie.instrument.simulator.api.filter.MultiClassNameFilter) NameRegexFilter(com.shulie.instrument.simulator.api.filter.NameRegexFilter) File(java.io.File) HashSet(java.util.HashSet)

Aggregations

Filter (com.shulie.instrument.simulator.api.filter.Filter)2 ClassDescriptor (com.shulie.instrument.simulator.api.filter.ClassDescriptor)1 ExtFilter (com.shulie.instrument.simulator.api.filter.ExtFilter)1 MethodDescriptor (com.shulie.instrument.simulator.api.filter.MethodDescriptor)1 MultiClassNameFilter (com.shulie.instrument.simulator.api.filter.MultiClassNameFilter)1 NameRegexFilter (com.shulie.instrument.simulator.api.filter.NameRegexFilter)1 JadInfo (com.shulie.instrument.simulator.module.model.jad.JadInfo)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1