|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Oracle and/or its affiliates. |
| 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 | + |
| 17 | +package io.helidon.codegen.apt; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | + |
| 21 | +import javax.annotation.processing.Filer; |
| 22 | +import javax.lang.model.element.Element; |
| 23 | +import javax.tools.FileObject; |
| 24 | +import javax.tools.StandardLocation; |
| 25 | + |
| 26 | +import io.helidon.codegen.CodegenException; |
| 27 | +import io.helidon.codegen.FilerResource; |
| 28 | + |
| 29 | +class FilerResourceImpl implements FilerResource { |
| 30 | + private final Filer filer; |
| 31 | + private final String location; |
| 32 | + private final Element[] originatingElements; |
| 33 | + private final FileObject originalResource; // may be null |
| 34 | + |
| 35 | + private byte[] currentBytes; |
| 36 | + |
| 37 | + private boolean modified; |
| 38 | + |
| 39 | + FilerResourceImpl(Filer filer, String location, Element[] originatingElements) { |
| 40 | + this.filer = filer; |
| 41 | + this.location = location; |
| 42 | + this.originatingElements = originatingElements; |
| 43 | + this.originalResource = null; |
| 44 | + this.currentBytes = new byte[0]; |
| 45 | + } |
| 46 | + |
| 47 | + FilerResourceImpl(Filer filer, |
| 48 | + String location, |
| 49 | + Element[] originatingElements, |
| 50 | + FileObject originalResource, |
| 51 | + byte[] existingBytes) { |
| 52 | + this.filer = filer; |
| 53 | + this.location = location; |
| 54 | + this.originatingElements = originatingElements; |
| 55 | + this.originalResource = originalResource; |
| 56 | + this.currentBytes = existingBytes; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public byte[] bytes() { |
| 61 | + return Arrays.copyOf(currentBytes, currentBytes.length); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void bytes(byte[] newBytes) { |
| 66 | + currentBytes = Arrays.copyOf(newBytes, newBytes.length); |
| 67 | + modified = true; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void write() { |
| 72 | + if (modified) { |
| 73 | + if (originalResource != null) { |
| 74 | + originalResource.delete(); |
| 75 | + } |
| 76 | + try { |
| 77 | + FileObject newResource = filer.createResource(StandardLocation.CLASS_OUTPUT, |
| 78 | + "", |
| 79 | + location, |
| 80 | + originatingElements); |
| 81 | + try (var os = newResource.openOutputStream()) { |
| 82 | + os.write(currentBytes); |
| 83 | + } |
| 84 | + } catch (Exception e) { |
| 85 | + throw new CodegenException("Failed to create resource: " + location, e); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments