From 47aa73bd13eedf5afc3e800574f1a7283e55955a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Wed, 29 Jul 2020 11:57:50 +0800 Subject: [PATCH] make testable inject method public --- .../core/translator/EnableTestableTranslator.java | 8 +++++--- .../com/alibaba/testable/core/util/TypeUtil.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java b/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java index a07eba4..77377ff 100644 --- a/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java +++ b/core/src/main/java/com/alibaba/testable/core/translator/EnableTestableTranslator.java @@ -5,6 +5,7 @@ import com.alibaba.testable.core.generator.TestSetupMethodGenerator; import com.alibaba.testable.core.generator.TestableRefFieldGenerator; import com.alibaba.testable.core.model.TestableContext; import com.alibaba.testable.core.constant.ConstPool; +import com.alibaba.testable.core.util.TypeUtil; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.*; import com.sun.tools.javac.util.ListBuffer; @@ -24,8 +25,8 @@ import java.util.Arrays; public class EnableTestableTranslator extends BaseTranslator { private final TestableContext cx; - private String testClassName; - private String sourceClassName; + private final String testClassName; + private final String sourceClassName; private final ListBuffer sourceClassIns = new ListBuffer<>(); private final ListBuffer privateOrFinalFields = new ListBuffer<>(); private final ListBuffer privateMethods = new ListBuffer<>(); @@ -85,12 +86,13 @@ public class EnableTestableTranslator extends BaseTranslator { } /** - * Search for TestableInject and TestSetup annotations + * Search for TestableInject annotations */ @Override public void visitMethodDef(JCMethodDecl jcMethodDecl) { for (JCAnnotation a : jcMethodDecl.mods.annotations) { if (a.type != null && ConstPool.ANNOTATION_TESTABLE_INJECT.equals(a.type.tsym.toString())) { + TypeUtil.toPublicFlags(jcMethodDecl.getModifiers()); ListBuffer args = new ListBuffer<>(); for (JCVariableDecl p : jcMethodDecl.params) { args.add(cx.treeMaker.Select(p.vartype, cx.names.fromString(ConstPool.CLASS_OF_TYPE))); diff --git a/core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java b/core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java index dc00692..5988366 100644 --- a/core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java +++ b/core/src/main/java/com/alibaba/testable/core/util/TypeUtil.java @@ -1,7 +1,10 @@ package com.alibaba.testable.core.util; +import com.sun.tools.javac.tree.JCTree; + import java.lang.reflect.Constructor; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; @@ -10,6 +13,15 @@ import java.util.List; */ public class TypeUtil { + /** + * Convert modifier to public + */ + public static void toPublicFlags(JCTree.JCModifiers modifiers) { + modifiers.flags &= ~Modifier.PRIVATE; + modifiers.flags &= ~Modifier.PROTECTED; + modifiers.flags |= Modifier.PUBLIC; + } + /** * Information of substitution method */