Commits

Artem Melentyev  committed b1fb98e

refactoring

  • Participants
  • Parent commits a4aac68

Comments (0)

Files changed (3)

File src/share/classes/com/sun/tools/javac/comp/Attr.java

      */
     String sourceName;
 
+    /** WeakHashMap to allow GC collect entries. Because we don't need them then they are gone */
+    public Map<JCTree, JCExpression> translateMap = new WeakHashMap<>();
+
     /** Check kind and type of given tree against protokind and prototype.
      *  If check succeeds, store type in tree and return it.
      *  If check fails, store errType in tree and return it.
             if ((ownkind & ~pkind) == 0) {
                 JCExpression t = tryImplicitConversion(tree, owntype, pt);
                 if (t != null) {
-                    putTranslate(tree, t);
+                    translateMap.put(tree, t);
                     return tree.type = owntype;
                 }
                 owntype = chk.checkType(tree.pos(), owntype, pt, errKey);
         tree.type = owntype;
         return owntype;
     }
-    /** WeakHashMap to allow GC collect entries. Because we don't need them then they are gone */
-    private Map<JCTree, JCExpression> translateMap = new WeakHashMap<>();
-    public void putTranslate(JCTree from, JCExpression to) {
-        translateMap.put(from, to);
-    }
-    public JCExpression removeTranslate(JCTree from) {
-        return translateMap.remove(from);
-    }
     /** try implicit conversion tree to pt type via #valueOf
      * @return static valueOf method call iff successful */
     JCMethodInvocation tryImplicitConversion(JCTree tree, Type owntype, Type req) {
                 if (m.kind == Kinds.MTH) {
                     JCMethodInvocation mi = make.Apply(null, make.Select(tree.indexed, m), List.of(tree.index, ass.rhs));
                     mi.type = attribExpr(mi, env);
-                    putTranslate(ass, mi);
+                    translateMap.put(ass, mi);
                     owntype = rhstype;
                     ok = true;
                 }
                     //owntype = rs.instantiate(env, atype, m, argtypes, null, true, false, noteWarner).getReturnType();
                     JCMethodInvocation mi = make.Apply(null, make.Select(tree.indexed, m), List.of(tree.index));
                     attribExpr(mi, env);
-                    putTranslate(tree, mi);
+                    translateMap.put(tree, mi);
                     owntype = mi.type;
                     ok = true;
                 }

File src/share/classes/com/sun/tools/javac/comp/Resolve.java

                     bestSoFar = concrete;
             }
         }
-        if (bestSoFar.kind >= ERR && operator) { // try operator overloading
-            String opname = null;
-            List<Type> args = List.nil();
-            if (argtypes.size() == 2) {
-                opname = binaryOperators.get(name.toString());
-                args = List.of(argtypes.get(1));
-            } else if (argtypes.size() == 1)
-                opname = unaryOperators.get(name.toString());
-            if (opname != null) {
-                Symbol method = findMethod(env, argtypes.get(0), names.fromString(opname), 
-                        args, null, false, false, false);
-                if (method.kind == Kinds.MTH) {
-                    bestSoFar = new OperatorSymbol(method.name, method.type, ByteCodes.error+1, method);
-                    if ("compareTo".equals(opname)) { // change result type to boolean if </>
-                        MethodType oldmt = (MethodType) method.type;
-                        bestSoFar.type = new Type.MethodType(oldmt.argtypes, syms.booleanType, oldmt.thrown, oldmt.tsym);
-                    }
-                }
-            }
-        }
+        bestSoFar = tryOperatorOverloading(env, name, argtypes, bestSoFar, operator);
         return bestSoFar;
     }
 
-    @SuppressWarnings("serial")
-    java.util.Map<String, String> binaryOperators = new java.util.HashMap<String, String>() {{
-        put("+", "add");
-        put("-", "subtract");
-        put("*", "multiply");
-        put("/", "divide");
-        put("%", "remainder");
-        put("&", "and");
-        put("|", "or");
-        put("^", "xor");
-        put("<<", "shiftLeft");
-        put(">>", "shiftRight");
-        put("<", "compareTo");
-        put(">", "compareTo");
-        put("<=", "compareTo");
-        put(">=", "compareTo");
-    }};
-    @SuppressWarnings("serial")
-    java.util.Map<String, String> unaryOperators = new java.util.HashMap<String, String>() {{
-        put("-", "negate");
-        put("~", "not");
-    }};
-
     /** Find unqualified method matching given name, type and value arguments.
      *  @param env       The current environment.
      *  @param name      The method's name.
         }
         return bestSoFar;
     }
+
+    // Operator Overloading stuff
+    @SuppressWarnings("serial")
+    java.util.Map<String, String> binaryOperators = new java.util.HashMap<String, String>() {{
+        put("+", "add");
+        put("-", "subtract");
+        put("*", "multiply");
+        put("/", "divide");
+        put("%", "remainder");
+        put("&", "and");
+        put("|", "or");
+        put("^", "xor");
+        put("<<", "shiftLeft");
+        put(">>", "shiftRight");
+        put("<", "compareTo");
+        put(">", "compareTo");
+        put("<=", "compareTo");
+        put(">=", "compareTo");
+    }};
+    @SuppressWarnings("serial")
+    java.util.Map<String, String> unaryOperators = new java.util.HashMap<String, String>() {{
+        put("-", "negate");
+        put("~", "not");
+    }};
+
+    Symbol tryOperatorOverloading(Env<AttrContext> env, Name name, List<Type> argtypes, Symbol bestSoFar, boolean operator) {
+        if (bestSoFar.kind >= ERR && operator) {
+            String opname = null;
+            List<Type> args = List.nil();
+            if (argtypes.size() == 2) {
+                opname = binaryOperators.get(name.toString());
+                args = List.of(argtypes.get(1));
+            } else if (argtypes.size() == 1)
+                opname = unaryOperators.get(name.toString());
+            if (opname != null) {
+                Symbol method = findMethod(env, argtypes.get(0), names.fromString(opname),
+                        args, null, false, false, false);
+                if (method.kind == Kinds.MTH) {
+                    bestSoFar = new OperatorSymbol(method.name, method.type, ByteCodes.error+1, method);
+                    if ("compareTo".equals(opname)) { // change result type to boolean if </>
+                        MethodType oldmt = (MethodType) method.type;
+                        bestSoFar.type = new Type.MethodType(oldmt.argtypes, syms.booleanType, oldmt.thrown, oldmt.tsym);
+                    }
+                }
+            }
+        }
+        return bestSoFar;
+    }
 }

File src/share/classes/com/sun/tools/javac/comp/TransTypes.java

 
     @Override
     public <T extends JCTree> T translate(T tree) {
-        JCExpression t = attr.removeTranslate(tree);
+        JCExpression t = attr.translateMap.remove(tree);
         if (t!=null)
             return (T) translate(t);
         return super.translate(tree);