private Detour callDetour(ClassNode cn, MethodNode mn, AnnotationNode a) { String owner = getFirstParameter(mn); if (owner == null) { _log.debug("Could not get parameter type for detour " + mn.name + mn.desc); return null; } MethodInsnNode mi = getMethodInstruction(mn, owner, mn.name); if (mi == null) { _log.debug("Could not get method instruction for detour " + mn.name + mn.desc); return null; } CallDetour detour = new CallDetour(_log); detour.matchMethod = mn.name; detour.matchDesc = mi.desc; detour.owner = owner.replace('/', '.'); detour.detourOwner = cn.name; detour.detourDesc = mn.desc; return detour; }
private boolean checkAnnotation(AnnotationNode a) { if (a.desc.equals("Lcom/rakuten/tech/mobile/perf/core/annotations/Exists;")) { if (!exists(((Type) a.values.get(1)).getClassName())) { return false; } } else if (a.desc .equals("Lcom/rakuten/tech/mobile/perf/core/annotations/MinCompileSdkVersion;")) { if (_compileSdkVersion < (int) a.values.get(1)) { return false; } } else if (a.desc .equals("Lcom/rakuten/tech/mobile/perf/core/annotations/MaxCompileSdkVersion;")) { if (_compileSdkVersion > (int) a.values.get(1)) { return false; } } return true; }
private boolean isParameterAnnotationFound(List<Annotation> annotationRules, MethodNode methodNode) { boolean annotationFound = true; List<AnnotationNode> allParameterAnnotations = new ArrayList<>(); List<AnnotationNode>[] visibleParameterAnnotations = methodNode.visibleParameterAnnotations; if (visibleParameterAnnotations != null && visibleParameterAnnotations.length != 0) { addIfNotNull(allParameterAnnotations, visibleParameterAnnotations); } List<AnnotationNode>[] inVisibleParameterAnnotations = methodNode.invisibleParameterAnnotations; if (inVisibleParameterAnnotations != null && inVisibleParameterAnnotations.length != 0) { addIfNotNull(allParameterAnnotations, inVisibleParameterAnnotations); } if (annotationRules != null && !annotationRules.isEmpty()) { for (Annotation annotationRule : annotationRules) { annotationFound &= RuleHelper.containsAnnotation(annotationRule, allParameterAnnotations); } } return annotationFound; }
private boolean isLocalVariableAnnotationFound(List<Annotation> annotationRules, MethodNode methodNode) { boolean annotationFound = true; List<AnnotationNode> allLocalVariableAnnotations = new ArrayList<>(); List<LocalVariableAnnotationNode> invisibleVariableAnnotations = methodNode.invisibleLocalVariableAnnotations; if (invisibleVariableAnnotations != null) { allLocalVariableAnnotations.addAll(invisibleVariableAnnotations); } List<LocalVariableAnnotationNode> visibleVariableAnnotations = methodNode.visibleLocalVariableAnnotations; if (visibleVariableAnnotations != null) { allLocalVariableAnnotations.addAll(visibleVariableAnnotations); } for (Annotation annotationRule : annotationRules) { annotationFound &= RuleHelper.containsAnnotation(annotationRule, allLocalVariableAnnotations); } return annotationFound; }
private boolean isMethodAnnotationFound(List<Annotation> annotationRules, MethodNode methodNode) { boolean annotationFound = true; List<AnnotationNode> allMethodAnnotations = new ArrayList<>(); List<AnnotationNode> invisibleAnnotations = methodNode.invisibleAnnotations; if (invisibleAnnotations != null) { allMethodAnnotations.addAll(invisibleAnnotations); } List<AnnotationNode> visibleAnnotations = methodNode.visibleAnnotations; if (visibleAnnotations != null) { allMethodAnnotations.addAll(visibleAnnotations); } for (Annotation annotationRule : annotationRules) { annotationFound &= RuleHelper.containsAnnotation(annotationRule, allMethodAnnotations); } return annotationFound; }
@Override public void process() { boolean issueFound; List<AnnotationNode> allAnnotations = new ArrayList<>(); List<AnnotationNode> visibleAnnotations = getClassNode().visibleAnnotations; if (visibleAnnotations != null) { allAnnotations.addAll(visibleAnnotations); } List<AnnotationNode> invisibleAnnotations = getClassNode().invisibleAnnotations; if (invisibleAnnotations != null) { allAnnotations.addAll(invisibleAnnotations); } for (AnnotationNode annotationNode : allAnnotations) { String desc = annotationNode.desc; boolean visible = visibleAnnotations == null ? false : visibleAnnotations.contains(annotationNode); logger.trace("visitAnnotation: desc={}, visible={}", desc, visible); issueFound = StringsHelper.simpleDescriptorToHuman(desc).equals(StringsHelper.dotsToSlashes(annotation.getType())); if (issueFound) { ReportItem reportItem = new ReportItem(getRuleName(), showInReport()); this.itemsFound().add(reportItem); this.setIssueFound(true); } } }
public Side getSide(final List<AnnotationNode> anns) { if (anns == null) { return null; } for (final AnnotationNode ann : anns) { if (ann.desc.equals(Type.getDescriptor((Class)SideOnly.class)) && ann.values != null) { for (int x = 0; x < ann.values.size() - 1; x += 2) { final Object key = ann.values.get(x); final Object value = ann.values.get(x + 1); if (key instanceof String && key.equals("value") && value instanceof String[]) { final String s = ((String[])value)[1]; if (s.equals(SafeCore.SIDE_SERVER)) { return Side.SERVER; } if (s.equals(SafeCore.SIDE_CLIENT)) { return Side.CLIENT; } } } } } return null; }
private static boolean isPackageInstantRunDisabled( File inputFile, ClassNode classNode) throws IOException { ClassNode packageInfoClass = parsePackageInfo(inputFile, classNode); if (packageInfoClass != null) { //noinspection unchecked List<AnnotationNode> annotations = packageInfoClass.invisibleAnnotations; if (annotations == null) { return false; } for (AnnotationNode annotation : annotations) { if (annotation.desc.equals(DISABLE_ANNOTATION_TYPE.getDescriptor())) { return true; } } } return false; }
private boolean removeInterfaces(ClassNode classNode) { boolean b = false; if (classNode.visibleAnnotations != null) { for (AnnotationNode an : classNode.visibleAnnotations) { Type anType = Type.getType(an.desc); if (anType.equals(Type.getType(Strippable.Interface.class))) { b = b | handleStrippableInterface(classNode, an); } else if (anType.equals(Type.getType(InterfaceContainer.class))) { for (Object o : (Iterable<?>) an.values.get(1)) { b = b | handleStrippableInterface(classNode, (AnnotationNode) o); } } } } return b; }
@SuppressWarnings("unchecked") private boolean isJUnit4TestSuite(ClassNode cn) { if (cn.visibleAnnotations == null) { return false; } boolean annotatedRunWith = false; boolean annotatedSuiteClasses = false; for (AnnotationNode annotation : (List<AnnotationNode>) cn.visibleAnnotations) { if (annotation.desc.equals(JUNIT_4_RUN_WITH_ANNOTATION)) { annotatedRunWith = true; } else if (annotation.desc.equals(JUNIT_4_SUITE_CLASSES_ANNOTATION)) { annotatedSuiteClasses = true; } } return annotatedRunWith && annotatedSuiteClasses; }
/** Check if the method is a JUnit 4 test case. */ @SuppressWarnings("unchecked") public boolean isJUnit4Testcase(MethodNode method) { if (method.visibleAnnotations == null) { return false; } boolean hasTestcaseAnnotation = false; boolean hasIgnoreAnnotation = false; for (AnnotationNode annotation : (List<AnnotationNode>) method.visibleAnnotations) { if (annotation.desc.equals(JUNIT_4_TEST_ANNOTATION)) { hasTestcaseAnnotation = true; } else if (annotation.desc.equals(JUNIT_4_IGNORE_ANNOTATION)) { hasIgnoreAnnotation = true; } } if (IGNORE_IGNORED_TEST_CASES && hasIgnoreAnnotation) { return false; } return hasTestcaseAnnotation; }
@Nullable private static IDLCInfo checkClassIsDLC(InputStream input) throws Exception { try { ClassReader reader = new ClassReader(input); ClassNode node = new ClassNode(Opcodes.ASM5); reader.accept(node, 0); if (node.visibleAnnotations != null) for (AnnotationNode annotation : node.visibleAnnotations) if (DESCRIPTOR.equals(annotation.desc)) return Tool.makeAnnotation(IDLCInfo.class, makeHandlerMapping(node.name.replace('/', '.')), annotation.values, "forgeVersion", "*", "description", ""); } finally { if (!(input instanceof ZipInputStream)) IOUtils.closeQuietly(input); } return null; }
@Unsafe(Unsafe.ASM_API) private static void loadHook(ClassNode node) throws Exception { if (node.visibleAnnotations != null) for (AnnotationNode nann : node.visibleAnnotations) if (nann.desc.equals(HOOK_PROVIDER_ANNOTATION_DESC)) { for (MethodNode methodNode : node.methods) if (checkSideOnly(methodNode) && methodNode.visibleAnnotations != null) for (AnnotationNode ann : methodNode.visibleAnnotations) if (ann.desc.equals(HOOK_ANNOTATION_DESC)) { Hook hook = Tool.makeAnnotation(Hook.class, ann.values, "isStatic", false, "type", Hook.Type.HEAD, "disable", ""); if (hook.disable().isEmpty() || !Boolean.getBoolean(hook.disable())) { String args[] = hook.value().split("#"); if (args.length == 2) transformers_mapping.get(args[0]).add(new TransformerHook(methodNode, node.name, args[0], args[1], hook.isStatic(), hook.type())); else AlchemyRuntimeException.onException(new RuntimeException("@Hook method -> split(\"#\") != 2")); } } break; } }
@Unsafe(Unsafe.ASM_API) private static void loadProxy(ClassNode node) throws Exception { if (node.visibleAnnotations != null) for (AnnotationNode nann : node.visibleAnnotations) if (nann.desc.equals(PROXY_PROVIDER_ANNOTATION_DESC)) { for (MethodNode methodNode : node.methods) if (checkSideOnly(methodNode) && methodNode.visibleAnnotations != null) for (AnnotationNode ann : methodNode.visibleAnnotations) if (ann.desc.equals(PROXY_ANNOTATION_DESC)) { Proxy proxy = Tool.makeAnnotation(Proxy.class, ann.values); String args[] = proxy.target().split("#"); if (args.length == 2) transformers_mapping.get(ASMHelper.getClassSrcName(node.name)).add(new TransformerProxy( methodNode, proxy.opcode(), args[0], args[1])); else AlchemyRuntimeException.onException(new RuntimeException("@Hook method -> split(\"#\") != 2")); } break; } }
private void copyClass(ZipFile inJar, ZipEntry entry, ZipOutputStream outJar, boolean isClientOnly) throws IOException { ClassReader reader = new ClassReader(readEntry(inJar, entry)); ClassNode classNode = new ClassNode(); reader.accept(classNode, 0); if (classNode.visibleAnnotations == null) { classNode.visibleAnnotations = new ArrayList<AnnotationNode>(); } classNode.visibleAnnotations.add(getSideAnn(isClientOnly)); ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(writer); byte[] data = writer.toByteArray(); ZipEntry newEntry = new ZipEntry(entry.getName()); if (outJar != null) { outJar.putNextEntry(newEntry); outJar.write(data); } }
@Test public void testApplicationPathAnnotation_None_And_ChangeThePath() throws Exception { String applicationPath = "/api"; JAXRSArchive archive = ShrinkWrap.create(JAXRSArchive.class); DefaultApplicationDeploymentProcessor processor = new DefaultApplicationDeploymentProcessor(archive); processor.applicationPath.set(applicationPath); processor.process(); try (InputStream in = archive.get(PATH).getAsset().openStream()) { ClassReader reader = new ClassReader(in); ClassNode node = new ClassNode(); reader.accept(node, 0); List<AnnotationNode> visibleAnnotations = node.visibleAnnotations; assertThat(visibleAnnotations.size()).isEqualTo(1); assertThat(visibleAnnotations.get(0).values).contains(applicationPath); } catch (IOException ignored) { } }
/** * Returns the minimum SDK to use according to the given annotation list, or * -1 if no annotation was found. * * @param annotations a list of annotation nodes from ASM * @return the API level to use for this node, or -1 */ @SuppressWarnings({"unchecked", "rawtypes"}) private static int getLocalMinSdk(List annotations) { if (annotations != null) { for (AnnotationNode annotation : (List<AnnotationNode>)annotations) { String desc = annotation.desc; if (desc.endsWith(TARGET_API_VMSIG)) { if (annotation.values != null) { for (int i = 0, n = annotation.values.size(); i < n; i += 2) { String key = (String) annotation.values.get(i); if (key.equals("value")) { //$NON-NLS-1$ Object value = annotation.values.get(i + 1); if (value instanceof Integer) { return (Integer) value; } } } } } } } return -1; }
private void addAnnotationValue(String name, Object value) { if (name != null) { buf.append(name).append("="); } if (value instanceof List<?>) { buf.append('{'); for (int i = 0; i < ((List) value).size(); i++) { addComma(i); addAnnotationValue(null, ((List) value).get(i)); } buf.append('}'); } else if (value instanceof String[]) { buf.append(Type.getType(((String[]) value)[0]).getClassName()).append(".").append(((String[]) value)[1]); } else if (value instanceof String) { buf.append("\"").append(value).append("\""); } else if (value instanceof Character) { buf.append("\'").append(value).append("\'"); } else if (value instanceof Type) { buf.append(((Type) value).getClassName()).append(".class"); } else if (value instanceof AnnotationNode) { addAnnotationNode(((AnnotationNode) value).desc, ((AnnotationNode) value).values); } else { buf.append(value); } }
protected int schedulerProviderParamIdx(MethodNode methodNode) { int result = -1; List<AnnotationNode>[] annotationBatches = visibleParameterAnnotationsOf(methodNode); if (null == annotationBatches) { return -1; } int idx = 0; for (List<AnnotationNode> annotations : annotationBatches) { if (null != annotations) { boolean found = annotations.stream().anyMatch(a -> SCHEDULER_PROVIDER_TYPE.getDescriptor().equals(a.desc)); if (found) { if (result < 0) { result = idx; } else { throw new IllegalStateException("More than one parameter of " + methodNode.desc + " is annotated as @SchedulerProvider"); } } } idx++; } return result; }
private static boolean isPartOfPU(ClassNode classNode, Class<? extends Annotation> qualifier, ClassLoader classLoader) { boolean anyQualifierFound = false; boolean requiredPUAnnotationFound = false; for (AnnotationNode annotation : classNode.visibleAnnotations) { Class<?> annotationClass = AsmUtil.loadClass(Type.getType(annotation.desc), classLoader); anyQualifierFound |= annotationClass.isAnnotationPresent(Qualifier.class); requiredPUAnnotationFound |= AnyUnit.class.equals(annotationClass); if (qualifier == null) { requiredPUAnnotationFound |= NullUnit.class.equals(annotationClass); } else { requiredPUAnnotationFound |= qualifier.equals(annotationClass); } } return requiredPUAnnotationFound || (qualifier == null && !anyQualifierFound); }
/** * Return the annotations present by type. Repeated annotations are taken * into account * * @param repeatedTypeInternalName * name of the repeat annoation. May be null */ public static List<AnnotationNode> getAnnotationsByType(List<AnnotationNode> annotations, String annotationTypeInternalName, String repeatedTypeInternalName) { if (annotations == null) return Collections.emptyList(); ArrayList<AnnotationNode> result = new ArrayList<>(); if (annotations != null) for (AnnotationNode node : annotations) { String nodeInternalName = Type.getType(node.desc).getInternalName(); if (Objects.equals(nodeInternalName, annotationTypeInternalName)) { result.add(node); } if (Objects.equals(nodeInternalName, repeatedTypeInternalName)) { if (node.values != null) for (int i = 0; i < node.values.size() - 1; i += 2) { if (Objects.equals("value", node.values.get(i))) result.add((AnnotationNode) node.values.get(i + 1)); } } } return result; }
public final static void readAnnotationValues(ScannedItem scanItem, AnnotationNode ann) { if(ann.values != null) { for (int i = 0; i < ann.values.size(); i += 2) { Object k = ann.values.get(i); Object v = ann.values.get(i + 1); if (v instanceof Object[]) { Object[] v0 = (Object[]) v; v = v0[1]; } scanItem.addAnnValue(k, v); } } }
private ClassNode findKeyClass(ClassNode domain) throws IOException { ClassNode key = null; if (DomainMojoHelper.log().isDebugEnabled()) DomainMojoHelper.log().debug( "Traverse domain annotations to find if there exists key class, domain:" + domain.name); for (Object o : domain.visibleAnnotations) { AnnotationNode an = (AnnotationNode) o; if (DomainMojoHelper.log().isDebugEnabled()) DomainMojoHelper.log().debug("Annotation:" + an.desc + " value:" + an.values); if (AN_Key.equals(an.desc)) { Type t = (Type) an.values.get(1); InputStream keyClassIn = DomainMojoHelper.getLoader().getResourceAsStream(t.getInternalName() + ".class"); byte[] keyClassBytes = new byte[keyClassIn.available()]; keyClassIn.read(keyClassBytes); keyClassIn.close(); ClassReader cr = new ClassReader(keyClassBytes); key = new ClassNode(); cr.accept(key, 0); } } return key; }
private boolean isKeyField(FieldNode fn) { if (fn.visibleAnnotations != null) { for (Object o : fn.visibleAnnotations) { AnnotationNode an = (AnnotationNode) o; if (AN_Key.equals(an.desc)) { if (DomainMojoHelper.log().isDebugEnabled()) DomainMojoHelper.log().debug("Found key field:" + fn.name + "," + fn.desc); return true; } } } return false; }
private String[] findTableNameAndRegionName(ClassNode domain) { String[] names= new String[2]; for (Object o : domain.visibleAnnotations) { AnnotationNode an = (AnnotationNode) o; if (DomainMojoHelper.log().isDebugEnabled()) DomainMojoHelper.log().debug("Annotation:" + an.desc + " value:" + an.values); if (AN_Region.equals(an.desc)) { names[0] = (String)an.values.get(1); } else if (AN_Table.equals(an.desc)) { names[1] = (String)an.values.get(1); } } return names; }
private static void acceptAnnotation(AnnotationVisitor av, String name, Object value) { if (value instanceof String[]) { String[] args = (String[]) value; av.visitEnum(name, args[0], args[1]); } else if (value instanceof AnnotationNode) { AnnotationNode node = (AnnotationNode) value; node.accept(av.visitAnnotation(name, node.desc)); } else if (value instanceof List) { AnnotationVisitor av1 = av.visitArray(name); if (av1 != null) { for (Object value1 : (Iterable<?>) value) { acceptAnnotation(av1, null, value1); } av1.visitEnd(); } } else { av.visit(name, value); } }
/** * Gets the annotation for a list of nodes * * @param nodes List of annotation nodes to search * @param name Name of the annotation to find * @return Map of name to value annotations */ public static Map<String, Object> getAnnotation(List<AnnotationNode> nodes, String name) { if (nodes == null) return null; for (AnnotationNode node : nodes) { if (node.desc.equals(name)) { Map<String, Object> result = new HashMap<String, Object>(); if (node.values != null && node.values.size() > 0) { for (int i = 0; i < node.values.size(); i += 2) { result.put((String) node.values.get(i), node.values.get(i + 1)); } } return result; } } return null; }
private void extractDefineTypeFinalityAnnotationValues(ClassNode classNode, AnnotationNode annotation) { int phaseValue = 1; // default to 1 String typeValue = null; Boolean finalityValue = null; if (annotation.values != null) { for (int i = 0; i < annotation.values.size(); i += 2) { String name = (String) annotation.values.get(i); Object value = annotation.values.get(i + 1); if(name.equals(PHASE)){ phaseValue = (int) value; } else if(name.equals(TYPE)){ typeValue = ((String)value).replaceAll("\\.", "/"); } else if(name.equals(FINALITY)){ finalityValue = (boolean) value; } } if(typeValue != null && finalityValue != null){ String className = typeValue; if(className.equals("")){ className = classNode.superName; } targetTypes.add(new DefineTypeFinalityAnnotation(phaseValue, className, finalityValue)); } } }
private void extractMergeTypeAnnotationValues(ClassNode classNode, AnnotationNode annotation){ if(classNode != null){ int phaseValue = 1; // default to 1 String superTypeValue = null; if(annotation.values != null){ for (int i = 0; i < annotation.values.size(); i += 2) { String name = (String) annotation.values.get(i); Object value = annotation.values.get(i + 1); if(name.equals(PHASE)){ phaseValue = (int) value; } else if(name.equals(SUPERTYPE)){ superTypeValue = ((String)value).replaceAll("\\.", "/"); } } } if(superTypeValue == null || superTypeValue.equals("")){ superTypeValue = classNode.superName; } mergeTypeAnnotation = new MergeTypeAnnotation(phaseValue, superTypeValue); } }
private void extractDefineTypeVisibilityAnnotationValues(ClassNode classNode, AnnotationNode annotation) { int phaseValue = 1; // default to 1 String typeValue = null; Visibility visibilityValue = null; if (annotation.values != null) { for (int i = 0; i < annotation.values.size(); i += 2) { String name = (String) annotation.values.get(i); Object value = annotation.values.get(i + 1); if(name.equals(PHASE)){ phaseValue = (int) value; } else if(name.equals(TYPE)){ typeValue = ((String)value).replaceAll("\\.", "/"); } else if(name.equals(VISIBILITY)){ String valueString = (String) value; visibilityValue = Visibility.getVisibilityFromString(valueString); } } if(typeValue != null && visibilityValue != null){ String className = typeValue; if(className.equals("")){ className = classNode.superName; } targetTypes.add(new DefineTypeVisibilityAnnotation(phaseValue, className, visibilityValue)); } } }
private void extractPurgeFieldValues(ClassNode classNode, AnnotationNode annotation) { int phaseValue = 1; // default to 1 String typeValue = null; String fieldValue = null; if (annotation.values != null) { for (int i = 0; i < annotation.values.size(); i += 2) { String name = (String) annotation.values.get(i); Object value = annotation.values.get(i + 1); if(name.equals(PHASE)){ phaseValue = (int) value; } else if(name.equals(TYPE)){ typeValue = ((String)value).replaceAll("\\.", "/"); } else if(name.equals(FIELD)){ fieldValue = (String) value; } } if(typeValue != null && fieldValue != null){ String className = typeValue; if(className.equals("")){ className = classNode.superName; } purgeFieldAnnotations.add(new PurgeFieldAnnotation(phaseValue, className, fieldValue)); } } }
private void extractPurgeMethodValues(ClassNode classNode, AnnotationNode annotation) { int phaseValue = 1; // default to 1 String typeValue = null; String methodValue = null; if (annotation.values != null) { for (int i = 0; i < annotation.values.size(); i += 2) { String name = (String) annotation.values.get(i); Object value = annotation.values.get(i + 1); if(name.equals(PHASE)){ phaseValue = (int) value; } else if(name.equals(TYPE)){ typeValue = ((String)value).replaceAll("\\.", "/"); } else if(name.equals(METHOD)){ methodValue = (String) value; } } if(typeValue != null && methodValue != null){ String className = typeValue; if(className.equals("")){ className = classNode.superName; } purgeMethodAnnotations.add(new PurgeMethodAnnotation(phaseValue, className, methodValue)); } } }
private void extractPurgeTypeAnnotationValues(ClassNode classNode, AnnotationNode annotation) { int phaseValue = 1; // default to 1 String typeValue = null; if (annotation.values != null) { for (int i = 0; i < annotation.values.size(); i += 2) { String name = (String) annotation.values.get(i); Object value = annotation.values.get(i + 1); if(name.equals(PHASE)){ phaseValue = (int) value; } else if(name.equals(TYPE)){ typeValue = ((String)value).replaceAll("\\.", "/"); } } if(typeValue != null){ String className = typeValue; if(className.equals("")){ className = classNode.superName; } purgeTypeAnnotations.add(new PurgeTypeAnnotation(phaseValue, className)); } } }
private void transferTraitFields(ClassWriter writer) { for (FieldNode f : traitNode.fields) { if (f.name.equals("_super")) continue; FieldVisitor v = writer.visitField(ACC_PUBLIC, f.name, f.desc, null, f.value); if (f.visibleAnnotations != null) { for (AnnotationNode a : f.visibleAnnotations) { AnnotationVisitor av = v.visitAnnotation(a.desc, true); Iterator<Object> it = a.values.iterator(); while (it.hasNext()) av.visit((String) it.next(), it.next()); } } v.visitEnd(); } }