001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang.implementation.java; 020 021import org.apache.reef.tang.ClassHierarchy; 022import org.apache.reef.tang.ExternalConstructor; 023import org.apache.reef.tang.JavaClassHierarchy; 024import org.apache.reef.tang.annotations.Name; 025import org.apache.reef.tang.annotations.NamedParameter; 026import org.apache.reef.tang.exceptions.BindException; 027import org.apache.reef.tang.exceptions.ClassHierarchyException; 028import org.apache.reef.tang.exceptions.NameResolutionException; 029import org.apache.reef.tang.exceptions.ParseException; 030import org.apache.reef.tang.formats.ParameterParser; 031import org.apache.reef.tang.types.*; 032import org.apache.reef.tang.util.MonotonicTreeMap; 033import org.apache.reef.tang.util.ReflectionUtilities; 034 035import java.lang.reflect.Type; 036import java.net.URL; 037import java.net.URLClassLoader; 038import java.util.*; 039 040public class ClassHierarchyImpl implements JavaClassHierarchy { 041 // TODO Want to add a "register namespace" method, but Java is not designed 042 // to support such things. 043 // There are third party libraries that would help, but they can fail if the 044 // relevant jar has not yet been loaded. Tint works around this using such 045 // a library. 046 047 /** 048 * The ParameterParser that this ClassHierarchy uses to parse default values. 049 * Custom parameter parsers allow applications to extend the set of classes 050 * that Tang can parse. 051 */ 052 private final ParameterParser parameterParser = new ParameterParser(); 053 /** 054 * The classloader that was used to populate this class hierarchy. 055 */ 056 private final URLClassLoader loader; 057 /** 058 * The jars that are reflected by that loader. These are in addition to 059 * whatever jars are available by the default classloader (i.e., the one 060 * that loaded Tang. We need this list so that we can merge class hierarchies 061 * that are backed by different classpaths. 062 */ 063 private final List<URL> jars; 064 /** 065 * A reference to the root package which is a root of a tree of nodes. 066 * The children of each node in the tree are accessible by name, and the 067 * structure of the tree mirrors Java's package namespace. 068 */ 069 private final PackageNode namespace; 070 /** 071 * A map from short name to named parameter node. This is only used to 072 * sanity check short names so that name clashes get resolved. 073 */ 074 private final Map<String, NamedParameterNode<?>> shortNames = new MonotonicTreeMap<>(); 075 076 @SuppressWarnings("unchecked") 077 public ClassHierarchyImpl() { 078 this(new URL[0], new Class[0]); 079 } 080 081 @SuppressWarnings("unchecked") 082 public ClassHierarchyImpl(final URL... jars) { 083 this(jars, new Class[0]); 084 } 085 086 public ClassHierarchyImpl(final URL[] jars, final Class<? extends ExternalConstructor<?>>[] parameterParsers) { 087 this.namespace = JavaNodeFactory.createRootPackageNode(); 088 this.jars = new ArrayList<>(Arrays.asList(jars)); 089 this.loader = new URLClassLoader(jars, this.getClass().getClassLoader()); 090 for (final Class<? extends ExternalConstructor<?>> p : parameterParsers) { 091 try { 092 parameterParser.addParser(p); 093 } catch (final BindException e) { 094 throw new IllegalArgumentException("Could not register parameter parsers", e); 095 } 096 } 097 } 098 099 /** 100 * A helper method that returns the parsed default value of a given 101 * NamedParameter. 102 * 103 * @return null or an empty set if there is no default value, the default value (or set of values) otherwise. 104 * @throws ClassHierarchyException if a default value was specified, but could not be parsed, or if a set of 105 * values were specified for a non-set parameter. 106 */ 107 @SuppressWarnings("unchecked") 108 @Override 109 public <T> T parseDefaultValue(final NamedParameterNode<T> name) { 110 final String[] vals = name.getDefaultInstanceAsStrings(); 111 final T[] ret = (T[]) new Object[vals.length]; 112 for (int i = 0; i < vals.length; i++) { 113 final String val = vals[i]; 114 try { 115 ret[i] = parse(name, val); 116 } catch (final ParseException e) { 117 throw new ClassHierarchyException("Could not parse default value", e); 118 } 119 } 120 if (name.isSet()) { 121 return (T) new HashSet<T>(Arrays.asList(ret)); 122 } else if (name.isList()) { 123 return (T) new ArrayList<T>(Arrays.asList(ret)); 124 } else { 125 if (ret.length == 0) { 126 return null; 127 } else if (ret.length == 1) { 128 return ret[0]; 129 } else { 130 throw new IllegalStateException("Multiple defaults for non-set named parameter! " + name.getFullName()); 131 } 132 } 133 } 134 135 /** 136 * Parse a string, assuming that it is of the type expected by a given NamedParameter. 137 * <p> 138 * This method does not deal with sets; if the NamedParameter is set valued, then the provided 139 * string should correspond to a single member of the set. It is up to the caller to call parse 140 * once for each value that should be parsed as a member of the set. 141 * 142 * @return a non-null reference to the parsed value. 143 */ 144 @Override 145 @SuppressWarnings("unchecked") 146 public <T> T parse(final NamedParameterNode<T> np, final String value) throws ParseException { 147 final ClassNode<T> iface; 148 try { 149 iface = (ClassNode<T>) getNode(np.getFullArgName()); 150 } catch (final NameResolutionException e) { 151 throw new IllegalStateException("Could not parse validated named parameter argument type. NamedParameter is " + 152 np.getFullName() + " argument type is " + np.getFullArgName(), e); 153 } 154 Class<?> clazz; 155 String fullName; 156 try { 157 clazz = classForName(iface.getFullName()); 158 fullName = null; 159 } catch (final ClassNotFoundException e) { 160 clazz = null; 161 fullName = iface.getFullName(); 162 } 163 try { 164 if (clazz != null) { 165 return (T) parameterParser.parse(clazz, value); 166 } else { 167 return parameterParser.parse(fullName, value); 168 } 169 } catch (final UnsupportedOperationException e) { 170 try { 171 final Node impl = getNode(value); 172 if (impl instanceof ClassNode && isImplementation(iface, (ClassNode<?>) impl)) { 173 return (T) impl; 174 } 175 throw new ParseException("Name<" + iface.getFullName() + "> " + np.getFullName() + 176 " cannot take non-subclass " + impl.getFullName(), e); 177 } catch (final NameResolutionException e2) { 178 throw new ParseException("Name<" + iface.getFullName() + "> " + np.getFullName() + 179 " cannot take non-class " + value, e2); 180 } 181 } 182 } 183 184 /** 185 * Helper method that converts a String to a Class using this 186 * ClassHierarchy's classloader. 187 */ 188 @Override 189 public Class<?> classForName(final String name) throws ClassNotFoundException { 190 return ReflectionUtilities.classForName(name, loader); 191 } 192 193 private <T, U> Node buildPathToNode(final Class<U> clazz) 194 throws ClassHierarchyException { 195 final String[] path = clazz.getName().split("\\$"); 196 197 Node root = namespace; 198 for (int i = 0; i < path.length - 1; i++) { 199 root = root.get(path[i]); 200 } 201 202 if (root == null) { 203 throw new NullPointerException(); 204 } 205 final Node parent = root; 206 207 final Type argType = ReflectionUtilities.getNamedParameterTargetOrNull(clazz); 208 209 if (argType == null) { 210 return JavaNodeFactory.createClassNode(parent, clazz); 211 } else { 212 213 // checked inside of NamedParameterNode, using reflection. 214 @SuppressWarnings("unchecked") final NamedParameterNode<T> np = JavaNodeFactory.createNamedParameterNode( 215 parent, (Class<? extends Name<T>>) clazz, argType); 216 217 if (parameterParser.canParse(ReflectionUtilities.getFullName(argType)) && 218 clazz.getAnnotation(NamedParameter.class).default_class() != Void.class) { 219 throw new ClassHierarchyException("Named parameter " + ReflectionUtilities.getFullName(clazz) + 220 " defines default implementation for parsable type " + ReflectionUtilities.getFullName(argType)); 221 } 222 223 final String shortName = np.getShortName(); 224 if (shortName != null) { 225 final NamedParameterNode<?> oldNode = shortNames.get(shortName); 226 if (oldNode != null) { 227 if (oldNode.getFullName().equals(np.getFullName())) { 228 throw new IllegalStateException("Tried to double bind " 229 + oldNode.getFullName() + " to short name " + shortName); 230 } 231 throw new ClassHierarchyException("Named parameters " + oldNode.getFullName() 232 + " and " + np.getFullName() + " have the same short name: " 233 + shortName); 234 } 235 shortNames.put(shortName, np); 236 } 237 return np; 238 } 239 } 240 241 private Node getAlreadyBoundNode(final Class<?> clazz) throws NameResolutionException { 242 return getAlreadyBoundNode(ReflectionUtilities.getFullName(clazz)); 243 } 244 245 @Override 246 public Node getNode(final Class<?> clazz) { 247 try { 248 return getNode(ReflectionUtilities.getFullName(clazz)); 249 } catch (final NameResolutionException e) { 250 throw new ClassHierarchyException("JavaClassHierarchy could not resolve " + clazz 251 + " which is definitely avalable at runtime", e); 252 } 253 } 254 255 @Override 256 public synchronized Node getNode(final String name) throws NameResolutionException { 257 final Node n = register(name); 258 if (n == null) { 259 // This will never succeed; it just generates a nice exception. 260 getAlreadyBoundNode(name); 261 throw new IllegalStateException("IMPLEMENTATION BUG: Register failed, " 262 + "but getAlreadyBoundNode succeeded!"); 263 } 264 return n; 265 } 266 267 private Node getAlreadyBoundNode(final String name) throws NameResolutionException { 268 Node root = namespace; 269 final String[] toks = name.split("\\$"); 270 final String outerClassName = toks[0]; 271 root = root.get(outerClassName); 272 if (root == null) { 273 throw new NameResolutionException(name, outerClassName); 274 } 275 for (int i = 1; i < toks.length; i++) { 276 root = root.get(toks[i]); 277 if (root == null) { 278 final StringBuilder sb = new StringBuilder(outerClassName); 279 for (int j = 0; j < i; j++) { 280 sb.append(toks[j]); 281 if (j != i - 1) { 282 sb.append("."); 283 } 284 } 285 throw new NameResolutionException(name, sb.toString()); 286 } 287 } 288 return root; 289 } 290 291 private Node register(final String s) { 292 final Class<?> c; 293 try { 294 c = classForName(s); 295 } catch (final ClassNotFoundException e1) { 296 return null; 297 } 298 try { 299 final Node n = getAlreadyBoundNode(c); 300 return n; 301 } catch (final NameResolutionException ignored) { 302 // node not bound yet 303 } 304 // First, walk up the class hierarchy, registering all out parents. This 305 // can't be loopy. 306 if (c.getSuperclass() != null) { 307 register(ReflectionUtilities.getFullName(c.getSuperclass())); 308 } 309 for (final Class<?> i : c.getInterfaces()) { 310 register(ReflectionUtilities.getFullName(i)); 311 } 312 // Now, we'd like to register our enclosing classes. This turns out to be 313 // safe. 314 // Thankfully, Java doesn't allow: 315 // class A implements A.B { class B { } } 316 317 // It also doesn't allow cycles such as: 318 // class A implements B.BB { interface AA { } } 319 // class B implements A.AA { interface BB { } } 320 321 // So, even though grafting arbitrary DAGs together can give us cycles, Java 322 // seems 323 // to have our back on this one. 324 final Class<?> enclosing = c.getEnclosingClass(); 325 if (enclosing != null) { 326 register(ReflectionUtilities.getFullName(enclosing)); 327 } 328 329 // Now register the class. This has to be after the above so we know our 330 // parents (superclasses and enclosing packages) are already registered. 331 final Node n = registerClass(c); 332 333 // Finally, do things that might introduce cycles that invlove c. 334 // This has to be below registerClass, which ensures that any cycles 335 // this stuff introduces are broken. 336 for (final Class<?> innerClass : c.getDeclaredClasses()) { 337 register(ReflectionUtilities.getFullName(innerClass)); 338 } 339 if (n instanceof ClassNode) { 340 final ClassNode<?> cls = (ClassNode<?>) n; 341 for (final ConstructorDef<?> def : cls.getInjectableConstructors()) { 342 for (final ConstructorArg arg : def.getArgs()) { 343 register(arg.getType()); 344 if (arg.getNamedParameterName() != null) { 345 final NamedParameterNode<?> np = (NamedParameterNode<?>) register(arg 346 .getNamedParameterName()); 347 try { 348 // TODO: When handling sets, need to track target of generic parameter, and check the type here! 349 if (!np.isSet() && !np.isList() && 350 !ReflectionUtilities.isCoercable(classForName(arg.getType()), classForName(np.getFullArgName()))) { 351 throw new ClassHierarchyException( 352 "Named parameter type mismatch in " + cls.getFullName() + ". Constructor expects a " 353 + arg.getType() + " but " + np.getName() + " is a " 354 + np.getFullArgName()); 355 } 356 } catch (final ClassNotFoundException e) { 357 throw new ClassHierarchyException("Constructor refers to unknown class " 358 + arg.getType(), e); 359 } 360 } 361 } 362 } 363 } else if (n instanceof NamedParameterNode) { 364 final NamedParameterNode<?> np = (NamedParameterNode<?>) n; 365 register(np.getFullArgName()); 366 } 367 return n; 368 } 369 370 /** 371 * Assumes that all of the parents of c have been registered already. 372 * 373 * @param c 374 */ 375 @SuppressWarnings("unchecked") 376 private <T> Node registerClass(final Class<T> c) 377 throws ClassHierarchyException { 378 if (c.isArray()) { 379 throw new UnsupportedOperationException("Can't register array types"); 380 } 381 try { 382 return getAlreadyBoundNode(c); 383 } catch (final NameResolutionException ignored) { 384 // node not bound yet 385 } 386 387 final Node n = buildPathToNode(c); 388 389 if (n instanceof ClassNode) { 390 final ClassNode<T> cn = (ClassNode<T>) n; 391 final Class<T> superclass = (Class<T>) c.getSuperclass(); 392 if (superclass != null) { 393 try { 394 ((ClassNode<T>) getAlreadyBoundNode(superclass)).putImpl(cn); 395 } catch (final NameResolutionException e) { 396 throw new IllegalStateException(e); 397 } 398 } 399 for (final Class<?> interf : c.getInterfaces()) { 400 try { 401 ((ClassNode<T>) getAlreadyBoundNode(interf)).putImpl(cn); 402 } catch (final NameResolutionException e) { 403 throw new IllegalStateException(e); 404 } 405 } 406 } 407 return n; 408 } 409 410 @Override 411 public PackageNode getNamespace() { 412 return namespace; 413 } 414 415 public ParameterParser getParameterParser() { 416 return parameterParser; 417 } 418 419 @Override 420 public synchronized boolean isImplementation(final ClassNode<?> inter, final ClassNode<?> impl) { 421 return impl.isImplementationOf(inter); 422 } 423 424 @Override 425 public synchronized ClassHierarchy merge(final ClassHierarchy ch) { 426 if (this == ch) { 427 return this; 428 } 429 if (!(ch instanceof ClassHierarchyImpl)) { 430 throw new UnsupportedOperationException("Can't merge java and non-java class hierarchies yet!"); 431 } 432 if (this.jars.size() == 0) { 433 return ch; 434 } 435 final ClassHierarchyImpl chi = (ClassHierarchyImpl) ch; 436 final HashSet<URL> otherJars = new HashSet<>(); 437 otherJars.addAll(chi.jars); 438 final HashSet<URL> myJars = new HashSet<>(); 439 myJars.addAll(this.jars); 440 if (myJars.containsAll(otherJars)) { 441 return this; 442 } else if (otherJars.containsAll(myJars)) { 443 return ch; 444 } else { 445 myJars.addAll(otherJars); 446 return new ClassHierarchyImpl(myJars.toArray(new URL[0])); 447 } 448 } 449}