MessageDefinitionReflectionProvider.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.message.definition;
00018 
00019 import com.google.common.annotations.VisibleForTesting;
00020 import com.google.common.collect.Maps;
00021 
00022 import org.ros.exception.RosMessageRuntimeException;
00023 import org.ros.message.MessageDefinitionProvider;
00024 import org.ros.message.MessageIdentifier;
00025 
00026 import java.util.Collection;
00027 import java.util.Map;
00028 
00038 public class MessageDefinitionReflectionProvider implements MessageDefinitionProvider {
00039 
00040   private static final String DEFINITION_FIELD = "_DEFINITION";
00041 
00042   private final Map<String, String> cache;
00043 
00044   public MessageDefinitionReflectionProvider() {
00045     cache = Maps.newConcurrentMap();
00046   }
00047 
00048   @Override
00049   public String get(String messageType) {
00050     String messageDefinition = cache.get(messageType);
00051     if (messageDefinition == null) {
00052       String className = messageType.replace("/", ".");
00053       try {
00054         Class<?> loadedClass = getClass().getClassLoader().loadClass(className);
00055         messageDefinition = (String) loadedClass.getDeclaredField(DEFINITION_FIELD).get(null);
00056         cache.put(messageType, messageDefinition);
00057       } catch (Exception e) {
00058         throw new RosMessageRuntimeException(e);
00059       }
00060     }
00061     return messageDefinition;
00062   }
00063 
00064   @Override
00065   public boolean has(String messageType) {
00066     try {
00067       get(messageType);
00068     } catch (Exception e) {
00069       return false;
00070     }
00071     return true;
00072   }
00073 
00074   @Override
00075   public Collection<String> getPackages() {
00076     throw new UnsupportedOperationException();
00077   }
00078 
00079   @Override
00080   public Collection<MessageIdentifier> getMessageIdentifiersByPackage(String pkg) {
00081     throw new UnsupportedOperationException();
00082   }
00083 
00084   @VisibleForTesting
00085   public void add(String messageType, String messageDefinition) {
00086     cache.put(messageType, messageDefinition);
00087   }
00088 }


rosjava_bootstrap
Author(s): Daniel Stonier , Damon Kohler
autogenerated on Fri Aug 28 2015 12:41:44