You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JavaFileObjectUtils.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (C) 2012-present the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.pf4j.test;
  17. import com.google.common.io.ByteStreams;
  18. import javax.tools.JavaFileObject;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. /**
  22. * @author Decebal Suiu
  23. */
  24. public class JavaFileObjectUtils {
  25. private JavaFileObjectUtils() {}
  26. public static String getClassName(JavaFileObject object) {
  27. if (object.getKind() != JavaFileObject.Kind.CLASS) {
  28. throw new IllegalStateException("Only Kind.CLASS is supported");
  29. }
  30. String name = object.getName();
  31. // Remove "/CLASS_OUT/" from head and ".class" from tail
  32. name = name.substring(14, name.length() - 6);
  33. name = name.replace('/', '.');
  34. return name;
  35. }
  36. public static byte[] getAllBytes(JavaFileObject object) {
  37. try (InputStream in = object.openInputStream()) {
  38. return ByteStreams.toByteArray(in);
  39. } catch (IOException e) {
  40. throw new IllegalStateException(e);
  41. }
  42. }
  43. }